1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-12-22 12:50:36 +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");
text.style.margin = "0.5em auto";
text.textContent = browser.i18n.getMessage("showCanvasWhileAsking_message");
text.textContent = _("showCanvasWhileAsking_message");
imgContainer.appendChild(text);
let img = document.createElement("img");

View File

@ -13,16 +13,17 @@
}
const settings = require("./settings");
const extension = require("./extension");
// Translation
var _ = function(name, replace, translateAPI){
if (!translateAPI){
translateAPI = browser.i18n.getMessage;
translateAPI = extension.getTranslation;
}
var str = translateAPI(name) || name;
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){
str = str.replace(new RegExp("{" + name + "}", "g"), replace[name]);
});
@ -58,7 +59,7 @@
}
// parse calling stack
const extensionID = browser.extension.getURL("");
const extensionID = extension.extensionID;
function parseErrorStack(errorStack){
var callers = errorStack.trim().split("\n");
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 {check: originalCheck, checkStack: originalCheckStack} = require("./check.js");
const getWrapped = require("sdk/getWrapped");
const extension = require("./extension");
const logging = require("./logging");
const {error, warning, message, notice, verbose, setPrefix: setLogPrefix} = logging;
@ -38,7 +39,7 @@
}
function askWrapper(data){
return ask(data, {
_: browser.i18n.getMessage,
_: extension.getTranslation,
prefs
});
}
@ -56,7 +57,7 @@
notice("my tab id is", data.tabId);
tabId = data.tabId;
}
const persistentRndName = "persistent" + (browser.extension.inIncognitoContext? "Incognito": "") + "Rnd";
const persistentRndName = "persistent" + (extension.inIncognitoContext? "Incognito": "") + "Rnd";
if (data.hasOwnProperty(persistentRndName)){
const persistentRndValue = data[persistentRndName];
notice("got persistent random data", persistentRndValue);
@ -151,7 +152,7 @@
}
message("register listener for messages from background script");
browser.runtime.onMessage.addListener(function(data){
extension.message.on(function(data){
if (data["canvasBlocker-unload"]){
enabled = false;
}
@ -160,7 +161,7 @@
data["canvasBlocker-sendNotifications"] === tabId
){
notice("sending notifications:", notifications);
browser.runtime.sendMessage({
extension.message.send({
sender: tabId,
url: window.location.href,
"canvasBlocker-notificationCounter": notificationCounter,

View File

@ -17,6 +17,7 @@
const getWrapped = require("sdk/getWrapped");
const logging = require("./logging");
const settings = require("./settings");
const extension = require("./extension");
setRandomSupply(randomSupplies.nonPersistent);
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}){
function getDataURL(object, prefs){
return (

View File

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

View File

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