1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2025-01-18 09:28:52 +01:00

Reduced direct dependency on browser.xxx APIs

This commit is contained in:
kkapsner 2019-03-14 16:51:20 +01:00
parent a9edf45aa8
commit 137c1688ba
7 changed files with 53 additions and 14 deletions

View File

@ -167,7 +167,7 @@
let text = document.createElement("div"); let text = document.createElement("div");
text.style.margin = "0.5em auto"; text.style.margin = "0.5em auto";
text.textContent = browser.i18n.getMessage("showCanvasWhileAsking_message"); text.textContent = _("showCanvasWhileAsking_message");
imgContainer.appendChild(text); imgContainer.appendChild(text);
let img = document.createElement("img"); let img = document.createElement("img");

View File

@ -13,16 +13,17 @@
} }
const settings = require("./settings"); const settings = require("./settings");
const extension = require("./extension");
// Translation // Translation
var _ = function(name, replace, translateAPI){ var _ = function(name, replace, translateAPI){
if (!translateAPI){ if (!translateAPI){
translateAPI = browser.i18n.getMessage; translateAPI = extension.getTranslation;
} }
var str = translateAPI(name) || name; var str = translateAPI(name) || name;
if (replace){ if (replace){
// replace generic content in the transation by given parameter // replace generic content in the translation by given parameter
Object.keys(replace).forEach(function(name){ Object.keys(replace).forEach(function(name){
str = str.replace(new RegExp("{" + name + "}", "g"), replace[name]); str = str.replace(new RegExp("{" + name + "}", "g"), replace[name]);
}); });
@ -58,7 +59,7 @@
} }
// parse calling stack // parse calling stack
const extensionID = browser.extension.getURL(""); const extensionID = extension.extensionID;
function parseErrorStack(errorStack){ function parseErrorStack(errorStack){
var callers = errorStack.trim().split("\n"); var callers = errorStack.trim().split("\n");
callers = callers.map(parseStackEntry).filter(function(caller){ callers = callers.map(parseStackEntry).filter(function(caller){

34
lib/extension.js Normal file
View File

@ -0,0 +1,34 @@
/* 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/. */
(function(){
"use strict";
var scope;
if ((typeof exports) !== "undefined"){
scope = exports;
}
else {
scope = require.register("./extension", {});
}
scope.getTranslation = function getTranslation(id){
return browser.i18n.getMessage(id);
};
scope.extensionID = browser.extension.getURL("");
scope.inIncognitoContext = browser.extension.inIncognitoContext;
scope.message = {
on: function(callback){
return browser.runtime.onMessage.addListener(callback);
},
send: function(data){
return browser.runtime.sendMessage(data);
}
};
Object.seal(scope.message);
Object.seal(scope);
}());

View File

@ -10,6 +10,7 @@
const lists = require("./lists.js"); const lists = require("./lists.js");
const {check: originalCheck, checkStack: originalCheckStack} = require("./check.js"); const {check: originalCheck, checkStack: originalCheckStack} = require("./check.js");
const getWrapped = require("sdk/getWrapped"); const getWrapped = require("sdk/getWrapped");
const extension = require("./extension");
const logging = require("./logging"); const logging = require("./logging");
const {error, warning, message, notice, verbose, setPrefix: setLogPrefix} = logging; const {error, warning, message, notice, verbose, setPrefix: setLogPrefix} = logging;
@ -38,7 +39,7 @@
} }
function askWrapper(data){ function askWrapper(data){
return ask(data, { return ask(data, {
_: browser.i18n.getMessage, _: extension.getTranslation,
prefs prefs
}); });
} }
@ -56,7 +57,7 @@
notice("my tab id is", data.tabId); notice("my tab id is", data.tabId);
tabId = data.tabId; tabId = data.tabId;
} }
const persistentRndName = "persistent" + (browser.extension.inIncognitoContext? "Incognito": "") + "Rnd"; const persistentRndName = "persistent" + (extension.inIncognitoContext? "Incognito": "") + "Rnd";
if (data.hasOwnProperty(persistentRndName)){ if (data.hasOwnProperty(persistentRndName)){
const persistentRndValue = data[persistentRndName]; const persistentRndValue = data[persistentRndName];
notice("got persistent random data", persistentRndValue); notice("got persistent random data", persistentRndValue);
@ -151,7 +152,7 @@
} }
message("register listener for messages from background script"); message("register listener for messages from background script");
browser.runtime.onMessage.addListener(function(data){ extension.message.on(function(data){
if (data["canvasBlocker-unload"]){ if (data["canvasBlocker-unload"]){
enabled = false; enabled = false;
} }
@ -160,7 +161,7 @@
data["canvasBlocker-sendNotifications"] === tabId data["canvasBlocker-sendNotifications"] === tabId
){ ){
notice("sending notifications:", notifications); notice("sending notifications:", notifications);
browser.runtime.sendMessage({ extension.message.send({
sender: tabId, sender: tabId,
url: window.location.href, url: window.location.href,
"canvasBlocker-notificationCounter": notificationCounter, "canvasBlocker-notificationCounter": notificationCounter,

View File

@ -17,6 +17,7 @@
const getWrapped = require("sdk/getWrapped"); const getWrapped = require("sdk/getWrapped");
const logging = require("./logging"); const logging = require("./logging");
const settings = require("./settings"); const settings = require("./settings");
const extension = require("./extension");
setRandomSupply(randomSupplies.nonPersistent); setRandomSupply(randomSupplies.nonPersistent);
var apiNames = Object.keys(changedFunctions); var apiNames = Object.keys(changedFunctions);
@ -176,7 +177,7 @@
} }
}; };
let extensionID = browser.extension.getURL(""); let extensionID = extension.extensionID;
scope.intercept = function intercept({subject: window}, {check, checkStack, ask, notify, prefs}){ scope.intercept = function intercept({subject: window}, {check, checkStack, ask, notify, prefs}){
function getDataURL(object, prefs){ function getDataURL(object, prefs){
return ( return (

View File

@ -63,6 +63,7 @@
}; };
const settings = require("./settings"); const settings = require("./settings");
const extension = require("./extension");
function getDomain(window){ function getDomain(window){
if (settings.sharePersistentRndBetweenDomains){ if (settings.sharePersistentRndBetweenDomains){
@ -83,7 +84,7 @@
settings.onloaded(function(){ settings.onloaded(function(){
try { try {
let storedData = JSON.parse( let storedData = JSON.parse(
browser.extension.inIncognitoContext? extension.inIncognitoContext?
settings.persistentIncognitoRndStorage: settings.persistentIncognitoRndStorage:
settings.persistentRndStorage settings.persistentRndStorage
); );
@ -106,10 +107,10 @@
}); });
const getPersistentRnd = (function(){ const getPersistentRnd = (function(){
browser.runtime.onMessage.addListener(function(data){ extension.message.on(function(data){
if (data["canvasBlocker-set-domain-rnd"]){ if (data["canvasBlocker-set-domain-rnd"]){
var {domain, incognito, rnd} = data["canvasBlocker-set-domain-rnd"]; var {domain, incognito, rnd} = data["canvasBlocker-set-domain-rnd"];
if (incognito === browser.extension.inIncognitoContext){ if (incognito === extension.inIncognitoContext){
persistentRnd[domain] = new Uint8Array(rnd); persistentRnd[domain] = new Uint8Array(rnd);
} }
} }
@ -124,10 +125,10 @@
// create the (sub-)domains random numbers if not existing // create the (sub-)domains random numbers if not existing
persistentRnd[domain] = new Uint8Array(128); persistentRnd[domain] = new Uint8Array(128);
window.crypto.getRandomValues(persistentRnd[domain]); window.crypto.getRandomValues(persistentRnd[domain]);
browser.runtime.sendMessage({ extension.message.send({
"canvasBlocker-new-domain-rnd": { "canvasBlocker-new-domain-rnd": {
domain, domain,
incognito: browser.extension.inIncognitoContext, incognito: extension.inIncognitoContext,
rnd: Array.from(persistentRnd[domain]) rnd: Array.from(persistentRnd[domain])
} }
}); });

View File

@ -30,6 +30,7 @@
"run_at": "document_start", "run_at": "document_start",
"js": [ "js": [
"lib/require.js", "lib/require.js",
"lib/extension.js",
"lib/logging.js", "lib/logging.js",
"lib/settingDefinitions.js", "lib/settingDefinitions.js",