From 0152d741a8d36dea375d8bc119994bbe3e940dc2 Mon Sep 17 00:00:00 2001 From: janus_wel Date: Thu, 6 Nov 2008 09:59:16 +0000 Subject: import git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@22832 d0d07461-0603-4401-acd4-de1884942a52 --- cookie.js | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 cookie.js (limited to 'cookie.js') diff --git a/cookie.js b/cookie.js new file mode 100644 index 0000000..c9092f8 --- /dev/null +++ b/cookie.js @@ -0,0 +1,117 @@ +/* + * ==VimperatorPlugin== + * @name cookie.js + * @description display cookie in ':pageinfo'. + * @description-ja :pageinfo で cookie を表示する + * @author janus_wel + * @version 0.10 + * @minversion 2.0pre + * ==/VimperatorPlugin== + * + * LICENSE + * New BSD License + * + * USAGE + * set 'c' to option 'pageinfo' and type ':pageinfo' + * + * EXAMPLE + * set pageinfo=gfmc + * + * HISTORY + * 2008/11/06 ver. 0.10 - initial written. + * + */ + +( function () { + +// class definition +// cookie manager +function CookieManager() { + this.initialize.apply(this, arguments); +} +CookieManager.prototype = { + initialize: function (uri) { + const Cc = Components.classes; + const Ci = Components.interfaces; + + const MOZILLA = '@mozilla.org/'; + const IO_SERVICE = MOZILLA + 'network/io-service;1'; + const COOKIE_SERVICE = MOZILLA + 'cookieService;1'; + + this.ioService = Cc[IO_SERVICE].getService(Ci.nsIIOService); + this.cookieService = Cc[COOKIE_SERVICE].getService(Ci.nsICookieService); + if(!this.ioService || !this.cookieService) { + throw new Error('error on CookieManager initialize.'); + } + + this.readCookie(uri); + }, + + readCookie: function (uri) { + if(uri) { + this.uri = uri; + this.uriObject = this.ioService.newURI(uri, null, null); + this.cookie = this._deserializeCookie(this._getCookieString()); + } + }, + + _getCookieString: function () { + return this.uriObject + ? this.cookieService.getCookieString(this.uriObject, null) + : null; + }, + + _setCookieString: function (cookieString) { + if(this.uriObject && cookieString) { + this.cookieService.setCookieString(this.uriObject, null, cookieString, null); + } + }, + + _deserializeCookie: function (cookieString) { + let cookies = cookieString.split('; '); + let cookie = {}; + let key, val; + for (let i=0, max=cookies.length ; i