1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-12-22 12:50:36 +01:00

Remove duplicated code

This commit is contained in:
kkapsner 2020-01-26 01:11:18 +01:00
parent 64b60c834a
commit 6fbb9339a1
6 changed files with 59 additions and 96 deletions

View File

@ -176,5 +176,18 @@
} }
}; };
scope.waitSync = function waitSync(reason = "for no reason"){
logging.message(`Starting synchronous request ${reason}.`);
try {
let xhr = new XMLHttpRequest();
xhr.open("GET", "https://[::]", false);
xhr.send();
xhr = null;
}
catch (error){
logging.verbose("Error in XHR:", error);
}
};
Object.seal(scope); Object.seal(scope);
}()); }());

View File

@ -157,19 +157,22 @@
}; };
const getChannelDataAlreadyFakedArrays = new WeakMap(); const getChannelDataAlreadyFakedArrays = new WeakMap();
function fakeArrayCheckerCallback(array, fakeFunction, args, check){
const {prefs, notify, window, original} = check;
notify("fakedAudioReadout");
const ret = original.call(this, ...args);
fakeFunction(array, window, prefs);
return ret;
}
// changed functions and their fakes // changed functions and their fakes
scope.changedFunctions = { scope.changedFunctions = {
getFloatFrequencyData: { getFloatFrequencyData: {
object: ["AnalyserNode"], object: ["AnalyserNode"],
fakeGenerator: function(checker){ fakeGenerator: function(checker){
return function getFloatFrequencyData(array){ return function getFloatFrequencyData(array){
return checkerWrapper(checker, this, arguments, function(args, check){ return checkerWrapper(checker, this, arguments,
const {prefs, notify, window, original} = check; fakeArrayCheckerCallback.bind(this, array, fakeFloat32Array)
notify("fakedAudioReadout"); );
const ret = original.call(this, ...args);
fakeFloat32Array(array, window, prefs);
return ret;
});
}; };
} }
}, },
@ -177,13 +180,9 @@
object: ["AnalyserNode"], object: ["AnalyserNode"],
fakeGenerator: function(checker){ fakeGenerator: function(checker){
return function getByteFrequencyData(array){ return function getByteFrequencyData(array){
return checkerWrapper(checker, this, arguments, function(args, check){ return checkerWrapper(checker, this, arguments,
const {prefs, notify, window, original} = check; fakeArrayCheckerCallback.bind(this, array, fakeUint8Array)
notify("fakedAudioReadout"); );
const ret = original.call(this, ...args);
fakeUint8Array(array, window, prefs);
return ret;
});
}; };
} }
}, },
@ -191,13 +190,9 @@
object: ["AnalyserNode"], object: ["AnalyserNode"],
fakeGenerator: function(checker){ fakeGenerator: function(checker){
return function getFloatTimeDomainData(array){ return function getFloatTimeDomainData(array){
return checkerWrapper(checker, this, arguments, function(args, check){ return checkerWrapper(checker, this, arguments,
const {prefs, notify, window, original} = check; fakeArrayCheckerCallback.bind(this, array, fakeFloat32Array)
notify("fakedAudioReadout"); );
const ret = original.call(this, ...args);
fakeFloat32Array(array, window, prefs);
return ret;
});
}; };
} }
}, },
@ -205,13 +200,9 @@
object: ["AnalyserNode"], object: ["AnalyserNode"],
fakeGenerator: function(checker){ fakeGenerator: function(checker){
return function getByteTimeDomainData(array){ return function getByteTimeDomainData(array){
return checkerWrapper(checker, this, arguments, function(args, check){ return checkerWrapper(checker, this, arguments,
const {prefs, notify, window, original} = check; fakeArrayCheckerCallback.bind(this, array, fakeUint8Array)
notify("fakedAudioReadout"); );
const ret = original.call(this, ...args);
fakeUint8Array(array, window, prefs);
return ret;
});
}; };
} }
}, },

View File

