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");
|
|
|
|
const {error, warning, message, notice, verbose, } = logging;
|
2017-11-07 00:36:44 +01:00
|
|
|
const lists = require("./lists");
|
2017-07-27 19:14:04 +02:00
|
|
|
logging.setPrefix("main script");
|
2017-11-08 17:46:41 +01:00
|
|
|
const persistentRndStorage = require("./persistentRndStorage");
|
2017-07-02 12:21:16 +02:00
|
|
|
|
2017-11-07 00:36:44 +01:00
|
|
|
message("start of background script");
|
|
|
|
message("waiting for settings to be loaded");
|
|
|
|
settings.onloaded(function(){
|
2017-07-27 19:14:04 +02:00
|
|
|
notice("everything loaded");
|
2017-07-07 08:50:23 +02:00
|
|
|
|
2018-07-17 13:07:50 +02:00
|
|
|
message("perform startup reset");
|
|
|
|
settings.startupReset();
|
|
|
|
|
2017-11-08 17:46:41 +01:00
|
|
|
persistentRndStorage.init();
|
2017-07-07 08:50:23 +02:00
|
|
|
|
2017-07-27 19:14:04 +02:00
|
|
|
message("register non port message listener");
|
2017-07-07 08:50:23 +02:00
|
|
|
browser.runtime.onMessage.addListener(function(data){
|
2017-07-27 19:14:04 +02:00
|
|
|
notice("got data without port", data);
|
2017-11-08 17:46:41 +01:00
|
|
|
var 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,
|
|
|
|
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"]){
|
2017-11-08 17:46:41 +01:00
|
|
|
persistentRndStorage.clear();
|
|
|
|
if (keys.length === 1){
|
|
|
|
return;
|
2017-11-07 00:36:44 +01:00
|
|
|
}
|
2017-07-07 08:50:23 +02:00
|
|
|
}
|
2017-07-27 19:14:04 +02:00
|
|
|
notice("pass the message to the tabs");
|
2017-07-02 12:21:16 +02:00
|
|
|
browser.tabs.query({}).then(function(tabs){
|
|
|
|
tabs.forEach(function(tab){
|
2017-07-07 08:50:23 +02:00
|
|
|
browser.tabs.sendMessage(tab.id, data);
|
2017-07-02 12:21:16 +02:00
|
|
|
});
|
|
|
|
});
|
2017-07-07 08:50:23 +02:00
|
|
|
});
|
|
|
|
|
2017-07-27 19:14:04 +02:00
|
|
|
message("register port listener");
|
2017-07-07 08:50:23 +02:00
|
|
|
browser.runtime.onConnect.addListener(function(port){
|
2017-07-27 19:14:04 +02:00
|
|
|
notice("got port", port);
|
|
|
|
verbose("send back the tab id", port.sender.tab.id);
|
2017-11-08 17:46:41 +01:00
|
|
|
verbose("send back the persistent random seeds", persistentRndStorage.persistentRnd);
|
2017-09-23 23:37:46 +02:00
|
|
|
port.postMessage({
|
|
|
|
tabId: port.sender.tab.id,
|
2017-11-08 17:46:41 +01:00
|
|
|
persistentRnd: persistentRndStorage.persistentRnd
|
2017-09-23 23:37:46 +02:00
|
|
|
});
|
2017-07-07 08:50:23 +02:00
|
|
|
var url = new URL(port.sender.url);
|
|
|
|
port.onMessage.addListener(function(data){
|
2017-11-07 00:36:44 +01:00
|
|
|
if (data.hasOwnProperty("canvasBlocker-notify")){
|
2017-07-07 08:50:23 +02:00
|
|
|
if (
|
2017-12-03 23:47:49 +01:00
|
|
|
settings.get("showNotifications", url) &&
|
2017-07-07 08:50:23 +02:00
|
|
|
!lists.get("ignore").match(url)
|
|
|
|
){
|
|
|
|
browser.pageAction.show(port.sender.tab.id);
|
|
|
|
}
|
2017-11-07 00:36:44 +01:00
|
|
|
}
|
2017-11-14 23:57:44 +01:00
|
|
|
if (data.hasOwnProperty("canvasBlocker-clear-page-action")){
|
|
|
|
notice("Hide page action for tab", port.sender.tab.id);
|
|
|
|
browser.pageAction.hide(port.sender.tab.id);
|
|
|
|
}
|
2017-07-27 19:14:04 +02:00
|
|
|
verbose("got data", data, "from port", port);
|
2017-07-07 08:50:23 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-07-27 19:14:04 +02:00
|
|
|
message("register storage change event listener");
|
2017-11-07 00:36:44 +01:00
|
|
|
|
|
|
|
settings.on("showNotifications", function({newValue}){
|
|
|
|
if (!newValue){
|
|
|
|
message("notifications were disabled -> hide all page actions");
|
|
|
|
browser.tabs.query({}).then(function(tabs){
|
|
|
|
tabs.forEach(function(tab){
|
|
|
|
browser.pageAction.hide(tab.id);
|
2017-07-07 08:50:23 +02:00
|
|
|
});
|
2017-11-07 00:36:44 +01:00
|
|
|
});
|
2017-02-10 17:37:35 +01:00
|
|
|
}
|
2017-07-07 08:50:23 +02:00
|
|
|
});
|
2017-12-19 00:28:42 +01:00
|
|
|
|
2017-11-27 12:28:01 +01:00
|
|
|
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(){
|
2017-12-24 21:54:39 +01:00
|
|
|
if (typeof require !== "undefined"){
|
|
|
|
const settings = require("./settings");
|
|
|
|
const logging = require("./logging");
|
|
|
|
if (settings.init(${JSON.stringify(data)})){
|
|
|
|
logging.message("Initialized settings by dynamic content script.");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
logging.error("Dynamic content script was too late to provide settings.");
|
|
|
|
}
|
2017-12-18 23:19:28 +01:00
|
|
|
}
|
|
|
|
else {
|
2018-07-15 22:39:03 +02:00
|
|
|
console.error(
|
|
|
|
"[CanvasBlocker] invalid content scripts: require not defined at",
|
|
|
|
window.location.href
|
|
|
|
);
|
2017-12-18 23:19:28 +01:00
|
|
|
}
|
2017-11-27 12:28:01 +01:00
|
|
|
}())`
|
|
|
|
}]
|
|
|
|
}).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);
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2018-07-16 00:14:44 +02:00
|
|
|
message("Initialize data-URL workaround.");
|
|
|
|
require("./dataUrls").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({
|
|
|
|
url: browser.extension.getURL("options/options.html?" + reason)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch (details.reason){
|
|
|
|
case "install":
|
|
|
|
message("CanvasBlocker installed");
|
|
|
|
openOptions(details.reason);
|
|
|
|
break;
|
|
|
|
case "update":
|
|
|
|
message("CanvasBlocker updated");
|
|
|
|
openOptions(details.reason);
|
|
|
|
}
|
2017-07-16 00:12:12 +02:00
|
|
|
});
|
2017-07-07 08:50:23 +02:00
|
|
|
|
2017-07-27 19:14:04 +02:00
|
|
|
message("end");
|
2017-09-24 03:38:44 +02:00
|
|
|
}());
|