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-07-27 19:14:04 +02:00
|
|
|
const logging = require("./logging");
|
|
|
|
const {error, warning, message, notice, verbose, } = logging;
|
|
|
|
logging.setPrefix("main script");
|
2017-07-02 12:21:16 +02:00
|
|
|
|
2017-07-27 19:14:04 +02:00
|
|
|
message("start");
|
|
|
|
message("loading storage");
|
2017-07-07 08:50:23 +02:00
|
|
|
browser.storage.local.get().then(function(data){
|
|
|
|
Object.keys(data).forEach(function(key){
|
|
|
|
settings[key] = data[key];
|
|
|
|
});
|
2017-07-27 19:14:04 +02:00
|
|
|
settings.isStillDefault = false;
|
|
|
|
logging.clearQueue();
|
2017-07-07 08:50:23 +02:00
|
|
|
return settings;
|
|
|
|
}).then(function(settings){
|
2017-07-27 19:14:04 +02:00
|
|
|
notice("everything loaded");
|
2017-07-07 08:50:23 +02:00
|
|
|
const lists = require("./lists");
|
|
|
|
lists.updateAll();
|
|
|
|
|
2017-07-27 19:14:04 +02:00
|
|
|
notice("build persistent storage");
|
2017-07-07 08:50:23 +02:00
|
|
|
var persistentRnd = Object.create(null);
|
|
|
|
try {
|
|
|
|
let storedData = JSON.parse(settings.persistentRndStorage);
|
|
|
|
for (var domain in storedData){
|
|
|
|
var value = storedData[domain];
|
|
|
|
if (
|
|
|
|
Array.isArray(value) &&
|
|
|
|
value.length === 128 &&
|
2017-10-03 15:35:31 +02:00
|
|
|
value.every(function(value){
|
|
|
|
return typeof value === "number" && value >= 0 && value < 256;
|
|
|
|
})
|
2017-07-07 08:50:23 +02:00
|
|
|
){
|
|
|
|
persistentRnd[domain] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-03 15:35:31 +02:00
|
|
|
catch(e){
|
|
|
|
// JSON is not valid -> ignore it
|
|
|
|
}
|
2017-07-07 08:50:23 +02:00
|
|
|
|
|
|
|
function updateContentScripts(){
|
2017-07-27 19:14:04 +02:00
|
|
|
message("update content scripts");
|
|
|
|
notice("build settings blob");
|
2017-07-07 08:50:23 +02:00
|
|
|
var settingsBlob = new Blob(
|
|
|
|
[
|
|
|
|
"var settings = " + JSON.stringify(settings) + ";",
|
|
|
|
"var persistentRnd = " + JSON.stringify(persistentRnd) + ";"
|
|
|
|
],
|
|
|
|
{
|
|
|
|
type: "text/javascript"
|
|
|
|
}
|
|
|
|
);
|
2017-07-27 19:14:04 +02:00
|
|
|
warning("TODO: register content scripts -> have to wait for the API to be released");
|
2017-07-07 08:50:23 +02:00
|
|
|
}
|
|
|
|
updateContentScripts();
|
|
|
|
|
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-07-07 08:50:23 +02:00
|
|
|
if (data["canvasBlocker-new-domain-rnd"]){
|
2017-07-27 19:14:04 +02:00
|
|
|
verbose("got new domain rnd", data["canvasBlocker-new-domain-rnd"]);
|
2017-07-07 08:50:23 +02:00
|
|
|
data["canvasBlocker-set-domain-rnd"] = data["canvasBlocker-new-domain-rnd"];
|
|
|
|
persistentRnd[data["canvasBlocker-new-domain-rnd"].domain] = data["canvasBlocker-new-domain-rnd"].rnd;
|
|
|
|
browser.storage.local.get("storePersistentRnd").then(function(prefs){
|
|
|
|
if (prefs.storePersistentRnd){
|
|
|
|
browser.storage.local.set({persistentRndStorage: JSON.stringify(persistentRnd)});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
updateContentScripts();
|
|
|
|
}
|
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-09-24 03:38:44 +02:00
|
|
|
verbose("send back the persistent random seeds", persistentRnd);
|
2017-09-23 23:37:46 +02:00
|
|
|
verbose("send back the settings", settings);
|
|
|
|
port.postMessage({
|
|
|
|
tabId: port.sender.tab.id,
|
|
|
|
persistentRnd: persistentRnd,
|
|
|
|
settings: settings
|
|
|
|
});
|
2017-07-07 08:50:23 +02:00
|
|
|
var url = new URL(port.sender.url);
|
|
|
|
port.onMessage.addListener(function(data){
|
|
|
|
browser.storage.local.get("showNotifications").then(function(data){
|
|
|
|
if (
|
|
|
|
(
|
|
|
|
!data.hasOwnProperty("showNotifications") ||
|
|
|
|
data.showNotifications
|
|
|
|
) &&
|
|
|
|
!lists.get("ignore").match(url)
|
|
|
|
){
|
|
|
|
browser.pageAction.show(port.sender.tab.id);
|
|
|
|
}
|
2017-10-03 15:35:31 +02:00
|
|
|
});
|
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-07-07 08:50:23 +02:00
|
|
|
browser.storage.onChanged.addListener(function(change, area){
|
|
|
|
if (area === "local"){
|
2017-07-27 19:14:04 +02:00
|
|
|
notice("settings changed", change);
|
|
|
|
notice("update settings object");
|
2017-07-07 08:50:23 +02:00
|
|
|
Object.keys(change).forEach(function(key){
|
|
|
|
settings[key] = change[key].newValue;
|
|
|
|
});
|
|
|
|
updateContentScripts();
|
|
|
|
|
|
|
|
if (change.hasOwnProperty("showNotifications") && !change.showNotifications.newValue){
|
2017-07-27 19:14:04 +02:00
|
|
|
message("notifications were disabled -> hide all page actions");
|
2017-07-07 08:50:23 +02:00
|
|
|
browser.tabs.query({}).then(function(tabs){
|
|
|
|
tabs.forEach(function(tab){
|
|
|
|
browser.pageAction.hide(tab.id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (change.hasOwnProperty("storePersistentRnd")){
|
|
|
|
browser.storage.local.set({
|
|
|
|
persistentRndStorage: change.storePersistentRnd.newValue? JSON.stringify(persistentRnd): ""
|
|
|
|
});
|
|
|
|
}
|
2017-02-10 17:37:35 +01:00
|
|
|
}
|
2017-07-07 08:50:23 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// hide page action when a tab is refreshed
|
|
|
|
browser.tabs.onUpdated.addListener(function(tabId, data){
|
|
|
|
if (data.status === "loading"){
|
|
|
|
browser.pageAction.hide(tabId);
|
2017-02-01 10:49:33 +01:00
|
|
|
}
|
2017-07-07 08:50:23 +02:00
|
|
|
});
|
2017-02-10 17:37:35 +01:00
|
|
|
});
|
2017-02-01 10:49:33 +01:00
|
|
|
|
2017-07-16 00:12:12 +02:00
|
|
|
browser.runtime.onInstalled.addListener(function(){
|
2017-07-27 19:14:04 +02:00
|
|
|
message("CanvasBlocker installed");
|
2017-07-16 00:12:12 +02:00
|
|
|
browser.storage.local.get("storageVersion").then(function(data){
|
|
|
|
if (data.storageVersion !== 0.1){
|
|
|
|
browser.storage.local.set({
|
|
|
|
storageVersion: 0.1
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
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
|
|
|
}());
|