From 32f9ea7447027690341e8f0a39fb6cf5f2933eb5 Mon Sep 17 00:00:00 2001 From: kkapsner Date: Tue, 10 Dec 2019 15:07:22 +0100 Subject: [PATCH] Remove code duplications and cleanup --- .eslintrc.json | 1 + lib/modifiedAPIFunctions.js | 8 ++++ lib/modifiedAudioAPI.js | 17 +++------ lib/modifiedDOMRectAPI.js | 76 ++++++++++--------------------------- lib/modifiedHistoryAPI.js | 10 ++--- lib/modifiedNavigatorAPI.js | 16 +++----- lib/modifiedScreenAPI.js | 16 +++----- lib/modifiedWindowAPI.js | 26 ++++--------- lib/settingsMigration.js | 5 +-- 9 files changed, 58 insertions(+), 117 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index e1d4de0..270708e 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -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"], diff --git a/lib/modifiedAPIFunctions.js b/lib/modifiedAPIFunctions.js index 775cc05..ab6d565 100644 --- a/lib/modifiedAPIFunctions.js +++ b/lib/modifiedAPIFunctions.js @@ -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]; diff --git a/lib/modifiedAudioAPI.js b/lib/modifiedAudioAPI.js index ad48af2..d85a3da 100644 --- a/lib/modifiedAudioAPI.js +++ b/lib/modifiedAudioAPI.js @@ -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" }); }()); \ No newline at end of file diff --git a/lib/modifiedDOMRectAPI.js b/lib/modifiedDOMRectAPI.js index dcbf126..983d23f 100644 --- a/lib/modifiedDOMRectAPI.js +++ b/lib/modifiedDOMRectAPI.js @@ -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" }); }()); \ No newline at end of file diff --git a/lib/modifiedHistoryAPI.js b/lib/modifiedHistoryAPI.js index cd1e1ea..0ab34b9 100644 --- a/lib/modifiedHistoryAPI.js +++ b/lib/modifiedHistoryAPI.js @@ -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" }); }()); \ No newline at end of file diff --git a/lib/modifiedNavigatorAPI.js b/lib/modifiedNavigatorAPI.js index 72b76a0..6cca5a4 100644 --- a/lib/modifiedNavigatorAPI.js +++ b/lib/modifiedNavigatorAPI.js @@ -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" }); }()); \ No newline at end of file diff --git a/lib/modifiedScreenAPI.js b/lib/modifiedScreenAPI.js index 112c279..152f488 100644 --- a/lib/modifiedScreenAPI.js +++ b/lib/modifiedScreenAPI.js @@ -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" }); }()); \ No newline at end of file diff --git a/lib/modifiedWindowAPI.js b/lib/modifiedWindowAPI.js index a388913..532abbd 100644 --- a/lib/modifiedWindowAPI.js +++ b/lib/modifiedWindowAPI.js @@ -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" }); }()); \ No newline at end of file diff --git a/lib/settingsMigration.js b/lib/settingsMigration.js index 0c6ed79..1f477b3 100644 --- a/lib/settingsMigration.js +++ b/lib/settingsMigration.js @@ -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 };