-1

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

4

2 回答 2

0

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

一些例子:你读过吗

document.cookie = "YOUR_KEY_1=YOUR_VALUE_1;YOUR_KEY_2=YOUR_VALUE_2;..."

当你设置它时(cookie by cookie)

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
*/

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

// 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;
  }
}

我希望它有所帮助。

于 2013-07-15T14:10:44.293 回答
0

使用此代码。

$.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 https://github.com/carhartl/jquery-cookie/blob/master/jquery.cookie.js下载 jquery cookie()

于 2013-07-04T04:51:02.093 回答