From e4dc3ba0cee459c3a567677d18d48380b8553583 Mon Sep 17 00:00:00 2001 From: janus_wel Date: Wed, 1 Oct 2008 22:56:55 +0000 Subject: refactoring. use Error object when throw exception. extract constant variables from code and define in NicoPlayerController.constants. git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@20436 d0d07461-0603-4401-acd4-de1884942a52 --- nicontroller.js | 309 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 182 insertions(+), 127 deletions(-) (limited to 'nicontroller.js') diff --git a/nicontroller.js b/nicontroller.js index 28965d7..29adea9 100644 --- a/nicontroller.js +++ b/nicontroller.js @@ -4,7 +4,7 @@ * @description this script give you keyboard opration for nicovideo.jp. * @description-ja ニコニコ動画のプレーヤーをキーボードで操作できるようにする。 * @author janus_wel - * @version 0.50 + * @version 0.51 * @minversion 1.2 * ==VimperatorPlugin== * @@ -62,7 +62,10 @@ * thanks to なまえ (no name ?) * refer: http://d.hatena.ne.jp/janus_wel/20080914/1221387317 * 2008/10/01 ver. 0.50 - add :nicodescription. - * + * 2008/10/02 ver. 0.51 - refactoring. + * - use Error object when throw exception. + * - extract constant variables from code + * and define in NicoPlayerController.constants. * */ /* @@ -102,15 +105,120 @@ EOM (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) { + var cookies = cookieString.split('; '); + var cookie = {}; + var key, val; + for (var i=0, max=cookies.length ; i