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

Remove code duplications and cleanup

This commit is contained in:
kkapsner 2019-12-10 15:07:22 +01:00
parent 8e414becf0
commit 32f9ea7447
9 changed files with 58 additions and 117 deletions

View File

@ -50,6 +50,7 @@
"no-unreachable": "warn",
"no-unused-vars": "off",
"no-use-before-define": ["error", {"functions": false}],
"no-useless-rename": "warn",
"no-var": "error",
"quotes": ["error", "double"],
"semi": ["error", "always"],

View File

@ -30,6 +30,14 @@
return undefined;
};
scope.getStatusByFlag = function getStatusByFlag(flag){
return function getStatus(obj, status, prefs){
status = Object.create(status);
status.active = prefs(flag, status.url);
return status;
};
};
scope.setFunctionProperties = function setFunctionProperties(functions, data){
Object.keys(functions).forEach(function(key){
const func = functions[key];

View File

@ -13,7 +13,7 @@
}
const {sha256String: hashing} = require("./hash");
const {checkerWrapper} = require("./modifiedAPIFunctions");
const {checkerWrapper, setFunctionProperties, getStatusByFlag} = require("./modifiedAPIFunctions");
let randomSupply = null;
@ -156,12 +156,6 @@
randomSupply = supply;
};
function getStatus(obj, status, prefs){
status = Object.create(status);
status.active = prefs("protectAudio", status.url);
return status;
}
const getChannelDataAlreadyFakedArrays = new WeakMap();
// changed functions and their fakes
scope.changedFunctions = {
@ -275,9 +269,10 @@
}
},
};
Object.keys(scope.changedFunctions).forEach(function(key){
scope.changedFunctions[key].type = "readout";
scope.changedFunctions[key].getStatus = getStatus;
scope.changedFunctions[key].api = "audio";
setFunctionProperties(scope.changedFunctions, {
type: "readout",
getStatus: getStatusByFlag("protectAudio"),
api: "audio"
});
}());

View File

@ -12,7 +12,7 @@
scope = require.register("./modifiedDOMRectAPI", {});
}
const {getWrapped, checkerWrapper, setProperties: setProperties} = require("./modifiedAPIFunctions");
const {getWrapped, checkerWrapper, setProperties, getStatusByFlag} = require("./modifiedAPIFunctions");
const {byteArrayToString: hash} = require("./hash");
@ -87,6 +87,15 @@
return cached;
}
function registerCallback(args, check){
const {prefs, notify, window, original} = check;
const originalValue = args.length?
original.apply(this, window.Array.from(args)):
original.call(this);
registerDOMRect(originalValue, notify, window, prefs);
return originalValue;
}
scope.changedFunctions = {
getClientRects: {
object: ["Range", "Element"],
@ -107,12 +116,7 @@
object: ["Range", "Element"],
fakeGenerator: function(checker){
return function getBoundingClientRect(){
return checkerWrapper(checker, this, arguments, function(args, check){
const {prefs, notify, window, original} = check;
const ret = args.length? original.apply(this, window.Array.from(args)): original.call(this);
registerDOMRect(ret, notify, window, prefs);
return ret;
});
return checkerWrapper(checker, this, arguments, registerCallback);
};
}
@ -121,12 +125,7 @@
object: ["DOMQuad"],
fakeGenerator: function(checker){
return function getBounds(){
return checkerWrapper(checker, this, arguments, function(args, check){
const {prefs, notify, window, original} = check;
const ret = args.length? original.apply(this, window.Array.from(args)): original.call(this);
registerDOMRect(ret, notify, window, prefs);
return ret;
});
return checkerWrapper(checker, this, arguments, registerCallback);
};
}
},
@ -134,25 +133,15 @@
object: ["SVGGraphicsElement"],
fakeGenerator: function(checker){
return function getBBox(){
return checkerWrapper(checker, this, arguments, function(args, check){
const {prefs, notify, window, original} = check;
const ret = args.length? original.apply(this, window.Array.from(args)): original.call(this);
registerDOMRect(ret, notify, window, prefs);
return ret;
});
return checkerWrapper(checker, this, arguments, registerCallback);
};
}
},
getExtentOfChar: {
object: ["SVGTextContentElement"],
fakeGenerator: function(checker){
return function getBBox(){
return checkerWrapper(checker, this, arguments, function(args, check){
const {prefs, notify, window, original} = check;
const ret = args.length? original.apply(this, window.Array.from(args)): original.call(this);
registerDOMRect(ret, notify, window, prefs);
return ret;
});
return function getExtentOfChar(){
return checkerWrapper(checker, this, arguments, registerCallback);
};
}
},
@ -234,14 +223,7 @@
getterGenerator: function(checker){
const temp = {
get intersectionRect(){
return checkerWrapper(checker, this, arguments, function(args, check){
const {prefs, notify, window, original} = check;
const originalValue = args.length?
original.apply(this, window.Array.from(args)):
original.call(this);
registerDOMRect(originalValue, notify, window, prefs);
return originalValue;
});
return checkerWrapper(checker, this, arguments, registerCallback);
}
};
return Object.getOwnPropertyDescriptor(temp, "intersectionRect").get;
@ -257,14 +239,7 @@
getterGenerator: function(checker){
const temp = {
get boundingClientRect(){
return checkerWrapper(checker, this, arguments, function(args, check){
const {prefs, notify, window, original} = check;
const originalValue = args.length?
original.apply(this, window.Array.from(args)):
original.call(this);
registerDOMRect(originalValue, notify, window, prefs);
return originalValue;
});
return checkerWrapper(checker, this, arguments, registerCallback);
}
};
return Object.getOwnPropertyDescriptor(temp, "boundingClientRect").get;
@ -280,14 +255,7 @@
getterGenerator: function(checker){
const temp = {
get rootBounds(){
return checkerWrapper(checker, this, arguments, function(args, check){
const {prefs, notify, window, original} = check;
const originalValue = args.length?
original.apply(this, window.Array.from(args)):
original.call(this);
registerDOMRect(originalValue, notify, window, prefs);
return originalValue;
});
return checkerWrapper(checker, this, arguments, registerCallback);
}
};
return Object.getOwnPropertyDescriptor(temp, "rootBounds").get;
@ -295,15 +263,9 @@
}
];
function getStatus(obj, status, prefs){
status = Object.create(status);
status.active = prefs("protectDOMRect", status.url);
return status;
}
setProperties(scope.changedFunctions, scope.changedGetters, {
type: "readout",
getStatus: getStatus,
getStatus: getStatusByFlag("protectDOMRect"),
api: "domRect"
});
}());

