Javascript插件可在任何浏览器中读取网址的cookie值

时间:2013-07-04 04:26:20

标签: javascript plugins cookies

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

2 个答案:

答案 0 :(得分: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 });

并从此网址https://github.com/carhartl/jquery-cookie/blob/master/jquery.cookie.js

下载jquery cookie()

答案 1 :(得分:0)

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

一些例子: 你读过它吗

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

当你设置它(cookie由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;
  }
}

我希望它有所帮助。