| 1 | var PLUGIN_INFO = |
|---|
| 2 | <VimperatorPlugin> |
|---|
| 3 | <name>{NAME}</name> |
|---|
| 4 | <description>Manage Cookies (list, remove, add/remove permission)</description> |
|---|
| 5 | <author mail="teramako@gmail.com" homepage="http://vimperator.g.hatena.ne.jp/teramako/">teramako</author> |
|---|
| 6 | <version>1.1</version> |
|---|
| 7 | <license>MPL 1.1/GPL 2.0/LGPL 2.1</license> |
|---|
| 8 | <minVersion>2.0pre</minVersion> |
|---|
| 9 | <maxVersion>2.0</maxVersion> |
|---|
| 10 | <updateURL>http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/cookieManager.js</updateURL> |
|---|
| 11 | <detail lang="ja"><![CDATA[ |
|---|
| 12 | Cookie の管理をするプラグイン |
|---|
| 13 | |
|---|
| 14 | == Command == |
|---|
| 15 | -permオプションの有無で2種類に分かれる |
|---|
| 16 | |
|---|
| 17 | === Cookieのパーミッション === |
|---|
| 18 | |
|---|
| 19 | :cookiem[anager] list {hostname and path}: |
|---|
| 20 | {hostname and path}のCookieを表示 |
|---|
| 21 | |
|---|
| 22 | :cookiem[anager] remove {hostname and path}: |
|---|
| 23 | {hostname and path}のCookieを削除 |
|---|
| 24 | |
|---|
| 25 | === 現在保存されているCookie === |
|---|
| 26 | |
|---|
| 27 | :cookiem[anager] -p[erm] list {hostname}: |
|---|
| 28 | {hostname}のCookieのパーミッションを表示 |
|---|
| 29 | |
|---|
| 30 | :cookiem[anager] -p[erm] add {hostname} {capability}: |
|---|
| 31 | {hostname}のCookieのパーミッションを設定 |
|---|
| 32 | |
|---|
| 33 | :cookiem[anager] -p[erm] list {hostname}: |
|---|
| 34 | {hostname}のCookieのパーミッションを削除 |
|---|
| 35 | |
|---|
| 36 | == pageinfo == |
|---|
| 37 | :pageinfo c で現在開いているホストのCookieを表示 |
|---|
| 38 | |
|---|
| 39 | == どうでも良いこと == |
|---|
| 40 | 補完機能を存分にお楽しみください :) |
|---|
| 41 | |
|---|
| 42 | ]]></detail> |
|---|
| 43 | </VimperatorPlugin>; |
|---|
| 44 | |
|---|
| 45 | liberator.plugins.cookieManager = (function(){ |
|---|
| 46 | |
|---|
| 47 | const CM = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2); |
|---|
| 48 | const PM = Cc["@mozilla.org/permissionmanager;1"].getService(Ci.nsIPermissionManager); |
|---|
| 49 | const I_CPM = Ci.nsICookiePermission; |
|---|
| 50 | const PERM_TYPE = "cookie"; |
|---|
| 51 | |
|---|
| 52 | function getIterator(enum, interface){ |
|---|
| 53 | while (enum.hasMoreElements()){ |
|---|
| 54 | let obj = enum.getNext().QueryInterface(interface); |
|---|
| 55 | yield obj; |
|---|
| 56 | } |
|---|
| 57 | } |
|---|
| 58 | function cookieIterator() getIterator(CM.enumerator, Ci.nsICookie2); |
|---|
| 59 | function cookiePermissionIterator(){ |
|---|
| 60 | for (let perm in getIterator(PM.enumerator, Ci.nsIPermission)){ |
|---|
| 61 | if (perm.type = PERM_TYPE) |
|---|
| 62 | yield perm; |
|---|
| 63 | } |
|---|
| 64 | } |
|---|
| 65 | function capabilityToString(capability){ |
|---|
| 66 | switch (capability){ |
|---|
| 67 | case I_CPM.ACCESS_ALLOW: // 1 |
|---|
| 68 | return "ALLOW"; |
|---|
| 69 | case I_CPM.ACCESS_DENY: // 2 |
|---|
| 70 | return "DENY"; |
|---|
| 71 | case I_CPM.ACCESS_SESSION: // 8 |
|---|
| 72 | return "ONLY_SESSION"; |
|---|
| 73 | default: |
|---|
| 74 | return "DEFAULT"; |
|---|
| 75 | } |
|---|
| 76 | } |
|---|
| 77 | function stringToCapability(str){ |
|---|
| 78 | switch (str){ |
|---|
| 79 | case "ALLOW": |
|---|
| 80 | return I_CPM.ACCESS_ALLOW; |
|---|
| 81 | case "DENY": |
|---|
| 82 | return I_CPM.ACCESS_DENY; |
|---|
| 83 | case "ONLY_SESSION": |
|---|
| 84 | return I_CPM.ACCESS_SESSION; |
|---|
| 85 | default: |
|---|
| 86 | return I_CPM.ACCESS_DEFAULT; |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | function getHost(){ |
|---|
| 90 | var host; |
|---|
| 91 | try { |
|---|
| 92 | host = content.document.location.host; |
|---|
| 93 | } catch (e){} |
|---|
| 94 | return host; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | // -------------------------------------------------------- |
|---|
| 98 | // PageInfo |
|---|
| 99 | // -------------------------------------------------------- |
|---|
| 100 | buffer.addPageInfoSection("c", "Cookies", function(verbose){ |
|---|
| 101 | var hostname; |
|---|
| 102 | try { |
|---|
| 103 | hostname = content.window.location.host; |
|---|
| 104 | } catch (e){ return []; } |
|---|
| 105 | return [[c.rawHost + c.path, c.name + " = " + c.value] for (c in cManager.stored.getByHostAndPath(hostname))]; |
|---|
| 106 | }); |
|---|
| 107 | |
|---|
| 108 | // -------------------------------------------------------- |
|---|
| 109 | // Command |
|---|
| 110 | // -----------------------------------------------------{{{ |
|---|
| 111 | commands.addUserCommand(["cookiem[anager]"], "Cookie Management", |
|---|
| 112 | function(args){ |
|---|
| 113 | if (args["-perm"]){ |
|---|
| 114 | switch (args[0]){ |
|---|
| 115 | case "list": |
|---|
| 116 | let list = cManager.permission.list(args[1]); |
|---|
| 117 | liberator.echo(template.table("Cookie Permission", list)); |
|---|
| 118 | break; |
|---|
| 119 | case "remove": |
|---|
| 120 | if (cManager.permission.remove(args[1])){ |
|---|
| 121 | liberator.echo("Removed permission: `" + args[1] + "'"); |
|---|
| 122 | } else { |
|---|
| 123 | liberator.echo("Failed to removed permission: `" + args[1] + "'"); |
|---|
| 124 | } |
|---|
| 125 | break; |
|---|
| 126 | case "add": |
|---|
| 127 | cManager.permission.add(args[1], args[2]); |
|---|
| 128 | break; |
|---|
| 129 | default: |
|---|
| 130 | liberator.echoerr("Invalid sub-command."); |
|---|
| 131 | } |
|---|
| 132 | return; |
|---|
| 133 | } |
|---|
| 134 | var host = args[1] || getHost(); |
|---|
| 135 | if (!host) return; |
|---|
| 136 | switch (args[0]){ |
|---|
| 137 | case "list": |
|---|
| 138 | let xml = <></>; |
|---|
| 139 | let tree = cManager.stored.getTree(host); |
|---|
| 140 | for (let name in tree){ |
|---|
| 141 | xml += template.table(name, [[c.name, c.value] for each(c in tree[name])]); |
|---|
| 142 | } |
|---|
| 143 | liberator.echo(xml, true); |
|---|
| 144 | break; |
|---|
| 145 | case "remove": |
|---|
| 146 | cManager.stored.remove(host); |
|---|
| 147 | break; |
|---|
| 148 | default: |
|---|
| 149 | liberator.echoerr("Invalid sub-command."); |
|---|
| 150 | } |
|---|
| 151 | }, { |
|---|
| 152 | options: [ |
|---|
| 153 | [["-perm", "-p"], commands.OPTION_NOARG] |
|---|
| 154 | ], |
|---|
| 155 | completer: function(context, args){ |
|---|
| 156 | if (args["-perm"]){ |
|---|
| 157 | plugins.cookieManager.permission.completer(context, args); |
|---|
| 158 | } else { |
|---|
| 159 | plugins.cookieManager.stored.completer(context, args); |
|---|
| 160 | } |
|---|
| 161 | }, |
|---|
| 162 | }, true); |
|---|
| 163 | // Command End }}} |
|---|
| 164 | var cManager = { |
|---|
| 165 | stored: { // {{{ |
|---|
| 166 | getByHostAndPath: function(hostAndPath){ |
|---|
| 167 | for (let cookie in cookieIterator()){ |
|---|
| 168 | if (!hostAndPath || (cookie.rawHost + cookie.path).indexOf(hostAndPath) == 0) |
|---|
| 169 | yield cookie; |
|---|
| 170 | } |
|---|
| 171 | }, |
|---|
| 172 | remove: function(hostAndPath){ |
|---|
| 173 | if (!hostAndPath) return false; |
|---|
| 174 | for (let cookie in this.getByHostAndPath(hostAndPath)){ |
|---|
| 175 | CM.remove(cookie.host, cookie.name, cookie.path, false); |
|---|
| 176 | } |
|---|
| 177 | return true; |
|---|
| 178 | }, |
|---|
| 179 | getTree: function(hostAndPath){ |
|---|
| 180 | var tree = {}; |
|---|
| 181 | function getTree(name){ |
|---|
| 182 | if (name in tree) return tree[name]; |
|---|
| 183 | tree[name] = []; |
|---|
| 184 | return tree[name]; |
|---|
| 185 | } |
|---|
| 186 | for (let cookie in this.getByHostAndPath(hostAndPath)){ |
|---|
| 187 | getTree(cookie.rawHost + cookie.path).push(cookie); |
|---|
| 188 | } |
|---|
| 189 | return tree; |
|---|
| 190 | }, |
|---|
| 191 | subcommands: [ |
|---|
| 192 | ["list", "list cookie permission"], |
|---|
| 193 | ["remove", "remove cookie premission"] |
|---|
| 194 | ], |
|---|
| 195 | completer: function(context, args){ |
|---|
| 196 | if (args.length == 1){ |
|---|
| 197 | context.title = ["SubCommand", "Description"]; |
|---|
| 198 | context.completions = context.filter ? |
|---|
| 199 | this.subcommands.filter(function(c) c[0].indexOf(context.filter) >= 0) : |
|---|
| 200 | this.subcommands; |
|---|
| 201 | } else if (args.length == 2){ |
|---|
| 202 | let list = util.Array.uniq([c.rawHost + c.path for (c in this.getByHostAndPath())]).map(function(host) [host, "-"]); |
|---|
| 203 | context.title = ["Host and Path"]; |
|---|
| 204 | context.completions = context.filter ? |
|---|
| 205 | list.filter(function(c) c[0].indexOf(context.filter) >= 0) : |
|---|
| 206 | list; |
|---|
| 207 | } |
|---|
| 208 | }, |
|---|
| 209 | }, // }}} |
|---|
| 210 | permission: { // {{{ |
|---|
| 211 | getByHost: function(hostname){ |
|---|
| 212 | for (let permission in cookiePermissionIterator()){ |
|---|
| 213 | if (permission.host == hostname) |
|---|
| 214 | return permission; |
|---|
| 215 | } |
|---|
| 216 | return null; |
|---|
| 217 | }, |
|---|
| 218 | add: function(hostname, capability, force){ |
|---|
| 219 | var uri = util.newURI("http://" + hostname); |
|---|
| 220 | var perm = this.getByHost(hostname); |
|---|
| 221 | switch (typeof capability){ |
|---|
| 222 | case "string": |
|---|
| 223 | capability = stringToCapability(capability); |
|---|
| 224 | break; |
|---|
| 225 | case "number": |
|---|
| 226 | break; |
|---|
| 227 | default: |
|---|
| 228 | throw "Invalid capability"; |
|---|
| 229 | } |
|---|
| 230 | if (perm && force){ |
|---|
| 231 | this.remove(hostname); |
|---|
| 232 | } |
|---|
| 233 | PM.add(uri, PERM_TYPE, capability); |
|---|
| 234 | }, |
|---|
| 235 | remove: function(hostname){ |
|---|
| 236 | if (this.getByHost(hostname)){ |
|---|
| 237 | PM.remove(hostname, PERM_TYPE); |
|---|
| 238 | return true; |
|---|
| 239 | } |
|---|
| 240 | return false; |
|---|
| 241 | }, |
|---|
| 242 | list: function(filterReg){ |
|---|
| 243 | if (filterReg && !(filterReg instanceof RegExp)){ |
|---|
| 244 | filterReg = new RegExp(filterReg.toString()); |
|---|
| 245 | } else if (!filterReg){ |
|---|
| 246 | filterReg = new RegExp(""); |
|---|
| 247 | } |
|---|
| 248 | return [[p.host, capabilityToString(p.capability)] for (p in cookiePermissionIterator())].filter(function($_) filterReg.test($_[0])); |
|---|
| 249 | }, |
|---|
| 250 | subcommands: [ |
|---|
| 251 | ["list", "list cookie permission"], |
|---|
| 252 | ["add", "add cookie permission"], |
|---|
| 253 | ["remove", "remove cookie premission"] |
|---|
| 254 | ], |
|---|
| 255 | capabilityList: [ |
|---|
| 256 | ["ALLOW", "-"], |
|---|
| 257 | ["DENY", "-"], |
|---|
| 258 | ["ONLY_SESSION", "-"] |
|---|
| 259 | ], |
|---|
| 260 | completer: function(context, args){ |
|---|
| 261 | if (args.length == 1){ |
|---|
| 262 | context.title = ["SubCommand", "Description"]; |
|---|
| 263 | context.completions = context.filter ? |
|---|
| 264 | this.subcommands.filter(function(c) c[0].indexOf(context.filter) >= 0) : |
|---|
| 265 | this.subcommands; |
|---|
| 266 | } else { |
|---|
| 267 | let suggestion = []; |
|---|
| 268 | switch (args[0]){ |
|---|
| 269 | case "add": |
|---|
| 270 | if (args.length == 3){ |
|---|
| 271 | context.title = ["Capability"]; |
|---|
| 272 | context.completions = context.filter ? |
|---|
| 273 | this.capabilityList.filter(function($_) $_[0].toLowerCase().indexOf(context.filter.toLowerCase()) == 0) : |
|---|
| 274 | this.capabilityList; |
|---|
| 275 | } else if (args.length == 2){ |
|---|
| 276 | let host = getHost(); |
|---|
| 277 | if (host){ |
|---|
| 278 | let hosts = []; |
|---|
| 279 | host.split(".").reduceRight(function(p, c){ |
|---|
| 280 | let domain = c + "." + p; |
|---|
| 281 | hosts.push([domain, "-"]); |
|---|
| 282 | return domain; |
|---|
| 283 | }); |
|---|
| 284 | suggestion = hosts.reverse(); |
|---|
| 285 | context.title = ["Current Host"]; |
|---|
| 286 | context.completions = context.filter ? |
|---|
| 287 | suggestion.filter(function($_) $_[0].indexOf(context.filter) >= 0) : suggestion; |
|---|
| 288 | return; |
|---|
| 289 | } |
|---|
| 290 | } |
|---|
| 291 | case "list": |
|---|
| 292 | case "remove": |
|---|
| 293 | if (args.length > 2) return; |
|---|
| 294 | context.title = ["Host", "Capability"]; |
|---|
| 295 | let list = this.list(); |
|---|
| 296 | context.completions = context.filter ? |
|---|
| 297 | list.filter(function($_) $_[0].indexOf(context.filter) >= 0) : list; |
|---|
| 298 | } |
|---|
| 299 | } |
|---|
| 300 | }, |
|---|
| 301 | }, // }}} |
|---|
| 302 | }; |
|---|
| 303 | return cManager; |
|---|
| 304 | })(); |
|---|
| 305 | |
|---|
| 306 | // vim: sw=4 ts=4 et fdm=marker: |
|---|