2015-01-16 13:01:01 +01:00
|
|
|
/* 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/. */
|
2014-08-20 10:21:38 +02:00
|
|
|
(function(){
|
2014-10-14 01:06:11 +02:00
|
|
|
"use strict";
|
2017-06-25 22:33:12 +02:00
|
|
|
|
2017-11-07 00:36:44 +01:00
|
|
|
const settings = require("./settings");
|
2017-07-27 19:14:04 +02:00
|
|
|
const logging = require("./logging");
|
|
|
|
logging.setPrefix("main script");
|
2017-11-08 17:46:41 +01:00
|
|
|
const persistentRndStorage = require("./persistentRndStorage");
|
2018-08-20 21:48:05 +02:00
|
|
|
const notification = require("./notification");
|
2019-12-02 19:16:32 +01:00
|
|
|
const mobile = require("./mobile");
|
2021-11-01 13:18:38 +01:00
|
|
|
const extension = require("./extension");
|
2017-07-02 12:21:16 +02:00
|
|
|
|
2019-11-28 01:26:35 +01:00
|
|
|
const registerSettingsContentScript = (function(){
|
|
|
|
let unregisterSettingsContentScript = function(){};
|
|
|
|
let lastRegistering;
|
2019-12-28 23:23:55 +01:00
|
|
|
return async function registerSettingsContentScript(){
|
2019-11-28 01:26:35 +01:00
|
|
|
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;
|
2019-12-28 23:23:55 +01:00
|
|
|
const api = await browser.contentScripts.register({
|
2019-11-28 01:26:35 +01:00
|
|
|
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)}))`
|
|
|
|
}]
|
|
|
|
});
|
2019-12-28 23:23:55 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2019-11-28 01:26:35 +01:00
|
|
|
};
|
|
|
|
}());
|
|
|
|
|
|
|
|
logging.message("start of background script");
|
|
|
|
logging.message("waiting for settings to be loaded");
|
2017-11-07 00:36:44 +01:00
|
|
|
settings.onloaded(function(){
|
2019-11-28 01:26:35 +01:00
|
|
|
logging.notice("everything loaded");
|
2017-07-07 08:50:23 +02:00
|
|
|
|
2019-11-28 01:26:35 +01:00
|
|
|
logging.message("perform startup reset");
|
2018-07-17 13:07:50 +02:00
|
|
|
settings.startupReset();
|
|
|
|
|
2017-11-08 17:46:41 +01:00
|
|
|
persistentRndStorage.init();
|
2017-07-07 08:50:23 +02:00
|
|
|
|
2019-11-28 01:26:35 +01:00
|
|
|
logging.message("register non port message listener");
|
2019-12-28 23:23:55 +01:00
|
|
|
browser.runtime.onMessage.addListener(async function(data){
|
2019-11-28 01:26:35 +01:00
|
|
|
logging.notice("got data without port", data);
|
|
|
|
const keys = Object.keys(data);
|
2017-07-07 08:50:23 +02:00
|
|
|
if (data["canvasBlocker-new-domain-rnd"]){
|
2017-11-08 17:46:41 +01:00
|
|
|
persistentRndStorage.setDomainData(
|
|
|
|
data["canvasBlocker-new-domain-rnd"].domain,
|
2018-07-28 14:13:14 +02:00
|
|
|
data["canvasBlocker-new-domain-rnd"].incognito,
|
2017-11-08 17:46:41 +01:00
|
|
|
data["canvasBlocker-new-domain-rnd"].rnd
|
|
|
|
);
|
|
|
|
if (keys.length === 1){
|
|
|
|
return;
|
2017-11-07 00:36:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (data["canvasBlocker-clear-domain-rnd"]){
|
2021-02-21 11:49:29 +01:00
|
|
|
persistentRndStorage.clear(data["canvasBlocker-clear-domain-rnd"] === "force");
|
2017-11-08 17:46:41 +01:00
|
|
|
if (keys.length === 1){
|
|
|
|
return;
|
2017-11-07 00:36:44 +01:00
|
|
|
}
|
2017-07-07 08:50:23 +02:00
|
|
|
}
|
2019-06-14 10:45:34 +02:00
|
|
|
if (data["canvasBlocker-clear-container-rnd"]){
|
|
|
|
persistentRndStorage.clearContainerData(data["canvasBlocker-clear-container-rnd"]);
|
|
|
|
if (keys.length === 1){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-11-28 01:26:35 +01:00
|
|
|
logging.notice("pass the message to the tabs");
|
2019-12-28 23:23:55 +01:00
|
|
|
const tabs = await browser.tabs.query({});
|
|
|
|
tabs.forEach(function(tab){
|
|
|
|
browser.tabs.sendMessage(tab.id, data);
|
2018-09-23 12:33:23 +02:00
|
|
|
});
|
2017-07-07 08:50:23 +02:00
|
|
|
});
|
|
|
|
|
2019-11-28 01:26:35 +01:00
|
|
|
logging.message("register port listener");
|
2017-07-07 08:50:23 +02:00
|
|
|
browser.runtime.onConnect.addListener(function(port){
|
2019-11-28 01:26:35 +01:00
|
|
|
logging.notice("got port", port);
|
2018-09-13 22:50:10 +02:00
|
|
|
if (!port.sender.tab){
|
2019-11-28 01:26:35 +01:00
|
|
|
logging.notice("got port without tab = Firefox bug:", port);
|
2018-09-13 22:50:10 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-11-28 01:26:35 +01:00
|
|
|
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);
|
2017-09-23 23:37:46 +02:00
|
|
|
port.postMessage({
|
|
|
|
tabId: port.sender.tab.id,
|
2020-11-02 11:54:43 +01:00
|
|
|
cookieStoreId: port.sender.tab.cookieStoreId || "",
|
2018-07-28 14:13:14 +02:00
|
|
|
persistentRnd: persistentRndStorage.persistentRnd,
|
|
|
|
persistentIncognitoRnd: persistentRndStorage.persistentIncognitoRnd
|
2017-09-23 23:37:46 +02:00
|
|
|
});
|
2019-11-28 01:26:35 +01:00
|
|
|
const url = new URL(port.sender.url);
|
2017-07-07 08:50:23 +02:00
|
|
|
port.onMessage.addListener(function(data){
|
2017-11-07 00:36:44 +01:00
|
|
|
if (data.hasOwnProperty("canvasBlocker-notify")){
|
2018-08-21 22:43:41 +02:00
|
|
|
notification.show(port.sender.tab.id, url, data["canvasBlocker-notify"].api);
|
2017-11-07 00:36:44 +01:00
|
|
|
}
|
2017-11-14 23:57:44 +01:00
|
|
|
if (data.hasOwnProperty("canvasBlocker-clear-page-action")){
|
2018-09-06 20:13:16 +02:00
|
|
|
notification.hide(port.sender.tab.id, url);
|
2017-11-14 23:57:44 +01:00
|
|
|
}
|
2019-11-28 01:26:35 +01:00
|
|
|
logging.verbose("got data", data, "from port", port);
|
2017-07-07 08:50:23 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-11-28 01:26:35 +01:00
|
|
|
logging.message("register storage change event listener");
|
2017-11-07 00:36:44 +01:00
|
|
|
|
2017-11-27 12:28:01 +01:00
|
|
|
if (browser.contentScripts){
|
2019-11-28 01:26:35 +01:00
|
|
|
registerSettingsContentScript();
|
|
|
|
settings.on("any", registerSettingsContentScript);
|
2017-11-27 12:28:01 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
logging.error("Old Firefox does not support browser.contentScript.register()");
|
|
|
|
}
|
2017-02-10 17:37:35 +01:00
|
|
|
});
|
2017-02-01 10:49:33 +01:00
|
|
|
|
2019-11-28 01:26:35 +01:00
|
|
|
logging.message("Initialize data-URL workaround.");
|
2018-07-16 00:14:44 +02:00
|
|
|
require("./dataUrls").init();
|
|
|
|
|
2019-11-28 01:26:35 +01:00
|
|
|
logging.message("Initialize navigator HTTP header protection.");
|
2019-02-27 23:49:00 +01:00
|
|
|
require("./navigator").init();
|
|
|
|
|
2018-01-08 17:07:46 +01:00
|
|
|
browser.runtime.onInstalled.addListener(function(details){
|
|
|
|
function openOptions(reason){
|
|
|
|
if (
|
|
|
|
!browser.pageAction ||
|
|
|
|
!browser.pageAction.show ||
|
|
|
|
!browser.pageAction.openPopup
|
|
|
|
){
|
|
|
|
browser.tabs.create({
|
2021-11-01 13:18:38 +01:00
|
|
|
url: extension.getURL("options/options.html?notice=" + reason)
|
2018-01-08 17:07:46 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch (details.reason){
|
|
|
|
case "install":
|
2019-11-28 01:26:35 +01:00
|
|
|
logging.message("CanvasBlocker installed");
|
2018-01-08 17:07:46 +01:00
|
|
|
openOptions(details.reason);
|
2023-05-29 14:05:03 +02:00
|
|
|
settings.onloaded(function(){
|
|
|
|
if (settings.showPresetsOnInstallation){
|
|
|
|
browser.tabs.create({
|
|
|
|
url: extension.getURL("options/presets.html?notice=" + details.reason)
|
|
|
|
});
|
|
|
|
}
|
2019-07-16 18:25:30 +02:00
|
|
|
});
|
2018-01-08 17:07:46 +01:00
|
|
|
break;
|
|
|
|
case "update":
|
2018-08-19 00:13:43 +02:00
|
|
|
settings.onloaded(function(){
|
|
|
|
if (!settings.dontShowOptionsOnUpdate){
|
2019-11-28 01:26:35 +01:00
|
|
|
logging.message("CanvasBlocker updated");
|
2018-08-19 00:13:43 +02:00
|
|
|
openOptions(details.reason);
|
|
|
|
}
|
|
|
|
});
|
2018-01-08 17:07:46 +01:00
|
|
|
}
|
2019-12-02 19:16:32 +01:00
|
|
|
|
|
|
|
// mobile default settings
|
2019-12-28 23:23:55 +01:00
|
|
|
mobile.ifMobile(async function(){
|
|
|
|
const settings = await browser.storage.local.get();
|
|
|
|
mobile.applyMobileDefaults(settings);
|
2019-12-02 19:16:32 +01:00
|
|
|
});
|
2017-07-16 00:12:12 +02:00
|
|
|
});
|
2020-01-23 15:36:32 +01:00
|
|
|
|
|
|
|
if (browser.runtime.onSuspend){
|
|
|
|
browser.runtime.onSuspend.addListener(async function(){
|
|
|
|
logging.message("Suspending CanvasBlocker");
|
|
|
|
(await browser.tabs.query({})).forEach(function(tab){
|
|
|
|
browser.tabs.sendMessage(tab.id, {
|
|
|
|
"canvasBlocker-unload": true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2021-01-26 13:47:11 +01:00
|
|
|
if (browser.runtime.onUpdateAvailable){
|
|
|
|
browser.runtime.onUpdateAvailable.addListener(async function(details){
|
|
|
|
logging.message("Update available", details);
|
|
|
|
if (settings.disruptSessionOnUpdate){
|
|
|
|
await Promise.all((await browser.tabs.query({})).map(async function(tab){
|
|
|
|
try{
|
|
|
|
await browser.tabs.sendMessage(tab.id, {
|
|
|
|
"canvasBlocker-unload": true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch(error){
|
|
|
|
logging.verbose("error while unloading", tab, ":", error);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
window.setTimeout(function(){
|
|
|
|
logging.verbose("Reload extension after one second");
|
|
|
|
browser.runtime.reload();
|
|
|
|
}, 1000);
|
|
|
|
}
|
2021-01-26 21:45:15 +01:00
|
|
|
else {
|
|
|
|
settings.updatePending = true;
|
|
|
|
}
|
2021-01-26 13:47:11 +01:00
|
|
|
});
|
|
|
|
}
|
2019-11-28 01:26:35 +01:00
|
|
|
logging.message("end");
|
2017-09-24 03:38:44 +02:00
|
|
|
}());
|