@ -13,7 +13,7 @@
} }
const {checkerWrapper, setGetterProperties, getStatusByFlag} = require("./modifiedAPIFunctions"); const {checkerWrapper, setGetterProperties, getStatusByFlag} = require("./modifiedAPIFunctions");
const logging = require("./logging"); const extension = require("./extension");
const navigator = require("./navigator"); const navigator = require("./navigator");
let cookieStoreId = false; let cookieStoreId = false;
@ -27,16 +27,7 @@
}; };
function getCookieStoreId(){ function getCookieStoreId(){
while (cookieStoreId === false){ while (cookieStoreId === false){
logging.message("Starting synchronous request to wait for cookie store id."); extension.waitSync("to wait for cookie store id");
try {
let xhr = new XMLHttpRequest();
xhr.open("GET", "https://[::]", false);
xhr.send();
xhr = null;
}
catch (error){
logging.verbose("Error in XHR:", error);
}
} }
return cookieStoreId; return cookieStoreId;
} }

View File

@ -123,16 +123,7 @@
return function getPersistentRnd(window){ return function getPersistentRnd(window){
while (cookieStoreId === false){ while (cookieStoreId === false){
logging.message("Starting synchronous request to wait for cookie store id."); extension.waitSync("to wait for cookie store id");
try {
let xhr = new XMLHttpRequest();
xhr.open("GET", "https://[::]", false);
xhr.send();
xhr = null;
}
catch (error){
logging.verbose("Error in XHR:", error);
}
} }
const domain = cookieStoreId + getDomain(window); const domain = cookieStoreId + getDomain(window);
if (!persistentRnd[domain]){ if (!persistentRnd[domain]){

View File

@ -13,6 +13,7 @@
} }
const logging = require("./logging"); const logging = require("./logging");
const extension = require("./extension");
const settingDefinitions = require("./settingDefinitions"); const settingDefinitions = require("./settingDefinitions");
const settingContainers = require("./settingContainers"); const settingContainers = require("./settingContainers");
const definitionsByName = {}; const definitionsByName = {};
@ -450,16 +451,7 @@
}; };
scope.forceLoad = function(){ scope.forceLoad = function(){
while (settings.isStillDefault){ while (settings.isStillDefault){
logging.message("Starting synchronous request to wait for settings."); extension.waitSync("to wait for settings");
try {
let xhr = new XMLHttpRequest();
xhr.open("GET", "https://[::]", false);
xhr.send();
xhr = null;
}
catch (error){
logging.verbose("Error in XHR:", error);
}
logging.message("settings still default?", settings.isStillDefault); logging.message("settings still default?", settings.isStillDefault);
} }
}; };

View File

@ -42,42 +42,27 @@
const urlSettings = {}; const urlSettings = {};
(oldStorage.blackList || "").split(",") [
{listName: "blackList", property: "blockMode", value: "block"},
{listName: "whiteList", property: "blockMode", value: "allow"},
{listName: "ignoreList", property: "showNotifications", value: false}
].forEach(function(listAction){
(oldStorage[listAction.listName] || "").split(",")
.map(function(url){return url.trim();}) .map(function(url){return url.trim();})
.filter(function(url){return !!url;}) .filter(function(url){return !!url;})
.forEach(function(url){ .forEach(function(url){
let entry = urlSettings[url]; let entry = urlSettings[url];
if (!entry){ if (!entry){
entry = {url, blockMode: "block"}; entry = {url, [listAction.property]: listAction.value};
urlSettings[url] = entry;
newStorage.urlSettings.push(entry);
}
});
(oldStorage.whiteList || "").split(",")
.map(function(url){return url.trim();})
.filter(function(url){return !!url;})
.forEach(function(url){
let entry = urlSettings[url];
if (!entry){
entry = {url, blockMode: "allow"};
urlSettings[url] = entry;
newStorage.urlSettings.push(entry);
}
});
(oldStorage.ignoreList || "").split(",")
.map(function(url){return url.trim();})
.filter(function(url){return !!url;})
.forEach(function(url){
let entry = urlSettings[url];
if (!entry){
entry = {url, showNotifications: false};
urlSettings[url] = entry; urlSettings[url] = entry;
newStorage.urlSettings.push(entry); newStorage.urlSettings.push(entry);
} }
else { else {
entry.showNotifications = false; entry[listAction.property] = listAction.value;
} }
}); });
});
["whiteList", "blackList", "ignoreList"].forEach(function(list){ ["whiteList", "blackList", "ignoreList"].forEach(function(list){
if (oldStorage.hasOwnProperty(list)){ if (oldStorage.hasOwnProperty(list)){
newStorage[list] = ""; newStorage[list] = "";