View File

@ -12,7 +12,7 @@
scope = require.register("./modifiedHistoryAPI", {});
}
const {checkerWrapper} = require("./modifiedAPIFunctions");
const {checkerWrapper, setGetterProperties} = require("./modifiedAPIFunctions");
scope.changedGetters = [
{
@ -47,9 +47,9 @@
return status;
}
scope.changedGetters.forEach(function(changedGetter){
changedGetter.type = "readout";
changedGetter.getStatus = getStatus;
changedGetter.api = "history";
setGetterProperties(scope.changedGetters, {
type: "readout",
getStatus: getStatus,
api: "history"
});
}());

View File

@ -12,7 +12,7 @@
scope = require.register("./modifiedNavigatorAPI", {});
}
const {checkerWrapper} = require("./modifiedAPIFunctions");
const {checkerWrapper, setGetterProperties, getStatusByFlag} = require("./modifiedAPIFunctions");
const navigator = require("./navigator");
scope.changedGetters = navigator.allProperties.map(function(property){
@ -38,15 +38,9 @@
};
});
function getStatus(obj, status, prefs){
status = Object.create(status);
status.active = prefs("protectNavigator", status.url);
return status;
}
scope.changedGetters.forEach(function(changedGetter){
changedGetter.type = "readout";
changedGetter.getStatus = getStatus;
changedGetter.api = "navigator";
setGetterProperties(scope.changedGetters, {
type: "readout",
getStatus: getStatusByFlag("protectNavigator"),
api: "navigator"
});
}());

View File

@ -12,7 +12,7 @@
scope = require.register("./modifiedScreenAPI", {});
}
const {checkerWrapper} = require("./modifiedAPIFunctions");
const {checkerWrapper, setGetterProperties, getStatusByFlag} = require("./modifiedAPIFunctions");
const physical = {
width: Math.round(window.screen.width * window.devicePixelRatio),
@ -280,15 +280,9 @@
},
];
function getStatus(obj, status, prefs){
status = Object.create(status);
status.active = prefs("protectScreen", status.url);
return status;
}
scope.changedGetters.forEach(function(changedGetter){
changedGetter.type = "readout";
changedGetter.getStatus = getStatus;
changedGetter.api = "screen";
setGetterProperties(scope.changedGetters, {
type: "readout",
getStatus: getStatusByFlag("protectScreen"),
api: "screen"
});
}());

View File

@ -12,7 +12,7 @@
scope = require.register("./modifiedWindowAPI", {});
}
const {checkerWrapper} = require("./modifiedAPIFunctions");
const {checkerWrapper, setGetterProperties, getStatusByFlag} = require("./modifiedAPIFunctions");
const windowNames = new WeakMap();
scope.changedGetters = [
@ -23,10 +23,7 @@
const temp = {
get opener(){
return checkerWrapper(checker, this, arguments, function(args, check){
const {prefs, notify, window, original} = check;
if (!prefs("protectWindow", window.location)){
return original.apply(this, window.Array.from(args));
}
const {notify, window, original} = check;
const originalOpener = original.apply(this, window.Array.from(args));
if (originalOpener !== null){
notify("fakedWindowReadout");
@ -51,10 +48,7 @@
const temp = {
get name(){
return checkerWrapper(checker, this, arguments, function(args, check){
const {prefs, notify, window, original} = check;
if (!prefs("protectWindow", window.location)){
return original.apply(this, window.Array.from(args));
}
const {notify, window, original} = check;
const originalName = original.apply(this, window.Array.from(args));
const returnedName = windowNames.get(window) || "";
if (originalName !== returnedName){
@ -78,15 +72,9 @@
}
];
function getStatus(obj, status, prefs){
status = Object.create(status);
status.active = prefs("protectWindow", status.url);
return status;
}
scope.changedGetters.forEach(function(changedGetter){
changedGetter.type = "readout";
changedGetter.getStatus = getStatus;
changedGetter.api = "window";
setGetterProperties(scope.changedGetters, {
type: "readout",
getStatus: getStatusByFlag("protectWindow"),
api: "window"
});
}());

View File

@ -15,10 +15,9 @@
const settingDefinitions = require("./settingDefinitions");
scope.validVersions = [undefined, 0.1, 0.2, 0.3, 0.4, 0.5];
scope.validVersions = [undefined, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6];
scope.transitions = {
// eslint-disable-next-line no-unused-vars
"": function(oldStorage){
"": function(){
return {
storageVersion: 0.6
};