こんにちは、ブラウザ用のプラグインを書くのは初めてです。私の要件は、URL の Cookie の値を読み取るためのサンプル JavaScript プラグイン コードを記述し、すべてのブラウザーをサポートする必要があることです。システム内の Cookie ファイルから直接読み取ることができず、Chrome の sqlite データベースを介して読み取る必要があることがわかりました。続行する方法が本当に役立つので、私はここで立ち往生しています。
2 に答える
0
Cookie は、document.cookie を介して文字列形式でアクセスできます。
いくつかの例:あなたはそれを読みましたか
document.cookie = "YOUR_KEY_1=YOUR_VALUE_1;YOUR_KEY_2=YOUR_VALUE_2;..."
そして、あなたがそれを設定すると(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
*/
2 つの関数 (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 に答える