mirror of
https://github.com/kkapsner/CanvasBlocker
synced 2025-01-03 10:31:54 +01:00
Added dynamic settings content script
Not yet fully usable due to missing detection in frame.js
This commit is contained in:
parent
2bc0315c5b
commit
5f5d36de23
42
lib/main.js
42
lib/main.js
@ -84,6 +84,48 @@
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
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(){
|
||||
const settings = require("./settings");
|
||||
console.log(settings.init(${JSON.stringify(data)}));
|
||||
}())`
|
||||
// code: "(function(){const settings = require(\"./settings\");console.log(settings);}());"
|
||||
}]
|
||||
}).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()");
|
||||
}
|
||||
});
|
||||
|
||||
browser.runtime.onInstalled.addListener(function(){
|
||||
|
@ -176,7 +176,13 @@
|
||||
};
|
||||
|
||||
logging.verbose("loading settings");
|
||||
scope.loaded = browser.storage.local.get().then(function(storage){
|
||||
let initialized = false;
|
||||
const initEvents = [];
|
||||
scope.init = function(storage){
|
||||
if (initialized){
|
||||
return false;
|
||||
}
|
||||
initialized = true;
|
||||
logging.message("settings loaded");
|
||||
if (!storage.storageVersion){
|
||||
logging.message("No storage version found. Initializing storage.");
|
||||
@ -209,14 +215,14 @@
|
||||
changeValue(name, value);
|
||||
});
|
||||
changeValue("isStillDefault", false);
|
||||
|
||||
eventHandler.any.forEach(function(callback){
|
||||
callback();
|
||||
});
|
||||
});
|
||||
|
||||
initEvents.forEach(function(callback){callback();});
|
||||
return true;
|
||||
};
|
||||
scope.loaded = browser.storage.local.get().then(scope.init);
|
||||
scope.onloaded = function(callback){
|
||||
if (scope.isStillDefault){
|
||||
scope.loaded.then(function(){callback();});
|
||||
initEvents.push(callback);
|
||||
}
|
||||
else {
|
||||
callback();
|
||||
|
Loading…
x
Reference in New Issue
Block a user