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";
const settings = require("./settings");
const {intercept} = require("./intercept.js");
const {preIntercept: intercept} = require("./intercept.js");
const {ask} = require("./askForPermission.js");
const lists = require("./lists.js");
const {check: originalCheck, checkStack: originalCheckStack} = require("./check.js");
@ -163,22 +163,5 @@
}
});
// need to wait for the settings to arrive!
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 getWrapped = require("sdk/getWrapped");
const logging = require("./logging");
const settings = require("./settings");
setRandomSupply(randomSupplies.nonPersistent);
var apiNames = Object.keys(changedFunctions);
@ -59,6 +60,83 @@
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}){
var siteStatus = check({url: getURL(window)});
logging.verbose("status for page", window, siteStatus);
@ -82,7 +160,7 @@
name,
{
enumerable: true,
configureable: false,
configureable: true,
get: exportFunction(function(){
var url = getURL(window);
if (!url){
@ -158,7 +236,12 @@
Object.defineProperty(
constructor.prototype,
name,
{value}
{
value,
writable: true,
configurable: true,
enumerable: true
}
);
}, window)
}

View File

@ -105,9 +105,8 @@
js: [{
code: `(function(){
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){
logging.verbose("Content script registered.");

View File

@ -590,5 +590,20 @@
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);
}());