Javascript插件,用于在任何浏览器中读取url的cookie值

Javascript插件,用于在任何浏览器中读取url的cookie值,javascript,plugins,cookies,Javascript,Plugins,Cookies,您好,我是为浏览器编写插件的新手。我的要求是,我应该编写一个示例javascript插件代码来读取url的cookie值,并且它应该支持所有浏览器。我刚刚看到,我们无法直接从系统中的cookie文件读取数据,需要通过chrome的sqlite数据库进行读取。我被困在这里,任何帮助都将非常有用。使用此代码 $.cookie('the_cookie', 'the_value'); //Create expiring cookie, 7 days from then: $.cookie('the_co

您好,我是为浏览器编写插件的新手。我的要求是,我应该编写一个示例javascript插件代码来读取url的cookie值,并且它应该支持所有浏览器。我刚刚看到,我们无法直接从系统中的cookie文件读取数据,需要通过chrome的sqlite数据库进行读取。我被困在这里,任何帮助都将非常有用。

使用此代码

$.cookie('the_cookie', 'the_value');
//Create expiring cookie, 7 days from then:
$.cookie('the_cookie', 'the_value', { expires: 7 });

//Create expiring cookie, valid across entire page:
$.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });

//Read cookie
$.cookie('the_cookie'); // => 'the_value'
$.cookie('not_existing'); // => null

//Delete cookie by passing null as value:
$.cookie('the_cookie', null);

// Creating cookie with all availabl options
$.cookie('myCookie2', 'myValue2', { expires: 7, path: '/', domain: 'example.com', 
secure: true, raw: true });

并从此URL下载jquery cookie()

可以通过document.cookie以字符串格式访问cookie

一些例子: 你去看了吗

document.cookie = "YOUR_KEY_1=YOUR_VALUE_1;YOUR_KEY_2=YOUR_VALUE_2;..."
当你设置它时(一个接一个的cookie)

我为您创建了两个函数(get和set)以及如何使用它的示例。 (与IE6和Firefox 22兼容)

//cookie持续时间的定义
var my\u cookie\u duration\u hash={
天数:1,
小时:0,
分钟:0,
秒:0
};
//cookies键/值对的定义
var my_cookies={
键1:'value1=',
键2:“值2”
};
//让我们做些饼干
设置cookies(我的cookies、我的cookies、持续时间、散列“/”);
//我们去拿饼干吧
警告(获取cookies()['key1']);
/*****************************************************************
Cookie自制函数
*****************************************************************/
//获取JSON格式的cookie键/值
函数get_cookies(){
var cookies={};
cookies_str=document.cookie.split(“;”);
//处理cookies
对于(变量i=0;i

我希望它能有所帮助。

你已经尝试了什么?@mishik:我尝试了w3学校的示例代码。下面是代码。函数getCookie(c_name){var c_value=document.cookie;var c_start=c_value.indexOf(“+c_name+”);if(c_start=-1){c_start=c_value.indexOf(c_name+”;}if(c_start=-1){c_value=null;}else{c_start=c_value.indexOf(“=”,c_start)+1;var c_end=c_value.indexOf(“;”,c_start);if(c_end==-1){c_end=c_value.length;}c_value=unescape(c_value.substring(c_start,c_end))}警报(c_value)返回c_value;}但我不确定这将如何帮助我必须使用javascript而不是jquery开发
console.log(document.cookie)
/* "YOUR_KEY=YOUR_VALUE; expires=EXPIRATION_DATE; path=WHERE_YOUR_COOKIE_WILL_BE_ACCESSIBLE"
eg.
  user=599a9182df1...; expires=Tue, 15 Jul 2014 14:06:12 GMT; path=/logged_in
  So, the cookie "user" as the value 599a9182df1... (Some SHA1 example)
  It expires in 1 year (Won't be accessible if not renewed)
  Is only accessible under /logged_in
*/
// Definition of the cookie duration
var my_cookie_duration_hash = {
  days:     1,
  hours:    0,
  minutes:  0,
  seconds:  0
};

// Definition of the cookies key/value pair
var my_cookies = {
  key1: 'value1=',
  key2: 'value2'
};


// Let's set some cookies
set_cookies( my_cookies, my_cookie_duration_hash, '/');

// Let's get our cookies
window.alert( get_cookies()['key1']);


/*****************************************************************
  Cookie home made functions
*****************************************************************/

// Get the cookie key/value in JSON format
function get_cookies () {
  var cookies = {};

  cookies_str = document.cookie.split(';');

  // Processing the cookies
  for( var i = 0 ; i < cookies_str.length ; i++ ) {
    cookie = cookies_str[i];

    var cookie_match = cookie.match(/^([^=]*)=(.*)$/);

    // If The cookie exist, we get it back in a JSON
    if( cookie_match ) {
      cookies[cookie_match[1]] = cookie_match[2];
    }
  }

  // We return the cookies in a JSON format
  return cookies;
}

// Set the cookies
//   arg(0) : Cookies values
//      Format : Some { key: value } JSON object
//   arg(1) : Cookie duration
function set_cookies () {
  cookies         = arguments[0] || {};
  cookie_duration = arguments[1] || {};
  cookie_domain   = arguments[2] || '/';

  if( typeof(cookie_duration) === 'object' ) {
    my_cookie_duration_int = (cookie_duration.days    || 0) * 24 * 60 * 60 * 1000;
                           + (cookie_duration.hours   || 0) * 60 * 60 * 1000;
                           + (cookie_duration.minutes || 0) * 60 * 1000;
                           + (cookie_duration.seconds || 0) * 1000;
  } else if( typeof(cookie_duration) === 'number' ) {
    my_cookie_duration_int = cookie_duration;
  } else if( typeof(cookie_duration) === 'undefined' ) {
    my_cookie_duration_int = 0;
  } else if( typeof(cookie_duration) !== 'undefined' ) {
    console.error( typeof(cookie_duration) +' is not a recognize type for the cookie duration.');
  }

  // Calculation of the cookie end of validity
  var date     = new Date();
  var end_date = date.getTime() + my_cookie_duration_int;
  date.setTime ( end_date);

  var my_cookie_expiration_date = date.toGMTString();

  // Processing of the cookie
  for( var my_cookie in cookies ) {
    new_cookie = my_cookie +"="+ cookies[my_cookie]
               + "; expires="+   my_cookie_expiration_date
               + "; path="+      cookie_domain;

    // Definition of the cookies
    document.cookie = new_cookie;
  }
}