diff --git a/lib/askForPermission.js b/lib/askForPermission.js index fccd86d..a601373 100644 --- a/lib/askForPermission.js +++ b/lib/askForPermission.js @@ -4,6 +4,15 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ (function(){ "use strict"; + var scope; + if ((typeof exports) !== "undefined"){ + scope = exports; + } + else { + window.scope.askForPermission = {}; + scope = window.scope.askForPermission; + } + const {parseErrorStack} = require("./callingStack"); // Check canvas appearance @@ -107,7 +116,7 @@ } } - exports.ask = function({window, type, canvas, errorStack}, {_, prefs}){ + scope.ask = function({window, type, canvas, errorStack}, {_, prefs}){ var answer; var askMode = getAskMode(window, type, _); var askStatus = askMode.askStatus; diff --git a/lib/callingStack.js b/lib/callingStack.js index ef1c1b5..536bb99 100644 --- a/lib/callingStack.js +++ b/lib/callingStack.js @@ -3,101 +3,113 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -const preferences = require("sdk/simple-prefs"); -const prefs = preferences.prefs; - -// Translation -var translate = require("sdk/l10n").get; -var _ = function(name, replace, translateAPI){ - "use strict"; - if (!translateAPI){ - translateAPI = translate; - } - - var str = translateAPI(name) || name; - if (replace){ - // replace generic content in the transation by given parameter - Object.keys(replace).forEach(function(name){ - str = str.replace(new RegExp("{" + name + "}", "g"), replace[name]); - }); - } - return str; -}; - -// Stack parsing -function parseStackEntry(entry){ +(function(){ "use strict"; - var m = /@(.*):(\d*):(\d*)$/.exec(entry) || ["", entry, "--", "--"]; - return { - url: m[1], - line: parseInt(m[2], 10), - column: parseInt(m[3], 10), - raw: entry - }; -} - -function stackRuleMatch(stackEntry, stackRule){ - if (!stackEntry){ - return false; + var scope; + if ((typeof exports) !== "undefined"){ + scope = exports; } - if (stackEntry.url !== stackRule.url){ - return false; + else { + window.scope.callingStack = {}; + scope = window.scope.callingStack; } - if ((typeof stackRule.line) !== "undefined" && stackEntry.line !== stackRule.line){ - return false; - } - if ((typeof stackRule.column) !== "undefined" && stackEntry.column !== stackRule.column){ - return false; - } - return true; -} - -// parse calling stack -function parseErrorStack(errorStack){ - "use strict"; - var callers = errorStack.trim().split("\n"); - var findme = callers.shift(); // Remove us from the stack - findme = findme.replace(/(:[0-9]+){1,2}$/, ""); // rm line & column - // Eliminate squashed stack. stack may contain 2+ stacks, but why... - var inDoubleStack = false; - callers = callers.filter(function(caller){ - var doubleStackStart = caller.search(findme) !== -1; - inDoubleStack = inDoubleStack || doubleStackStart; - return !inDoubleStack; - }).map(parseStackEntry); - return { - toString: function(translateAPI){ - var msg = ""; - msg += "\n\n" + _("sourceOutput", undefined, translateAPI) + ": "; - if (prefs.showCompleteCallingStack){ - msg += callers.reduce(function(stack, c){ - return stack + "\n\t" + _("stackEntryOutput", c, translateAPI); - }, ""); - } - else{ - msg += _("stackEntryOutput", callers[0], translateAPI); - } - - return msg; - }, - match: function(stackRule){ - if (typeof stackRule.stackPosition !== "undefined"){ - var pos = stackRule.stackPosition; - if (pos < 0){ - pos += callers.length; - } - return stackRuleMatch(callers[pos], stackRule); - } - else { - return callers.some(function(stackEntry){ - return stackRuleMatch(stackEntry, stackRule); - }); - } + const preferences = require("sdk/simple-prefs"); + const prefs = preferences.prefs; + + // Translation + var translate = require("sdk/l10n").get; + var _ = function(name, replace, translateAPI){ + "use strict"; + if (!translateAPI){ + translateAPI = translate; } + + var str = translateAPI(name) || name; + if (replace){ + // replace generic content in the transation by given parameter + Object.keys(replace).forEach(function(name){ + str = str.replace(new RegExp("{" + name + "}", "g"), replace[name]); + }); + } + return str; }; -} + + // Stack parsing + function parseStackEntry(entry){ + "use strict"; + + var m = /@(.*):(\d*):(\d*)$/.exec(entry) || ["", entry, "--", "--"]; + return { + url: m[1], + line: parseInt(m[2], 10), + column: parseInt(m[3], 10), + raw: entry + }; + } + + function stackRuleMatch(stackEntry, stackRule){ + if (!stackEntry){ + return false; + } + if (stackEntry.url !== stackRule.url){ + return false; + } + if ((typeof stackRule.line) !== "undefined" && stackEntry.line !== stackRule.line){ + return false; + } + if ((typeof stackRule.column) !== "undefined" && stackEntry.column !== stackRule.column){ + return false; + } + return true; + } -exports.parseErrorStack = parseErrorStack; \ No newline at end of file + // parse calling stack + function parseErrorStack(errorStack){ + "use strict"; + + var callers = errorStack.trim().split("\n"); + var findme = callers.shift(); // Remove us from the stack + findme = findme.replace(/(:[0-9]+){1,2}$/, ""); // rm line & column + // Eliminate squashed stack. stack may contain 2+ stacks, but why... + var inDoubleStack = false; + callers = callers.filter(function(caller){ + var doubleStackStart = caller.search(findme) !== -1; + inDoubleStack = inDoubleStack || doubleStackStart; + return !inDoubleStack; + }).map(parseStackEntry); + return { + toString: function(translateAPI){ + var msg = ""; + msg += "\n\n" + _("sourceOutput", undefined, translateAPI) + ": "; + if (prefs.showCompleteCallingStack){ + msg += callers.reduce(function(stack, c){ + return stack + "\n\t" + _("stackEntryOutput", c, translateAPI); + }, ""); + } + else{ + msg += _("stackEntryOutput", callers[0], translateAPI); + } + + return msg; + }, + match: function(stackRule){ + if (typeof stackRule.stackPosition !== "undefined"){ + var pos = stackRule.stackPosition; + if (pos < 0){ + pos += callers.length; + } + return stackRuleMatch(callers[pos], stackRule); + } + else { + return callers.some(function(stackEntry){ + return stackRuleMatch(stackEntry, stackRule); + }); + } + } + }; + } + + scope.parseErrorStack = parseErrorStack; +}()); \ No newline at end of file diff --git a/lib/check.js b/lib/check.js index fe9e5d5..290eaff 100644 --- a/lib/check.js +++ b/lib/check.js @@ -6,13 +6,22 @@ (function(){ "use strict"; + var scope; + if ((typeof exports) !== "undefined"){ + scope = exports; + } + else { + window.scope.check = {}; + scope = window.scope.check; + } + const lists = require("./lists"); const preferences = require("sdk/simple-prefs"); const prefs = preferences.prefs; const {parseErrorStack} = require("./callingStack"); const {URL} = require("sdk/url"); - exports.check = function check({url, errorStack}){ + scope.check = function check({url, errorStack}){ var match = checkBoth(errorStack, url, prefs.blockMode).match(/^(block|allow|fake|ask)(|Readout|Everything|Context|Input|Internal)$/); if (match){ return { @@ -96,5 +105,5 @@ var callingStack = parseErrorStack(errorStack); return lists.get("stack").match(callingStack); } - exports.checkStack = checkStack; + scope.checkStack = checkStack; }()); \ No newline at end of file diff --git a/lib/defaultSettings.js b/lib/defaultSettings.js new file mode 100644 index 0000000..fdbde18 --- /dev/null +++ b/lib/defaultSettings.js @@ -0,0 +1,14 @@ +"use strict"; + +var settings = { whiteList: "", blackList: "", blockMode: "fakeReadout", maxFakeSize: 0, rng: "nonPersistent", persistentRndStorage: "", storePersistentRnd: false, askOnlyOnce: true, showNotifications: true, notificationDisplayTime: 30, ignoreList: "", showCallingFile: false, showCompleteCallingStack: false, enableStackList: false, stackList: "" +}; + +(function(){ + browser.storage.onChanged.addListener(function(change, area){ + if (area === "local"){ + Object.keys(change).forEach(function(key){ + settings[key] = change[key].newValue; + }); + } + }); +}()); \ No newline at end of file diff --git a/lib/intercept.js b/lib/intercept.js index fc91109..5ed2d06 100644 --- a/lib/intercept.js +++ b/lib/intercept.js @@ -5,6 +5,15 @@ (function(){ "use strict"; + var scope; + if ((typeof exports) !== "undefined"){ + scope = exports; + } + else { + window.scope.intercept = {}; + scope = window.scope.intercept; + } + const {changedFunctions, setRandomSupply} = require("./modifiedAPI"); const randomSupplies = require("./randomSupplies"); setRandomSupply(randomSupplies.nonPersistent); @@ -20,7 +29,7 @@ setRandomSupply(randomSupplies.nonPersistent); } } - exports.setRandomSupplyByType = setRandomSupplyByType; + scope.setRandomSupplyByType = setRandomSupplyByType; function getURL(window){ if (!window.location.href || window.location.href === "about:blank"){ @@ -34,7 +43,7 @@ return window.location.href; } - exports.intercept = function intercept({subject: window}, {check, checkStack, ask, notify, prefs}){ + scope.intercept = function intercept({subject: window}, {check, checkStack, ask, notify, prefs}){ var siteStatus = check({url: getURL(window)}); if (siteStatus.mode !== "allow"){ apiNames.forEach(function(name){ @@ -51,7 +60,7 @@ { enumerable: true, configureable: false, - get: function(){ + get: exportFunction(function() var url = getURL(window); if (!url){ return undef; @@ -90,7 +99,7 @@ else { return original; } - } + }, window) } ); } diff --git a/lib/require.js b/lib/require.js new file mode 100644 index 0000000..642832a --- /dev/null +++ b/lib/require.js @@ -0,0 +1,40 @@ +window.scope = {}; +function require(module){ + if (module.startsWith("./")){ + var scopeName = module.substr(2).replace(/\..+/, ""); + return window.scope[scopeName]; + } + else if (module === "chrome"){ + return { + Cu: {exportFunction} + }; + } + else if (module === "sdk/simple-prefs"){ + return { + prefs: settings, + on: function(key, callback){ + browser.storage.onChanged.addListener(function(changes, area){ + if (area === "local"){ + if (changes[key]){ + callback(); + } + } + }); + } + } + } + else if (module === "sdk/l10n"){ + return { + get: function(key){console.log(key); + return browser.i18n.getMessage(key); + } + } + } + else if (module === "sdk/url"){ + return { + URL + } + } + console.error("Not able to get non relative modules!", module); + return undefined; +} \ No newline at end of file