mirror of
https://github.com/kkapsner/CanvasBlocker
synced 2025-07-04 20:46:39 +02:00
Big linting
This commit is contained in:
parent
b5e6d049ce
commit
aef6bd3d59
58 changed files with 2074 additions and 1856 deletions
160
lib/main.js
160
lib/main.js
|
@ -6,25 +6,81 @@
|
|||
|
||||
const settings = require("./settings");
|
||||
const logging = require("./logging");
|
||||
const {error, warning, message, notice, verbose, } = logging;
|
||||
logging.setPrefix("main script");
|
||||
const persistentRndStorage = require("./persistentRndStorage");
|
||||
const notification = require("./notification");
|
||||
|
||||
message("start of background script");
|
||||
message("waiting for settings to be loaded");
|
||||
const registerSettingsContentScript = (function(){
|
||||
let unregisterSettingsContentScript = function(){};
|
||||
let lastRegistering;
|
||||
return function registerSettingsContentScript(){
|
||||
logging.message("Register content script for the settings.");
|
||||
logging.verbose("Unregister old content script, if present.");
|
||||
unregisterSettingsContentScript();
|
||||
const data = {};
|
||||
settings.forEach(function(def){
|
||||
data[def.name] = def.get();
|
||||
});
|
||||
lastRegistering = data;
|
||||
browser.contentScripts.register({
|
||||
matches: ["<all_urls>"],
|
||||
matchAboutBlank: true,
|
||||
allFrames: true,
|
||||
runAt: "document_start",
|
||||
js: [{
|
||||
code: `(function(settingsData){
|
||||
if (typeof require !== "undefined"){
|
||||
const settings = require("./settings");
|
||||
const logging = require("./logging");
|
||||
if (settings.init(settingsData)){
|
||||
logging.message("Initialized settings by dynamic content script.");
|
||||
}
|
||||
else {
|
||||
logging.warning("Dynamic content script was too late to provide settings.");
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!window.scope){
|
||||
window.scope = {};
|
||||
}
|
||||
window.scope.settingsData = settingsData;
|
||||
console.warn(
|
||||
"[CanvasBlocker] invalid content script order: require not defined at",
|
||||
window.location.href
|
||||
);
|
||||
}
|
||||
}(${JSON.stringify(data)}))`
|
||||
}]
|
||||
}).then(function(api){
|
||||
logging.verbose("Content script registered.");
|
||||
if (data !== lastRegistering){
|
||||
logging.verbose("Multiple content scripts registered at once. Remove unnecessary one.");
|
||||
api.unregister();
|
||||
}
|
||||
else {
|
||||
unregisterSettingsContentScript = api.unregister;
|
||||
}
|
||||
return;
|
||||
}).catch(function(error){
|
||||
logging.warning("Unable to register content script:", error);
|
||||
});
|
||||
};
|
||||
}());
|
||||
|
||||
logging.message("start of background script");
|
||||
logging.message("waiting for settings to be loaded");
|
||||
settings.onloaded(function(){
|
||||
notice("everything loaded");
|
||||
logging.notice("everything loaded");
|
||||
|
||||
message("perform startup reset");
|
||||
logging.message("perform startup reset");
|
||||
settings.startupReset();
|
||||
|
||||
persistentRndStorage.init();
|
||||
|
||||
message("register non port message listener");
|
||||
logging.message("register non port message listener");
|
||||
browser.runtime.onMessage.addListener(function(data){
|
||||
notice("got data without port", data);
|
||||
var keys = Object.keys(data);
|
||||
logging.notice("got data without port", data);
|
||||
const keys = Object.keys(data);
|
||||
if (data["canvasBlocker-new-domain-rnd"]){
|
||||
persistentRndStorage.setDomainData(
|
||||
data["canvasBlocker-new-domain-rnd"].domain,
|
||||
|
@ -47,31 +103,34 @@
|
|||
return;
|
||||
}
|
||||
}
|
||||
notice("pass the message to the tabs");
|
||||
logging.notice("pass the message to the tabs");
|
||||
browser.tabs.query({}).then(function(tabs){
|
||||
tabs.forEach(function(tab){
|
||||
browser.tabs.sendMessage(tab.id, data);
|
||||
});
|
||||
return;
|
||||
}).catch(function(error){
|
||||
logging.warning("Unable to get browser tabs:", error);
|
||||
});
|
||||
});
|
||||
|
||||
message("register port listener");
|
||||
logging.message("register port listener");
|
||||
browser.runtime.onConnect.addListener(function(port){
|
||||
notice("got port", port);
|
||||
logging.notice("got port", port);
|
||||
if (!port.sender.tab){
|
||||
notice("got port without tab = Firefox bug:", port);
|
||||
logging.notice("got port without tab = Firefox bug:", port);
|
||||
return;
|
||||
}
|
||||
verbose("send back the tab id", port.sender.tab.id);
|
||||
verbose("send back the tab cookie store id", port.sender.tab.cookieStoreId);
|
||||
verbose("send back the persistent random seeds", persistentRndStorage.persistentRnd);
|
||||
logging.verbose("send back the tab id", port.sender.tab.id);
|
||||
logging.verbose("send back the tab cookie store id", port.sender.tab.cookieStoreId);
|
||||
logging.verbose("send back the persistent random seeds", persistentRndStorage.persistentRnd);
|
||||
port.postMessage({
|
||||
tabId: port.sender.tab.id,
|
||||
cookieStoreId: port.sender.tab.cookieStoreId,
|
||||
persistentRnd: persistentRndStorage.persistentRnd,
|
||||
persistentIncognitoRnd: persistentRndStorage.persistentIncognitoRnd
|
||||
});
|
||||
var url = new URL(port.sender.url);
|
||||
const url = new URL(port.sender.url);
|
||||
port.onMessage.addListener(function(data){
|
||||
if (data.hasOwnProperty("canvasBlocker-notify")){
|
||||
notification.show(port.sender.tab.id, url, data["canvasBlocker-notify"].api);
|
||||
|
@ -79,76 +138,25 @@
|
|||
if (data.hasOwnProperty("canvasBlocker-clear-page-action")){
|
||||
notification.hide(port.sender.tab.id, url);
|
||||
}
|
||||
verbose("got data", data, "from port", port);
|
||||
logging.verbose("got data", data, "from port", port);
|
||||
});
|
||||
});
|
||||
|
||||
message("register storage change event listener");
|
||||
logging.message("register storage change event listener");
|
||||
|
||||
if (browser.contentScripts){
|
||||
let unregister = function(){};
|
||||
let lastRegistering;
|
||||
const register = function register(){
|
||||
logging.message("Register content script for the settings.");
|
||||
logging.verbose("Unregister old content script, if present.");
|
||||
unregister();
|
||||
var data = {};
|
||||
settings.forEach(function(def){
|
||||
data[def.name] = def.get();
|
||||
});
|
||||
lastRegistering = data;
|
||||
browser.contentScripts.register({
|
||||
matches: ["<all_urls>"],
|
||||
matchAboutBlank: true,
|
||||
allFrames: true,
|
||||
runAt: "document_start",
|
||||
js: [{
|
||||
code: `(function(settingsData){
|
||||
if (typeof require !== "undefined"){
|
||||
const settings = require("./settings");
|
||||
const logging = require("./logging");
|
||||
if (settings.init(settingsData)){
|
||||
logging.message("Initialized settings by dynamic content script.");
|
||||
}
|
||||
else {
|
||||
logging.warning("Dynamic content script was too late to provide settings.");
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!window.scope){
|
||||
window.scope = {};
|
||||
}
|
||||
window.scope.settingsData = settingsData;
|
||||
console.warn(
|
||||
"[CanvasBlocker] invalid content script order: require not defined at",
|
||||
window.location.href
|
||||
);
|
||||
}
|
||||
}(${JSON.stringify(data)}))`
|
||||
}]
|
||||
}).then(function(api){
|
||||
logging.verbose("Content script registered.");
|
||||
if (data !== lastRegistering){
|
||||
logging.verbose("Multiple content scripts registered at once. Remove unnecessary one.");
|
||||
api.unregister();
|
||||
}
|
||||
else {
|
||||
unregister = api.unregister;
|
||||
}
|
||||
});
|
||||
};
|
||||
register();
|
||||
settings.on("any", register);
|
||||
registerSettingsContentScript();
|
||||
settings.on("any", registerSettingsContentScript);
|
||||
}
|
||||
else {
|
||||
logging.error("Old Firefox does not support browser.contentScript.register()");
|
||||
}
|
||||
});
|
||||
|
||||
message("Initialize data-URL workaround.");
|
||||
logging.message("Initialize data-URL workaround.");
|
||||
require("./dataUrls").init();
|
||||
|
||||
message("Initialize navigator HTTP header protection.");
|
||||
logging.message("Initialize navigator HTTP header protection.");
|
||||
require("./navigator").init();
|
||||
|
||||
browser.runtime.onInstalled.addListener(function(details){
|
||||
|
@ -165,7 +173,7 @@
|
|||
}
|
||||
switch (details.reason){
|
||||
case "install":
|
||||
message("CanvasBlocker installed");
|
||||
logging.message("CanvasBlocker installed");
|
||||
openOptions(details.reason);
|
||||
browser.tabs.create({
|
||||
url: browser.extension.getURL("options/presets.html?notice=" + details.reason)
|
||||
|
@ -174,12 +182,12 @@
|
|||
case "update":
|
||||
settings.onloaded(function(){
|
||||
if (!settings.dontShowOptionsOnUpdate){
|
||||
message("CanvasBlocker updated");
|
||||
logging.message("CanvasBlocker updated");
|
||||
openOptions(details.reason);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
message("end");
|
||||
logging.message("end");
|
||||
}());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue