Instantly share code, notes, and snippets.
Last active
April 27, 2016 05:25
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save 958/1312071 to your computer and use it in GitHub Desktop.
[keysnail]Cookie Manager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var PLUGIN_INFO = | |
<KeySnailPlugin> | |
<name>Cookie Manager</name> | |
<description>Cookie Manager</description> | |
<description lang="ja">Cookie を管理</description> | |
<updateURL>https://gist.github.com/958/1312071/raw/cookie-manager.ks.js</updateURL> | |
<version>0.0.3</version> | |
<license>MIT</license> | |
<detail lang="ja"><![CDATA[ | |
=== 使い方 === | |
Cookie を管理します | |
エクステ 'cookie-manager-show-list' を実行すると、保存されている Cookie を一覧表示し、選択中の Cookie を削除できます | |
エクステ 'cookie-manager-show-host-list' を実行すると、Cookie が保存されているホストを一覧表示し、選択中のホストが保存している Cookie を一括削除します | |
次のような使い方をすることもできます | |
>|javascript| | |
//現在表示中のホストが保存している Cookie を一覧表示 | |
ext.exec('cookie-manager-show-list', content.location.host); | |
//現在表示中のホストが保存している Cookie を一括削除 | |
plugins.cookieManager.removeCookiesByHost(content.location.host); | |
//google の Cookie を一括削除 | |
plugins.cookieManager.removeCookiesByHost('google'); | |
||< | |
]]></detail> | |
</KeySnailPlugin>; | |
let cm = (function(){ | |
const CM = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager); | |
// chrome://browser/content/preferences/cookies.js | |
function makeStrippedHost(aHost) { | |
let formattedHost = aHost.charAt(0) == "." ? aHost.substring(1, aHost.length) : aHost; | |
return formattedHost.substring(0, 4) == "www." ? formattedHost.substring(4, formattedHost.length) : formattedHost; | |
} | |
function loadCookieAll() { | |
let e = CM.enumerator; | |
let cookies = []; | |
while (e.hasMoreElements()) { | |
let cookie = e.getNext(); | |
if (cookie && cookie instanceof Ci.nsICookie) { | |
let strippedHost = makeStrippedHost(cookie.host); | |
cookies.push({host: strippedHost, orgHost: cookie.host, name: cookie.name, path: cookie.path, value: cookie.value}); | |
} | |
else | |
break; | |
} | |
return cookies; | |
} | |
function loadCookieByHost() { | |
let e = CM.enumerator; | |
let cookies = {}; | |
while (e.hasMoreElements()) { | |
let cookie = e.getNext(); | |
if (cookie && cookie instanceof Ci.nsICookie) { | |
let strippedHost = makeStrippedHost(cookie.host); | |
if (!cookies[strippedHost]) | |
cookies[strippedHost] = []; | |
cookies[strippedHost].push({host: strippedHost, orgHost: cookie.host, name: cookie.name, path: cookie.path, value: cookie.value}); | |
} | |
else | |
break; | |
} | |
return cookies; | |
} | |
function showCookiesByHost(initialInput) { | |
let cookies = loadCookieByHost(); | |
let collection = [for (kv of util.keyValues(cookies)) kv[0]]; | |
prompt.selector({ | |
message: "hosts:", | |
collection: collection, | |
initialInput: initialInput, | |
header: ['Host'], | |
actions:[ | |
[ | |
function(aIndex, rows) { | |
if (aIndex >= 0) { | |
for (let cookie of util.values(cookies[rows[aIndex]])) | |
CM.remove(cookie.orgHost, cookie.name, cookie.path, false); | |
// delete cookies[rows[aIndex]]; | |
// rows.splice(aIndex, 1); | |
// prompt.refresh(aIndex); | |
} | |
}, "Remove cookie","remove-cookie" | |
], | |
], | |
}); | |
} | |
function showCookies(host) { | |
if (host) | |
host = makeStrippedHost(host); | |
let cookies = loadCookieAll(); | |
let collection = [ | |
for (val of util.values(cookies)) | |
if (!host || (host && val.host.indexOf(host) != -1)) | |
[val.host, val.name, val.path, val.value] | |
]; | |
prompt.selector({ | |
message: "cookies:", | |
flags: [0, IGNORE, IGNORE, IGNORE], | |
collection: collection, | |
header: ['Host', 'Name', 'Path', 'Value'], | |
actions:[ | |
[ | |
function(aIndex, rows) { | |
if (aIndex >= 0) { | |
let cookie = cookies[aIndex]; | |
CM.remove(cookie.orgHost, cookie.name, cookie.path, false); | |
// cookies.splice(aIndex, 1); | |
// rows.splice(aIndex, 1); | |
// prompt.refresh(aIndex); | |
} | |
}, "Remove cookie","remove-cookie" | |
], | |
], | |
}); | |
} | |
function removeCookiesByHost(host) { | |
if (host) | |
host = makeStrippedHost(host); | |
let cookies = loadCookieByHost(); | |
if (cookies[host]) | |
for (let cookie in util.values(cookies[host])) | |
CM.remove(cookie.orgHost, cookie.name, cookie.path, false); | |
} | |
return { | |
showCookies: showCookies, | |
showCookiesByHost: showCookiesByHost, | |
removeCookiesByHost: removeCookiesByHost, | |
}; | |
})(); | |
plugins.withProvides(function (provide) { | |
provide('cookie-manager-show-list', function(ev, arg){ | |
cm.showCookies(arg); | |
}, 'Show cookie list'); | |
provide('cookie-manager-show-host-list', function(ev, arg){ | |
cm.showCookiesByHost(arg); | |
}, 'Show host list'); | |
}, PLUGIN_INFO); | |
plugins.cookieManager = cm; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment