diff options
Diffstat (limited to 'extensions/requestpolicy')
-rw-r--r-- | extensions/requestpolicy | 624 |
1 files changed, 333 insertions, 291 deletions
diff --git a/extensions/requestpolicy b/extensions/requestpolicy index 5ec6d6d4..f8d6b62e 100644 --- a/extensions/requestpolicy +++ b/extensions/requestpolicy @@ -87,338 +87,380 @@ INFO>*/ var me = "requestpolicy"; var defaultConfig = { -//<DEFAULT_CONFIG - // path to a whitelist - whiteList : data.configDir + "/" + data.profile + "/requestpolicy.json", + //<DEFAULT_CONFIG + // path to a whitelist + whiteList : data.configDir + "/" + data.profile + "/requestpolicy.json", - // shortcut to block/allow requests - shortcut : "erp", + // shortcut to block/allow requests + shortcut : "erp", - // shortcut to unblock requests from current site that are blocked on all - // sites - unblockCurrent : "erC", + // shortcut to unblock requests from current site that are blocked on all + // sites + unblockCurrent : "erC", - // shortcut to unblock requests that are blocked on all sites - unblockAll : "erA", + // shortcut to unblock requests that are blocked on all sites + unblockAll : "erA", - // reload current site after blocking / unblocking a request - autoreload : false, + // reload current site after blocking / unblocking a request + autoreload : false, - // notify about blocked requests - notify : false + // notify about blocked requests + notify : false -//>DEFAULT_CONFIG + //>DEFAULT_CONFIG }; var config = {}; var sigs = { - resource : -1, - navigation : -1, - loadFinished : -1 + resource : -1, + navigation : -1, + loadFinished : -1 }; var persistentList = null; var tmpList = {}; -var getPrivate = (function () { - var identifier = {}; - return function (wv) { - var p = wv.getPrivate("foo", identifier); - if (p === null) { - p = { domains : [], blocked : 0 }; - wv.setPrivate("foo", p, identifier); - } - return p; - }; +var getPrivate = (function () +{ + var identifier = {}; + return function (wv) { + var p = wv.getPrivate("foo", identifier); + if (!p) + { + p = { domains : [], blocked : 0 }; + wv.setPrivate("foo", p, identifier); + } + return p; + }; })(); -function listAdd(o, key, value, doWrite) { - if (!o[key]) - o[key] = []; - if (o[key].fastIndexOf(value) == -1) - o[key].push(value); - if (doWrite) - io.write(config.whiteList, "w", JSON.stringify(persistentList)); -} -function listRemove(o, firstParty, domain, doWrite) { - var idx; - if (o[firstParty] && (idx = o[firstParty].fastIndexOf(domain)) != -1) { - o[firstParty].splice(idx, 1); - if (o[firstParty].length === 0) - delete o[firstParty]; +function listAdd(o, key, value, doWrite) +{ + if (!o[key]) + o[key] = []; + if (o[key].fastIndexOf(value) == -1) + o[key].push(value); if (doWrite) - io.write(config.whiteList, "w", JSON.stringify(persistentList)); - return true; - } - return false; + io.write(config.whiteList, "w", JSON.stringify(persistentList)); +} +function listRemove(o, firstParty, domain, doWrite) +{ + var idx; + if (o[firstParty] && (idx = o[firstParty].fastIndexOf(domain)) != -1) + { + o[firstParty].splice(idx, 1); + if (o[firstParty].length === 0) + delete o[firstParty]; + if (doWrite) + io.write(config.whiteList, "w", JSON.stringify(persistentList)); + return true; + } + return false; } // MENU {{{ -function showMenu() { - var tmpWhiteListed, whiteListed; - var isWhiteListed = false; - var dom, i, l, domains, labels, currentDomain; - - var domain = tabs.current.mainFrame.domain; - if (domain === null) - return; - - domains = getPrivate(tabs.current).domains; - labels = []; - currentDomain = tabs.current.mainFrame.domain; - - for (i=0, l=domains.length; i<l; ++i){ - (function(dom) { - if (persistentList._alwaysBlock && persistentList._alwaysBlock.fastIndexOf(dom) != -1) - return; - whiteListed = persistentList[domain] && persistentList[domain].fastIndexOf(dom) != -1; - tmpWhiteListed = tmpList[domain] && tmpList[domain].fastIndexOf(dom) != -1; - if (!persistentList._always || persistentList._always.fastIndexOf(dom) == -1) { - if (!whiteListed && !tmpWhiteListed) { - labels.push({ - left : "[" + dom + "] allow", +function showMenu() +{ + var tmpWhiteListed, whiteListed; + var isWhiteListed = false; + var dom, i, l, domains, labels, currentDomain; + + var domain = tabs.current.mainFrame.domain; + if (domain === null) + return; + + domains = getPrivate(tabs.current).domains; + labels = []; + currentDomain = tabs.current.mainFrame.domain; + + for (i=0, l=domains.length; i<l; ++i) + { + (function(dom) + { + if (persistentList._alwaysBlock && persistentList._alwaysBlock.fastIndexOf(dom) != -1) + return; + + whiteListed = persistentList[domain] && persistentList[domain].fastIndexOf(dom) != -1; + tmpWhiteListed = tmpList[domain] && tmpList[domain].fastIndexOf(dom) != -1; + if (!persistentList._always || persistentList._always.fastIndexOf(dom) == -1) + { + if (!whiteListed && !tmpWhiteListed) + { + labels.push({ + left : "[" + dom + "] allow", + action : function () { + listAdd(persistentList, currentDomain, dom, true); + } + }); + labels.push({ + left : "[" + dom + "] allow temporarily", + action : function() { + listAdd(tmpList, currentDomain, dom, false); + } + }); + } + + else + { + labels.push({ + left : "[" + dom + "] block", + action : function() { + listRemove(persistentList, currentDomain, dom, true); + listRemove(tmpList, currentDomain, dom, false); + } + }); + } + } + isWhiteListed = isWhiteListed || whiteListed || tmpWhiteListed; + if (!persistentList._always || persistentList._always.fastIndexOf(dom) == -1) + { + labels.push({ + left : "[" + dom + "] allow on all sites", + action : function() { + listAdd(persistentList, "_always", dom, true); + } + }); + } + else + { + labels.push({ + left : "[" + dom + "] don't allow on all sites", + action : function() { + listRemove(persistentList, "_always", dom, true); + } + }); + } + labels.push({ + left : "[" + dom + "] block on all sites", + action : function () { + listAdd(persistentList, "_alwaysBlock", dom, true); + } + }); + })(domains[i]); + } + var allAllowed = (persistentList._all && persistentList._all.fastIndexOf(domain) != -1) || + (tmpList._all && tmpList._all.fastIndexOf(domain) != -1); + if (isWhiteListed || allAllowed) + { + labels.unshift({ + left : "Block all requests on " + domain, action : function () { - listAdd(persistentList, currentDomain, dom, true); + delete persistentList[currentDomain]; + listRemove(persistentList, "_all", currentDomain, false); + // necessary if persistentList.currentDomain exists + io.write(config.whiteList, "w", JSON.stringify(persistentList)); + delete tmpList[currentDomain]; + listRemove(tmpList, "_all", currentDomain, false); } - }); - labels.push({ - left : "[" + dom + "] allow temporarily", + }); + } + if (allAllowed) + { + labels.unshift({ + left : "Don't allow all requests on " + domain, action : function() { - listAdd(tmpList, currentDomain, dom, false); + listRemove(tmpList, "_all", currentDomain, false); + listRemove(persistentList, "_all", currentDomain, true); } - }); - } - - else { - labels.push({ - left : "[" + dom + "] block", + }); + } + else + { + labels.unshift({ + left : "Temporarily allow all requests on " + domain, action : function() { - listRemove(persistentList, currentDomain, dom, true); - listRemove(tmpList, currentDomain, dom, false); + listAdd(tmpList, "_all", currentDomain, false); + } + }); + labels.unshift({ + left : "Allow all requests on " + domain, + action : function() { + listAdd(persistentList, "_all", currentDomain, true); } - }); - } - } - isWhiteListed = isWhiteListed || whiteListed || tmpWhiteListed; - if (!persistentList._always || persistentList._always.fastIndexOf(dom) == -1) { - labels.push({ - left : "[" + dom + "] allow on all sites", - action : function() { - listAdd(persistentList, "_always", dom, true); - } - }); - } - else { - labels.push({ - left : "[" + dom + "] don't allow on all sites", - action : function() { - listRemove(persistentList, "_always", dom, true); - } - }); - } - labels.push({ - left : "[" + dom + "] block on all sites", - action : function () { - listAdd(persistentList, "_alwaysBlock", dom, true); - } }); - })(domains[i]); - } - var allAllowed = (persistentList._all && persistentList._all.fastIndexOf(domain) != -1) || - (tmpList._all && tmpList._all.fastIndexOf(domain) != -1); - if (isWhiteListed || allAllowed) { - labels.unshift({ - left : "Block all requests on " + domain, - action : function () { - delete persistentList[currentDomain]; - listRemove(persistentList, "_all", currentDomain, false); - // necessary if persistentList.currentDomain exists - io.write(config.whiteList, "w", JSON.stringify(persistentList)); - delete tmpList[currentDomain]; - listRemove(tmpList, "_all", currentDomain, false); - } - }); - } - if (allAllowed) { - labels.unshift({ - left : "Don't allow all requests on " + domain, - action : function() { - listRemove(tmpList, "_all", currentDomain, false); - listRemove(persistentList, "_all", currentDomain, true); - } - }); - } - else { - labels.unshift({ - left : "Temporarily allow all requests on " + domain, - action : function() { - listAdd(tmpList, "_all", currentDomain, false); - } - }); - labels.unshift({ - left : "Allow all requests on " + domain, - action : function() { - listAdd(persistentList, "_all", currentDomain, true); - } - }); - } - - tabComplete("Requestpolicy:", labels, function (response) { - var i, l, len; - for (i=0, len = labels.length; i<len; ++i) { - l = labels[i]; - if (l.left == response) { - l.action(); - if (config.autoreload) { - tabs.current.reload(); - } - } } - }, true); + + tabComplete("Requestpolicy:", labels, function (response) { + var i, l, len; + for (i=0, len = labels.length; i<len; ++i) + { + l = labels[i]; + if (l.left == response) + { + l.action(); + if (config.autoreload) + tabs.current.reload(); + } + } + }, true); }//}}} -function unblockCurrent() { - if (!persistentList._alwaysBlock) { - io.notify("No domains to unblock"); - return; - } - var domains = getPrivate(tabs.current).domains; - //var domains = persistentList._alwaysBlock; - var labels = [], i, l; - for (i=0, l = domains.length; i<l; i++) { - if (persistentList._alwaysBlock.fastIndexOf(domains[i]) != -1) { - labels.push({ left : domains[i] }); + +function unblockCurrent() +{ + if (!persistentList._alwaysBlock) + { + io.notify("No domains to unblock"); + return; } - } - if (labels.length > 0) { - tabComplete("Unblock:", labels, function(response) { - listRemove(persistentList, "_alwaysBlock", response, true); - if (config.autoreload) - tabs.current.reload(); - }, true); - } - else { - io.notify("No domains to unblock"); - } + var domains = getPrivate(tabs.current).domains; + //var domains = persistentList._alwaysBlock; + var labels = [], i, l; + for (i=0, l = domains.length; i<l; i++) + { + if (persistentList._alwaysBlock.fastIndexOf(domains[i]) != -1) + labels.push({ left : domains[i] }); + } + if (labels.length > 0) + { + tabComplete("Unblock:", labels, function(response) { + listRemove(persistentList, "_alwaysBlock", response, true); + if (config.autoreload) + tabs.current.reload(); + }, true); + } + else + io.notify("No domains to unblock"); } -function unblockAll() { - if (!persistentList._alwaysBlock) { - io.notify("No domains to unblock"); - return; - } - var i, l, labels = []; - var domains = persistentList._alwaysBlock; - for (i=0, l=domains.length; i<l; ++i) { - labels.push({ left : domains[i] }); - } - if (labels.length > 0) { - tabComplete("Unblock:", labels, function(response) { - listRemove(persistentList, "_alwaysBlock", response, true); - if (config.autoreload) - tabs.current.reload(); - }, true); - } + +function unblockAll() +{ + if (!persistentList._alwaysBlock) + { + io.notify("No domains to unblock"); + return; + } + var i, l, labels = []; + var domains = persistentList._alwaysBlock; + for (i=0, l=domains.length; i<l; ++i) + labels.push({ left : domains[i] }); + + if (labels.length > 0) + { + tabComplete("Unblock:", labels, function(response) { + listRemove(persistentList, "_alwaysBlock", response, true); + if (config.autoreload) + tabs.current.reload(); + }, true); + } } -function blockRequest(wv, request, priv, domain) { - request.uri = "about:blank"; - priv.blocked++; - if (config.notify && wv == tabs.current) - io.notify("RP: blocked " + domain); - return true; + +function blockRequest(wv, request, priv, domain) +{ + request.uri = "about:blank"; + priv.blocked++; + if (config.notify && wv == tabs.current) + io.notify("RP: blocked " + domain); + return true; } // SIGNALS {{{ -var resourceCB = (function () { - var regexEmpty = /^\s*$/; - return function resourceCB(wv, frame, request, response) { - var o, message, domain, firstParty; - if (regexEmpty.test(request.uri)) - return false; - - message = request.message; - if (!message) - return false; - - firstParty = util.domainFromHost(message.firstParty.host); - domain = util.domainFromHost(message.uri.host); - if (firstParty == domain) - return false; - - o = getPrivate(wv); - if (o.domains.fastIndexOf(domain) == -1) - o.domains.push(domain); - - // Check for requests that are always blocked - if (persistentList._alwaysBlock && persistentList._alwaysBlock.fastIndexOf(domain) != -1) - return blockRequest(wv, request, o, domain); - - // Check if domain is always allowed - if ((persistentList._all && persistentList._all.fastIndexOf(firstParty) != -1) || - (tmpList._all && tmpList._all.fastIndexOf(firstParty) != -1)) - return false; - - // Check request is always allowed - if (persistentList._always && persistentList._always.fastIndexOf(domain) != -1) - return false; - - // Check if request is whitelisted - if ( (!persistentList[firstParty] || persistentList[firstParty].fastIndexOf(domain) == -1) && - (!tmpList[firstParty] || tmpList[firstParty].fastIndexOf(domain) == -1)) - return blockRequest(wv, request, o, domain); - }; +var resourceCB = (function () +{ + var regexEmpty = /^\s*$/; + return function resourceCB(wv, frame, request, response) + { + var o, message, domain, firstParty; + if (regexEmpty.test(request.uri)) + return false; + + message = request.message; + if (!message) + return false; + + firstParty = util.domainFromHost(message.firstParty.host); + domain = util.domainFromHost(message.uri.host); + if (firstParty == domain) + return false; + + o = getPrivate(wv); + if (o.domains.fastIndexOf(domain) == -1) + o.domains.push(domain); + + // Check for requests that are always blocked + if (persistentList._alwaysBlock && persistentList._alwaysBlock.fastIndexOf(domain) != -1) + return blockRequest(wv, request, o, domain); + + // Check if domain is always allowed + if ((persistentList._all && persistentList._all.fastIndexOf(firstParty) != -1) || + (tmpList._all && tmpList._all.fastIndexOf(firstParty) != -1)) + return false; + + // Check request is always allowed + if (persistentList._always && persistentList._always.fastIndexOf(domain) != -1) + return false; + + // Check if request is whitelisted + if ( (!persistentList[firstParty] || persistentList[firstParty].fastIndexOf(domain) == -1) && + (!tmpList[firstParty] || tmpList[firstParty].fastIndexOf(domain) == -1)) + return blockRequest(wv, request, o, domain); + }; })(); -function navigationCB(wv, frame) { - if (frame == wv.mainFrame) { - var o = getPrivate(wv); - o.domains = []; - o.blocked = 0; - } -} -function loadFinishedCB(wv) { - if (wv != tabs.current) - return; +function navigationCB(wv, frame) +{ + if (frame == wv.mainFrame) + { + var o = getPrivate(wv); + o.domains = []; + o.blocked = 0; + } + } - var blocked = getPrivate(wv).blocked; - if (blocked > 0) - io.notify("RP: blocked " + blocked + " requests"); +function loadFinishedCB(wv) +{ + if (wv != tabs.current) + return; + + var blocked = getPrivate(wv).blocked; + if (blocked > 0) + io.notify("RP: blocked " + blocked + " requests"); } -function connect() { - sigs.resource = signals.connect("resource", resourceCB); - sigs.navigation = signals.connect("navigation", navigationCB); - if (config.notify) - sigs.loadFinished = signals.connect("loadFinished", loadFinishedCB); +function connect() +{ + sigs.resource = signals.connect("resource", resourceCB); + sigs.navigation = signals.connect("navigation", navigationCB); + if (config.notify) + sigs.loadFinished = signals.connect("loadFinished", loadFinishedCB); } -function disconnect() { - sigs.forEach(function (key, value) { - if (value != -1) { - signals.disconnect(value); - sigs[key] = -1; - } - }); +function disconnect() +{ + sigs.forEach(function (key, value) { + if (value != -1) + { + signals.disconnect(value); + sigs[key] = -1; + } + }); }//}}} return { - init : function(c) { - config = extensions.getConfig(c, defaultConfig); - if (system.fileTest(config.whiteList, FileTest.regular | FileTest.symlink)) { - var rawWhiteList = io.read(config.whiteList); - try { - persistentList = JSON.parse(rawWhiteList); - } - catch (e) { - extensions.debug(me, e, "Error parsing persistentList"); - } + defaultConfig : defaultConfig, + init : function(c) + { + config = c; + if (system.fileTest(config.whiteList, FileTest.regular | FileTest.symlink)) + { + var rawWhiteList = io.read(config.whiteList); + try + { + persistentList = JSON.parse(rawWhiteList); + } + catch (e) + { + extensions.debug(me, e, "Error parsing persistentList"); + } + } + persistentList = persistentList || {}; + connect(); + bind(config.shortcut, showMenu, "requestpolicy"); + bind(config.unblockCurrent, unblockCurrent, "requestpolicyUnblockCurrent"); + bind(config.unblockAll, unblockAll, "requestpolicyUnblockAll"); + return true; + }, + end : function () + { + disconnect(); + unbind("requestpolicy"); + unbind("requestpolicyUnblockCurrent"); + unbind("requestpolicyUnblockAll"); } - persistentList = persistentList || {}; - connect(); - bind(config.shortcut, showMenu, "requestpolicy"); - bind(config.unblockCurrent, unblockCurrent, "requestpolicyUnblockCurrent"); - bind(config.unblockAll, unblockAll, "requestpolicyUnblockAll"); - return true; - }, - end : function () { - disconnect(); - unbind("requestpolicy"); - unbind("requestpolicyUnblockCurrent"); - unbind("requestpolicyUnblockAll"); - } -} +}; // vim: set ft=javascript: |