1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2025-01-03 10:31:54 +01:00

Only force setting loading when necessary

May help for #160.
This commit is contained in:
kkapsner 2017-12-04 00:26:26 +01:00
parent ba8f35a9a8
commit ce7dc1b422
4 changed files with 103 additions and 23 deletions

View File

@ -5,7 +5,7 @@
"use strict"; "use strict";
const settings = require("./settings"); const settings = require("./settings");
const {intercept} = require("./intercept.js"); const {preIntercept: intercept} = require("./intercept.js");
const {ask} = require("./askForPermission.js"); const {ask} = require("./askForPermission.js");
const lists = require("./lists.js"); const lists = require("./lists.js");
const {check: originalCheck, checkStack: originalCheckStack} = require("./check.js"); const {check: originalCheck, checkStack: originalCheckStack} = require("./check.js");
@ -163,22 +163,5 @@
} }
}); });
// need to wait for the settings to arrive! interceptWindow(window);
while (settings.isStillDefault){
logging.message("Starting synchronous request to wait for settings.");
try {
let xhr = new XMLHttpRequest();
xhr.open("GET", "https://[::]", false);
xhr.send();
xhr = null;
}
catch (e){
logging.verbose("Error in XHR:", e);
}
logging.message("settings still default?", settings.isStillDefault);
}
settings.onloaded(function(){
interceptWindow(window);
});
}()); }());

View File

@ -17,6 +17,7 @@
const randomSupplies = require("./randomSupplies"); const randomSupplies = require("./randomSupplies");
const getWrapped = require("sdk/getWrapped"); const getWrapped = require("sdk/getWrapped");
const logging = require("./logging"); const logging = require("./logging");
const settings = require("./settings");
setRandomSupply(randomSupplies.nonPersistent); setRandomSupply(randomSupplies.nonPersistent);
var apiNames = Object.keys(changedFunctions); var apiNames = Object.keys(changedFunctions);
@ -59,6 +60,83 @@
return window.location.href; return window.location.href;
} }
scope.preIntercept = function preIntercept({subject: window}, apis){
if (!settings.isStillDefault){
scope.intercept({subject: window}, apis);
}
else {
let preIntercepted = false;
let intercepted = false;
const forEachFunction = function(callback){
apiNames.forEach(function(name){
const changedFunction = changedFunctions[name];
(
Array.isArray(changedFunction.object)?
changedFunction.object:
[changedFunction.object]
).forEach(function(object){
var constructor = getWrapped(window)[object];
if (constructor){
callback({name, changedFunction, constructor});
}
});
});
};
let originalPropertyDescriptors = {};
const doPreIntercept = function(){
if (!preIntercepted){
forEachFunction(function({name, constructor}){
var map = originalPropertyDescriptors[name] || new WeakMap();
originalPropertyDescriptors[name] = map;
map.set(constructor, Object.getOwnPropertyDescriptor(constructor.prototype, name));
Object.defineProperty(
constructor.prototype,
name,
{
enumerable: true,
configureable: true,
get: exportFunction(function(){
undoPreIntercept();
settings.forceLoad();
doRealIntercept();
var descriptor = Object.getOwnPropertyDescriptor(constructor.prototype, name);
return descriptor.value || descriptor.get();
}, window),
set: exportFunction(function(){}, window)
}
);
});
preIntercepted = true;
}
};
const undoPreIntercept = function(){
if (preIntercepted){
preIntercepted = false;
forEachFunction(function({name, constructor}){
Object.defineProperty(
constructor.prototype,
name,
originalPropertyDescriptors[name].get(constructor)
);
});
}
};
const doRealIntercept = function(){
if (!intercepted){
scope.intercept({subject: window}, apis);
intercepted = true;
}
};
logging.verbose("pre intercept until settings are loaded");
doPreIntercept();
settings.onloaded(function(){
undoPreIntercept();
doRealIntercept();
});
}
};
scope.intercept = function intercept({subject: window}, {check, checkStack, ask, notify, prefs}){ scope.intercept = function intercept({subject: window}, {check, checkStack, ask, notify, prefs}){
var siteStatus = check({url: getURL(window)}); var siteStatus = check({url: getURL(window)});
logging.verbose("status for page", window, siteStatus); logging.verbose("status for page", window, siteStatus);
@ -82,7 +160,7 @@
name, name,
{ {
enumerable: true, enumerable: true,
configureable: false, configureable: true,
get: exportFunction(function(){ get: exportFunction(function(){
var url = getURL(window); var url = getURL(window);
if (!url){ if (!url){
@ -158,7 +236,12 @@
Object.defineProperty( Object.defineProperty(
constructor.prototype, constructor.prototype,
name, name,
{value} {
value,
writable: true,
configurable: true,
enumerable: true
}
); );
}, window) }, window)
} }

View File

@ -105,9 +105,8 @@
js: [{ js: [{
code: `(function(){ code: `(function(){
const settings = require("./settings"); const settings = require("./settings");
console.log(settings.init(${JSON.stringify(data)})); settings.init(${JSON.stringify(data)});
}())` }())`
// code: "(function(){const settings = require(\"./settings\");console.log(settings);}());"
}] }]
}).then(function(api){ }).then(function(api){
logging.verbose("Content script registered."); logging.verbose("Content script registered.");

View File

@ -590,5 +590,20 @@
callback(); callback();
} }
}; };
scope.forceLoad = function(){
while (settings.isStillDefault){
logging.message("Starting synchronous request to wait for settings.");
try {
let xhr = new XMLHttpRequest();
xhr.open("GET", "https://[::]", false);
xhr.send();
xhr = null;
}
catch (e){
logging.verbose("Error in XHR:", e);
}
logging.message("settings still default?", settings.isStillDefault);
}
};
Object.seal(scope); Object.seal(scope);
}()); }());