1
0
Fork 0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2025-07-04 04:26:35 +02:00

Added clear interval

Also ensures that the persistent data is loaded correctly in ESR.

Fixes #139 and #143
This commit is contained in:
kkapsner 2017-11-08 17:46:41 +01:00
parent ec1c5ae7d8
commit 3cb1974b18
14 changed files with 283 additions and 43 deletions

View file

@ -85,13 +85,13 @@
metaLog("logging queue cleared");
}
};
settings.on("isStillDefault", scope.clearQueue);
settings.loaded.then(scope.clearQueue);
scope.error = error;
scope.warning = warning;
scope.message = message;
scope.notice = notice;
scope.verbose = verbose;
require.emit("./logging");
metaLog("logging available");

View file

@ -9,33 +9,13 @@
const {error, warning, message, notice, verbose, } = logging;
const lists = require("./lists");
logging.setPrefix("main script");
const persistentRndStorage = require("./persistentRndStorage");
message("start of background script");
message("waiting for settings to be loaded");
settings.onloaded(function(){
notice("everything loaded");
notice("build persistent storage");
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 &&
value.every(function(value){
return typeof value === "number" && value >= 0 && value < 256;
})
){
persistentRnd[domain] = value;
}
}
}
catch(e){
// JSON is not valid -> ignore it
}
function updateContentScripts(){
message("update content scripts");
notice("build settings blob");
@ -51,24 +31,26 @@
warning("TODO: register content scripts -> have to wait for the API to be released");
}
updateContentScripts();
persistentRndStorage.init();
message("register non port message listener");
browser.runtime.onMessage.addListener(function(data){
notice("got data without port", data);
var keys = Object.keys(data);
if (data["canvasBlocker-new-domain-rnd"]){
verbose("got new domain rnd", data["canvasBlocker-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;
if (settings.storePersistentRnd){
settings.persistentRndStorage = JSON.stringify(persistentRnd);
persistentRndStorage.setDomainData(
data["canvasBlocker-new-domain-rnd"].domain,
data["canvasBlocker-new-domain-rnd"].rnd
);
if (keys.length === 1){
return;
}
updateContentScripts();
}
if (data["canvasBlocker-clear-domain-rnd"]){
verbose("domain rnd cleared");
persistentRnd = Object.create(null);
if (settings.storePersistentRnd){
settings.persistentRndStorage = JSON.stringify(persistentRnd);
persistentRndStorage.clear();
if (keys.length === 1){
return;
}
updateContentScripts();
}
@ -84,10 +66,10 @@
browser.runtime.onConnect.addListener(function(port){
notice("got port", port);
verbose("send back the tab id", port.sender.tab.id);
verbose("send back the persistent random seeds", persistentRnd);
verbose("send back the persistent random seeds", persistentRndStorage.persistentRnd);
port.postMessage({
tabId: port.sender.tab.id,
persistentRnd: persistentRnd
persistentRnd: persistentRndStorage.persistentRnd
});
var url = new URL(port.sender.url);
port.onMessage.addListener(function(data){
@ -116,9 +98,6 @@
});
}
});
settings.on("storePersistentRnd", function({newValue}){
settings.persistentRndStorage = newValue? JSON.stringify(persistentRnd): "";
});
// hide page action when a tab is refreshed
browser.tabs.onUpdated.addListener(function(tabId, data){

111
lib/persistentRndStorage.js Normal file
View file

@ -0,0 +1,111 @@
/* 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/. */
(function(){
"use strict";
var scope;
if ((typeof exports) !== "undefined"){
scope = exports;
}
else {
scope = {};
window.scope.persistentRndStorage = scope;
}
const settings = require("./settings");
const logging = require("./logging");
scope.persistentRnd = Object.create(null);
scope.init = function init(){
logging.message("initializing persistent rng storage");
logging.notice("build persistent storage");
if (settings.storePersistentRnd){
try {
let storedData = JSON.parse(settings.persistentRndStorage);
for (var domain in storedData){
var value = storedData[domain];
if (
Array.isArray(value) &&
value.length === 128 &&
value.every(function(value){
return typeof value === "number" && value >= 0 && value < 256;
})
){
scope.persistentRnd[domain] = value;
}
}
}
catch (e){
// JSON is not valid -> ignore it
}
}
else {
settings.persistentRndStorage = "";
settings.lastPersistentRndClearing = Date.now();
}
registerTimeout();
logging.notice("register settings change event listener");
settings.on(["persistentRndClearIntervalValue", "persistentRndClearIntervalUnit"], function(){
window.clearTimeout(clearTimeout);
registerTimeout();
});
settings.on("storePersistentRnd", function({newValue}){
settings.persistentRndStorage = newValue? JSON.stringify(scope.persistentRnd): "";
});
};
const getInterval = function(){
var units = {
seconds: 1000,
minutes: 60 * 1000,
hours: 60 * 60 * 1000,
days: 24 * 60 * 60 * 1000,
weeks: 7 * 24 * 60 * 60 * 1000,
months: 30 * 24 * 60 * 60 * 1000,
years: 365 * 24 * 60 * 60 * 1000,
};
return function getInterval(){
return settings.persistentRndClearIntervalValue * units[settings.persistentRndClearIntervalUnit] || 0;
};
}();
let clearTimeout;
function registerTimeout(){
var interval = getInterval();
if (interval > 0){
var timeout = settings.lastPersistentRndClearing + interval - Date.now();
logging.message("registering persistent rng data clearing timeout. Clearing in ", timeout, "ms");
clearTimeout = window.setTimeout(clear, timeout);
}
}
function broadcast(data){
browser.tabs.query({}).then(function(tabs){
tabs.forEach(function(tab){
browser.tabs.sendMessage(tab.id, data);
});
});
}
function clear(){
logging.verbose("domain rnd cleared");
scope.persistentRnd = Object.create(null);
settings.persistentRndStorage = JSON.stringify(scope.persistentRnd);
settings.lastPersistentRndClearing = Date.now();
registerTimeout();
broadcast({"canvasBlocker-clear-domain-rnd": true});
}
function setDomainData(domain, rnd){
logging.verbose("got new domain rnd for ", domain, ":", rnd);
scope.persistentRnd[domain] = rnd;
settings.persistentRndStorage = JSON.stringify(scope.persistentRnd);
broadcast({"canvasBlocker-set-domain-rnd": {domain, rnd}});
}
scope.clear = clear;
scope.setDomainData = setDomainData;
}());

View file

@ -12,6 +12,8 @@
window.scope.randomSupplies = {};
scope = window.scope.randomSupplies;
}
const settings = require("./settings");
function getDomain(window){
if (!window.location.href || window.location.href === "about:blank"){
@ -26,6 +28,26 @@
}
var persistentRnd = Object.create(null);
settings.onloaded(function(){
try {
let storedData = JSON.parse(settings.persistentRndStorage);
for (var domain in storedData){
var value = storedData[domain];
if (
Array.isArray(value) &&
value.length === 128 &&
value.every(function(value){
return typeof value === "number" && value >= 0 && value < 256;
})
){
persistentRnd[domain] = value;
}
}
}
catch (e){
// JSON is not valid -> ignore it
}
});
const getPersistentRnd = (function(){
browser.runtime.onMessage.addListener(function(data){

View file

@ -59,6 +59,19 @@
name: "storePersistentRnd",
defaultValue: false
},
{
name: "persistentRndClearIntervalValue",
defaultValue: 0
},
{
name: "persistentRndClearIntervalUnit",
defaultValue: "days",
options: ["seconds", "minutes", "hours", "days", "weeks", "months", "years"]
},
{
name: "lastPersistentRndClearing",
defaultValue: 0
},
{
name: "askOnlyOnce",
defaultValue: true

View file

@ -98,11 +98,18 @@
}).forEach(...args);
};
scope.on = function onSettingsChange(name, callback){
if (eventHandler.hasOwnProperty(name)){
eventHandler[name].push(callback);
if (Array.isArray(name)){
name.forEach(function(name){
onSettingsChange(name, callback);
});
}
else {
logging.warning("Unable to register event handler for unknown setting", name);
if (eventHandler.hasOwnProperty(name)){
eventHandler[name].push(callback);
}
else {
logging.warning("Unable to register event handler for unknown setting", name);
}
}
};