mirror of
https://github.com/kkapsner/CanvasBlocker
synced 2024-12-22 21:00:23 +01:00
Restructuring of main.js.
This commit is contained in:
parent
18a2e32044
commit
8cec88f5f7
206
lib/main.js
206
lib/main.js
@ -5,86 +5,28 @@
|
|||||||
(function(){
|
(function(){
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
console.log("start main script");
|
function log(...args){
|
||||||
browser.runtime.onMessage.addListener(function(data){
|
args.unshift("main script:");
|
||||||
console.log("got data", data);
|
args.unshift(new Date());
|
||||||
});
|
console.log.apply(console, args);
|
||||||
browser.runtime.onConnect.addListener(function(port){
|
|
||||||
console.log("got port", port);
|
|
||||||
port.postMessage({tabId: port.sender.tab.id});
|
|
||||||
port.onMessage.addListener(function(data){
|
|
||||||
browser.storage.local.get("showNotifications").then(function(data){
|
|
||||||
// TODO: handle ignore list
|
|
||||||
if (!data.hasOwnProperty("showNotifications") || data.showNotifications){
|
|
||||||
browser.pageAction.show(port.sender.tab.id);
|
|
||||||
}
|
}
|
||||||
})
|
|
||||||
console.log("got data", data, "from port", port);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// hide all page actions when showNotifications is set to false
|
log("start");
|
||||||
browser.storage.onChanged.addListener(function(change, area){
|
log("loading storage");
|
||||||
if (area === "local" && change.hasOwnProperty("showNotifications") && !change.showNotifications.newValue){
|
browser.storage.local.get().then(function(data){
|
||||||
browser.tabs.query({}).then(function(tabs){
|
Object.keys(data).forEach(function(key){
|
||||||
tabs.forEach(function(tab){
|
settings[key] = data[key];
|
||||||
browser.pageAction.hide(tab.id);
|
|
||||||
});
|
});
|
||||||
});
|
return settings;
|
||||||
}
|
}).then(function(settings){
|
||||||
});
|
log("everything loaded");
|
||||||
|
|
||||||
// hide page action when a tab is refreshed
|
|
||||||
browser.tabs.onUpdated.addListener(function(tabId, data){
|
|
||||||
if (data.status === "loading"){
|
|
||||||
browser.pageAction.hide(tabId);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log("end main script");
|
|
||||||
return null;
|
|
||||||
require("./stylePreferencePane");
|
|
||||||
|
|
||||||
|
|
||||||
const {when: unload} = require("sdk/system/unload");
|
|
||||||
const {notify} = require("./notifications");
|
|
||||||
|
|
||||||
const _ = require("sdk/l10n").get;
|
|
||||||
const lists = require("./lists");
|
const lists = require("./lists");
|
||||||
const preferences = require("sdk/simple-prefs");
|
lists.updateAll();
|
||||||
const prefs = preferences.prefs;
|
|
||||||
|
|
||||||
require("./webExtension");
|
log("build persistent storage");
|
||||||
|
|
||||||
const notificationPref = {
|
|
||||||
doShow: function(){
|
|
||||||
return prefs.showNotifications;
|
|
||||||
},
|
|
||||||
setShow: function(value){
|
|
||||||
prefs.showNotifications = value;
|
|
||||||
prefService.set("extensions.CanvasBlocker@kkapsner.de.showNotifications", prefs.showNotifications);
|
|
||||||
},
|
|
||||||
displayTime: function(){
|
|
||||||
return prefs.notificationDisplayTime;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const {processes, frames, remoteRequire} = require("sdk/remote/parent");
|
|
||||||
// remoteRequire("./frame.js", module); // currently not working due to a regression in the SDK
|
|
||||||
var framePath = require("sdk/self").data.url("").replace(/data\/$/, "") + "lib/frame.js";
|
|
||||||
remoteRequire(framePath);
|
|
||||||
|
|
||||||
frames.port.on("canvasBlocker-notify", function(frame, data){
|
|
||||||
notify(data, {lists, _, notificationPref, browser: frame.frameElement});
|
|
||||||
});
|
|
||||||
unload(function(){
|
|
||||||
processes.port.emit("canvasBlocker-unload");
|
|
||||||
});
|
|
||||||
|
|
||||||
// persistent rng
|
|
||||||
var persistentRnd = Object.create(null);
|
var persistentRnd = Object.create(null);
|
||||||
try {
|
try {
|
||||||
let storedData = JSON.parse(prefs.persistentRndStorage);
|
let storedData = JSON.parse(settings.persistentRndStorage);
|
||||||
for (var domain in storedData){
|
for (var domain in storedData){
|
||||||
var value = storedData[domain];
|
var value = storedData[domain];
|
||||||
if (
|
if (
|
||||||
@ -98,38 +40,106 @@
|
|||||||
}
|
}
|
||||||
catch(e){}
|
catch(e){}
|
||||||
|
|
||||||
processes.port.on("canvasBlocker-new-domain-rnd", function(process, data){
|
function updateContentScripts(){
|
||||||
processes.port.emit("canvasBlocker-set-domain-rnd", data);
|
log("update content scripts");
|
||||||
persistentRnd[data.domain] = data.rnd;
|
log("build settings blob");
|
||||||
|
var settingsBlob = new Blob(
|
||||||
|
[
|
||||||
|
"var settings = " + JSON.stringify(settings) + ";",
|
||||||
|
"var persistentRnd = " + JSON.stringify(persistentRnd) + ";"
|
||||||
|
],
|
||||||
|
{
|
||||||
|
type: "text/javascript"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
log("TODO: register content scripts -> have to wait for the API to be released");
|
||||||
|
}
|
||||||
|
updateContentScripts();
|
||||||
|
|
||||||
|
log("register non port message listener");
|
||||||
|
browser.runtime.onMessage.addListener(function(data){
|
||||||
|
log("got data without port", data);
|
||||||
|
if (data["canvasBlocker-new-domain-rnd"]){
|
||||||
|
log("got new domain rnd");
|
||||||
|
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){
|
if (prefs.storePersistentRnd){
|
||||||
prefs.persistentRndStorage = JSON.stringify(persistentRnd);
|
browser.storage.local.set({persistentRndStorage: JSON.stringify(persistentRnd)});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
processes.forEvery(function(process){
|
updateContentScripts();
|
||||||
if (process.isRemote){
|
|
||||||
for (var domain in persistentRnd){
|
|
||||||
process.port.emit("canvasBlocker-set-domain-rnd", {domain, rnd: persistentRnd[domain]});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
log("pass the message to the tabs");
|
||||||
|
browser.tabs.query({}).then(function(tabs){
|
||||||
|
tabs.forEach(function(tab){
|
||||||
|
browser.tabs.sendMessage(tab.id, data);
|
||||||
});
|
});
|
||||||
preferences.on("storePersistentRnd", function(){
|
|
||||||
if (prefs.storePersistentRnd){
|
|
||||||
prefs.persistentRndStorage = JSON.stringify(persistentRnd);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
prefs.persistentRndStorage = "";
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
preferences.on("clearPersistentRnd", function(){
|
|
||||||
persistentRnd = Object.create(null);
|
|
||||||
prefs.persistentRndStorage = "";
|
|
||||||
processes.port.emit("canvasBlocker-clear-domain-rnd");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// show release notes
|
log("register port listener");
|
||||||
var data = require("sdk/self").data;
|
browser.runtime.onConnect.addListener(function(port){
|
||||||
preferences.on("showReleaseNotes", function(){
|
log("got port", port);
|
||||||
var url = data.url("releaseNotes.txt").replace("/data/", "/");
|
log("send back the tab id", port.sender.tab.id);
|
||||||
require("sdk/tabs").open(url);
|
port.postMessage({tabId: port.sender.tab.id});
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
log("got data", data, "from port", port);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
log("register storage change event listener");
|
||||||
|
browser.storage.onChanged.addListener(function(change, area){
|
||||||
|
if (area === "local"){
|
||||||
|
log("settings changed", change);
|
||||||
|
log("update settings object");
|
||||||
|
Object.keys(change).forEach(function(key){
|
||||||
|
settings[key] = change[key].newValue;
|
||||||
|
});
|
||||||
|
updateContentScripts();
|
||||||
|
|
||||||
|
if (change.hasOwnProperty("showNotifications") && !change.showNotifications.newValue){
|
||||||
|
log("notifications were disabled -> hide all page actions");
|
||||||
|
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): ""
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// hide page action when a tab is refreshed
|
||||||
|
browser.tabs.onUpdated.addListener(function(tabId, data){
|
||||||
|
if (data.status === "loading"){
|
||||||
|
browser.pageAction.hide(tabId);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
log("TODO: register unload events - do not know how");
|
||||||
|
// old code
|
||||||
|
// const {when: unload} = require("sdk/system/unload");
|
||||||
|
// unload(function(){
|
||||||
|
// processes.port.emit("canvasBlocker-unload");
|
||||||
|
// });
|
||||||
|
|
||||||
|
log("end");
|
||||||
}());
|
}());
|
@ -25,7 +25,7 @@ function require(module){
|
|||||||
}
|
}
|
||||||
else if (module === "sdk/l10n"){
|
else if (module === "sdk/l10n"){
|
||||||
return {
|
return {
|
||||||
get: function(key){console.log(key);
|
get: function(key){
|
||||||
return browser.i18n.getMessage(key);
|
return browser.i18n.getMessage(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,12 @@
|
|||||||
"homepage": "https://github.com/kkapsner/CanvasBlocker/",
|
"homepage": "https://github.com/kkapsner/CanvasBlocker/",
|
||||||
"version": "0.4.0-Development",
|
"version": "0.4.0-Development",
|
||||||
"background": {
|
"background": {
|
||||||
"scripts": ["lib/main.js"]
|
"scripts": [
|
||||||
|
"lib/defaultSettings.js",
|
||||||
|
"lib/require.js",
|
||||||
|
"lib/lists.js",
|
||||||
|
"lib/main.js"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"content_scripts": [{
|
"content_scripts": [{
|
||||||
"matches": ["<all_urls>"],
|
"matches": ["<all_urls>"],
|
||||||
|
@ -56,8 +56,8 @@
|
|||||||
window.open("../releaseNotes.txt", "_blank");
|
window.open("../releaseNotes.txt", "_blank");
|
||||||
},
|
},
|
||||||
clearPersistentRnd: function(){
|
clearPersistentRnd: function(){
|
||||||
browser.runtime.sendMessage({"canvasBlocker-clear-domain-rnd": true});
|
|
||||||
browser.storage.local.set({persistentRndStorage: ""});
|
browser.storage.local.set({persistentRndStorage: ""});
|
||||||
|
browser.runtime.sendMessage({"canvasBlocker-clear-domain-rnd": true});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Array.from(document.querySelectorAll("button.setting")).forEach(function(button){
|
Array.from(document.querySelectorAll("button.setting")).forEach(function(button){
|
||||||
|
Loading…
x
Reference in New Issue
Block a user