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

Code cleanup: reduce block nesting

This commit is contained in:
kkapsner 2019-12-29 00:18:05 +01:00
parent 10413a89c3
commit abdb95b815
6 changed files with 310 additions and 293 deletions

View File

@ -394,28 +394,29 @@
){
const protectedAPIFeatures = prefs("protectedAPIFeatures");
if (
!protectedAPIFeatures.hasOwnProperty(name + " @ " + changedGetter.api) ||
protectedAPIFeatures[name + " @ " + changedGetter.api]
protectedAPIFeatures.hasOwnProperty(name + " @ " + changedGetter.api) &&
!protectedAPIFeatures[name + " @ " + changedGetter.api]
){
switch (functionStatus.mode){
case "ask": case "block": case "fake":
descriptor.value = changedGetter.valueGenerator({
mode: functionStatus.mode,
original: descriptor.value,
notify: function notifyCallback(messageId){
notify({
url: getURL(windowToProcess),
errorStack: (new Error()).stack,
messageId,
timestamp: new Date(),
functionName: name,
api: changedGetter.api
});
}
});
Object.defineProperty(object, name, descriptor);
break;
}
return;
}
switch (functionStatus.mode){
case "ask": case "block": case "fake":
descriptor.value = changedGetter.valueGenerator({
mode: functionStatus.mode,
original: descriptor.value,
notify: function notifyCallback(messageId){
notify({
url: getURL(windowToProcess),
errorStack: (new Error()).stack,
messageId,
timestamp: new Date(),
functionName: name,
api: changedGetter.api
});
}
});
Object.defineProperty(object, name, descriptor);
break;
}
}
else {

View File

@ -8,7 +8,7 @@
const colorStatistics = require("./colorStatistics");
const logging = require("./logging");
const extension = require("./extension");
const {copyCanvasToWebgl} = require("./webgl");
const webgl = require("./webgl");
const {checkerWrapper} = require("./modifiedAPIFunctions");
let randomSupply = null;
@ -182,98 +182,10 @@
scope.setRandomSupply = function(supply){
randomSupply = supply;
webgl.setRandomSupply(supply);
};
function getNumber(originalValue, max, index, window){
const bitLength = Math.floor(Math.log2(max) + 1);
const rng = randomSupply.getBitRng(bitLength, window);
let value = 0;
for (let i = 0; i < bitLength; i += 1){
value <<= 1;
value ^= rng(originalValue, index + i);
}
return value;
}
const parameterFakeTypes = {
decimal: function(originalValue, definition, window){
const int = Math.floor(originalValue);
if (int !== originalValue){
const decimal = originalValue - int;
const rng = randomSupply.getRng(1, window);
const newDecimal = decimal * (rng(definition.pname) / 0xFFFFFFFF);
return int + newDecimal;
}
else {
return originalValue;
}
},
shift: function(originalValue, definition, window){
const value = getNumber(originalValue, definition.max, definition.pname, window);
return originalValue >>> value;
},
"-": function(originalValue, definition, window){
const value = getNumber(originalValue, definition.max, definition.pname, window) *
(definition.factor || 1);
if (value > originalValue){
return 0;
}
return originalValue - value;
}
};
const parameterChangeDefinition = {
2928: {name: "DEPTH_RANGE", type: "decimal", isArray: true},
3379: {name: "MAX_TEXTURE_SIZE", type: "shift", max: 1},
3386: {name: "MAX_VIEWPORT_DIMS", type: "shift", max: 1, isArray: true},
32883: {name: "MAX_3D_TEXTURE_SIZE", type: "shift", max: 1},
33000: {name: "MAX_ELEMENTS_VERTICES", type: "-", max: 3, factor: 50},
33001: {name: "MAX_ELEMENTS_INDICES", type: "-", max: 3, factor: 50},
33901: {name: "ALIASED_POINT_SIZE_RANGE", type: "decimal", isArray: true},
33902: {name: "ALIASED_LINE_WIDTH_RANGE", type: "decimal", isArray: true},
34024: {name: "MAX_RENDERBUFFER_SIZE", type: "shift", max: 1},
34045: {name: "MAX_TEXTURE_LOD_BIAS", type: "-", max: 1, factor: 1},
34076: {name: "MAX_CUBE_MAP_TEXTURE_SIZE", type: "shift", max: 1},
34921: {name: "MAX_VERTEX_ATTRIBS", type: "shift", max: 1},
34930: {name: "MAX_TEXTURE_IMAGE_UNITS", type: "shift", max: 1},
35071: {name: "MAX_ARRAY_TEXTURE_LAYERS", type: "shift", max: 1},
35371: {name: "MAX_VERTEX_UNIFORM_BLOCKS", type: "-", max: 1, factor: 1},
35373: {name: "MAX_FRAGMENT_UNIFORM_BLOCKS", type: "-", max: 1, factor: 1},
35374: {name: "MAX_COMBINED_UNIFORM_BLOCKS", type: "-", max: 3, factor: 1},
35375: {name: "MAX_UNIFORM_BUFFER_BINDINGS", type: "-", max: 3, factor: 1},
35376: {name: "MAX_UNIFORM_BLOCK_SIZE", type: "shift", max: 1},
35377: {name: "MAX_COMBINED_VERTEX_UNIFORM_COMPONENTS", type: "-", max: 7, factor: 10},
35379: {name: "MAX_COMBINED_FRAGMENT_UNIFORM_COMPONENTS", type: "-", max: 7, factor: 10},
35657: {name: "MAX_FRAGMENT_UNIFORM_COMPONENTS", type: "shift", max: 1},
35658: {name: "MAX_VERTEX_UNIFORM_COMPONENTS", type: "shift", max: 1},
35659: {name: "MAX_VARYING_COMPONENTS", type: "shift", max: 1},
35660: {name: "MAX_VERTEX_TEXTURE_IMAGE_UNITS", type: "shift", max: 1},
35661: {name: "MAX_COMBINED_TEXTURE_IMAGE_UNITS", type: "-", max: 1, factor: 2},
35968: {name: "MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS", type: "shift", max: 1},
35978: {name: "MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS", type: "shift", max: 1},
36203: {name: "MAX_ELEMENT_INDEX", type: "-", max: 15, factor: 1},
36347: {name: "MAX_VERTEX_UNIFORM_VECTORS", type: "shift", max: 1},
36348: {name: "MAX_VARYING_VECTORS", type: "shift", max: 1},
36349: {name: "MAX_FRAGMENT_UNIFORM_VECTORS", type: "shift", max: 1},
37154: {name: "MAX_VERTEX_OUTPUT_COMPONENTS", type: "shift", max: 1},
37157: {name: "MAX_FRAGMENT_INPUT_COMPONENTS", type: "shift", max: 1},
7936: {name: "VENDOR", fake: function(originalValue, window, prefs){
const settingValue = prefs("webGLVendor") || originalValue;
return {value: settingValue, faked: settingValue === originalValue};
}},
7937: {name: "RENDERER", fake: function(originalValue, window, prefs){
const settingValue = prefs("webGLRenderer") || originalValue;
return {value: settingValue, faked: settingValue === originalValue};
}},
37445: {name: "UNMASKED_VENDOR_WEBGL", fake: function(originalValue, window, prefs){
const settingValue = prefs("webGLUnmaskedVendor") || originalValue;
return {value: settingValue, faked: settingValue === originalValue};
}},
37446: {name: "UNMASKED_RENDERER_WEBGL", fake: function(originalValue, window, prefs){
const settingValue = prefs("webGLUnmaskedRenderer") || originalValue;
return {value: settingValue, faked: settingValue === originalValue};
}}
};
const canvasContextType = new WeakMap();
// changed functions and their fakes
scope.changedFunctions = {
@ -592,7 +504,7 @@
if (!this || canvasSizeShouldBeFaked(this.canvas, prefs)){
notify("fakedReadout");
const fakeCanvas = getFakeCanvas(window, this.canvas, prefs);
const {context} = copyCanvasToWebgl(
const {context} = webgl.copyCanvasToWebgl(
window,
fakeCanvas,
this instanceof window.WebGLRenderingContext? "webgl": "webgl2"
@ -616,51 +528,13 @@
},
object: ["WebGLRenderingContext", "WebGL2RenderingContext"],
fakeGenerator: function(checker){
Object.keys(parameterChangeDefinition).forEach(function(parameterName){
const definition = parameterChangeDefinition[parameterName];
definition.pname = parameterName;
if (!definition.fake){
definition.fake = definition.isArray?
function fake(originalValue, window){
let faked = false;
let fakedValue = [];
for (let i = 0; i < originalValue.length; i += 1){
fakedValue[i] = parameterFakeTypes[this.type](originalValue[i], this, window);
faked |= originalValue[i] === fakedValue[i];
originalValue[i] = fakedValue[i];
}
this.fake = function(originalValue){
if (faked){
for (let i = 0; i < originalValue.length; i += 1){
originalValue[i] = fakedValue[i];
}
}
return {
value: originalValue,
faked
};
};
return {
value: originalValue,
faked
};
}:
function fake(originalValue, window){
let value = parameterFakeTypes[this.type](originalValue, this, window);
let faked = value === originalValue;
this.fake = function(){
return {value, faked};
};
return {value, faked};
};
}
});
webgl.initializeParameterDefinitions();
return function getParameter(pname){
return checkerWrapper(checker, this, arguments, function(args, check){
const {prefs, notify, window, original} = check;
const originalValue = original.apply(this, window.Array.from(args));
if (parameterChangeDefinition[pname]){
const definition = parameterChangeDefinition[pname];
if (webgl.parameterChangeDefinition[pname]){
const definition = webgl.parameterChangeDefinition[pname];
const {value, faked} = definition.fake(originalValue, window, prefs);
if (faked){
notify("fakedReadout");

View File

@ -206,6 +206,50 @@
objectGetters: [function(window){return window.MediaQueryList && window.MediaQueryList.prototype;}],
name: "matches",
getterGenerator: function(checker){
function getAlteredMedia(originalMedia, prefs, window){
const dimensions = getScreenDimensions(prefs, window);
return originalMedia.replace(
/\(\s*(?:(min|max)-)?device-(width|height):\s+(\d+\.?\d*)px\s*\)/,
function(m, type, dimension, value){
value = parseFloat(value);
let newCompareValue = value;
switch (type){
case "min":
if (value <= dimensions[dimension]){
newCompareValue = 0;
}
else {
newCompareValue = 2 * physical[dimension];
}
break;
case "max":
if (value >= dimensions[dimension]){
newCompareValue = 2 * physical[dimension];
}
else {
newCompareValue = 0;
}
break;
default:
if (
Math.round(value * 100) ===
Math.round(dimensions[dimension] * 100)
){
newCompareValue = physical[dimension];
}
else {
newCompareValue = 0;
}
}
return "(" + (type? type + "-": "") +
"device-" + dimension + ": " +
(
newCompareValue /
window.devicePixelRatio
) + "px)";
}
);
}
const temp = {
get matches(){
return checkerWrapper(checker, this, arguments, function(args, check){
@ -219,49 +263,8 @@
) &&
this.media.match(/device-(width|height)/)
){
const dimensions = getScreenDimensions(prefs, window);
const originalMedia = this.media;
const alteredMedia = this.media.replace(
/\(\s*(?:(min|max)-)?device-(width|height):\s+(\d+\.?\d*)px\s*\)/,
function(m, type, dimension, value){
value = parseFloat(value);
let newCompareValue = value;
switch (type){
case "min":
if (value <= dimensions[dimension]){
newCompareValue = 0;
}
else {
newCompareValue = 2 * physical[dimension];
}
break;
case "max":
if (value >= dimensions[dimension]){
newCompareValue = 2 * physical[dimension];
}
else {
newCompareValue = 0;
}
break;
default:
if (
Math.round(value * 100) ===
Math.round(dimensions[dimension] * 100)
){
newCompareValue = physical[dimension];
}
else {
newCompareValue = 0;
}
}
return "(" + (type? type + "-": "") +
"device-" + dimension + ": " +
(
newCompareValue /
window.devicePixelRatio
) + "px)";
}
);
const alteredMedia = getAlteredMedia(originalMedia, prefs, window);
if (alteredMedia !== originalMedia){
const alteredQuery = window.matchMedia(alteredMedia);
const fakedValue = original.call(alteredQuery);

View File

@ -165,76 +165,77 @@
};
scope.initializeUrlContainer = function(eventHandler){
if (scope.urlContainer){
scope.urlContainer.on(function({newValue, oldValue}){
newValue.forEach(function(urlSetting){
let regExp;
const domain = !!urlSetting.url.match(/^[A-Za-z0-9_.-]+$/);
if (domain){
regExp = new RegExp(
"(?:^|\\.)" + urlSetting.url.replace(/([\\+*?[^\]$(){}=!|.])/g, "\\$1") + "\\.?$",
"i"
);
if (!scope.urlContainer){
return;
}
scope.urlContainer.on(function({newValue, oldValue}){
newValue.forEach(function(urlSetting){
let regExp;
const domain = !!urlSetting.url.match(/^[A-Za-z0-9_.-]+$/);
if (domain){
regExp = new RegExp(
"(?:^|\\.)" + urlSetting.url.replace(/([\\+*?[^\]$(){}=!|.])/g, "\\$1") + "\\.?$",
"i"
);
}
else {
regExp = new RegExp(urlSetting.url, "i");
}
const match = function(url){
if (!url){
return false;
}
else if (
url instanceof String ||
(typeof url) === "string"
){
return url === urlSetting.url;
}
else if (domain){
return (url.hostname || "").match(regExp);
}
else {
regExp = new RegExp(urlSetting.url, "i");
return url.href.match(regExp);
}
const match = function(url){
if (!url){
return false;
}
else if (
url instanceof String ||
(typeof url) === "string"
){
return url === urlSetting.url;
}
else if (domain){
return (url.hostname || "").match(regExp);
}
else {
return url.href.match(regExp);
}
};
Object.defineProperty(
urlSetting,
"match",
{
enumerable: false,
writable: true,
configurable: true,
value: match
}
);
});
const newUrls = newValue.map(function(entry){return entry.url;});
const oldUrls = oldValue.map(function(entry){return entry.url;});
const matching = {};
newUrls.forEach(function(url, i){
matching[url] = {new: i, old: oldUrls.indexOf(url)};
});
oldUrls.forEach(function(url, i){
if (!matching[url]){
matching[url] = {new: -1, old: i};
};
Object.defineProperty(
urlSetting,
"match",
{
enumerable: false,
writable: true,
configurable: true,
value: match
}
);
});
const newUrls = newValue.map(function(entry){return entry.url;});
const oldUrls = oldValue.map(function(entry){return entry.url;});
const matching = {};
newUrls.forEach(function(url, i){
matching[url] = {new: i, old: oldUrls.indexOf(url)};
});
oldUrls.forEach(function(url, i){
if (!matching[url]){
matching[url] = {new: -1, old: i};
}
});
Object.keys(matching).forEach(function(url){
const oldEntry = oldValue[matching[url].old] || {};
const newEntry = newValue[matching[url].new] || {};
scope.urlContainer.entries.forEach(function(settingDefinition){
const name = settingDefinition.name;
const oldValue = oldEntry[name];
const newValue = newEntry[name];
if (oldValue !== newValue){
((eventHandler[name] || {})[url] || []).forEach(function(callback){
callback({name, newValue, oldValue, url});
});
}
});
Object.keys(matching).forEach(function(url){
const oldEntry = oldValue[matching[url].old] || {};
const newEntry = newValue[matching[url].new] || {};
scope.urlContainer.entries.forEach(function(settingDefinition){
const name = settingDefinition.name;
const oldValue = oldEntry[name];
const newValue = newEntry[name];
if (oldValue !== newValue){
((eventHandler[name] || {})[url] || []).forEach(function(callback){
callback({name, newValue, oldValue, url});
});
}
});
});
});
}
});
};
}());

View File

@ -16,40 +16,40 @@
scope.getMessages = function(settingDefinition){
const messages = [];
if (settingDefinition){
messages.push(settingDefinition.name + "_title");
messages.push(settingDefinition.name + "_description");
if (settingDefinition.urlSpecific){
messages.push(settingDefinition.name + "_urlSpecific");
}
if (settingDefinition.options){
settingDefinition.options.forEach(function(option){
if (option !== null){
messages.push(settingDefinition.name + "_options." + option);
}
});
}
if (settingDefinition.inputs){
settingDefinition.inputs.forEach(function(input){
if (input){
if (input.options){
input.options.forEach(function(option){
if (option !== null){
messages.push(input.name + "_options." + option);
}
});
if (!settingDefinition){
return messages;
}
messages.push(settingDefinition.name + "_title");
messages.push(settingDefinition.name + "_description");
if (settingDefinition.urlSpecific){
messages.push(settingDefinition.name + "_urlSpecific");
}
if (settingDefinition.options){
settingDefinition.options.forEach(function(option){
if (option !== null){
messages.push(settingDefinition.name + "_options." + option);
}
});
}
if (settingDefinition.inputs){
settingDefinition.inputs.forEach(function(input){
if (input && input.options){
input.options.forEach(function(option){
if (option !== null){
messages.push(input.name + "_options." + option);
}
}
});
}
if (settingDefinition.action){
messages.push(settingDefinition.name + "_label");
}
if (settingDefinition.actions){
settingDefinition.actions.forEach(function(action){
messages.push(action.name + "_label");
});
}
});
}
});
}
if (settingDefinition.action){
messages.push(settingDefinition.name + "_label");
}
if (settingDefinition.actions){
settingDefinition.actions.forEach(function(action){
messages.push(action.name + "_label");
});
}
return messages;
};

View File

@ -109,4 +109,142 @@
return {webGlCanvas, context};
};
let randomSupply = null;
scope.setRandomSupply = function(supply){
randomSupply = supply;
};
function getNumber(originalValue, max, index, window){
const bitLength = Math.floor(Math.log2(max) + 1);
const rng = randomSupply.getBitRng(bitLength, window);
let value = 0;
for (let i = 0; i < bitLength; i += 1){
value <<= 1;
value ^= rng(originalValue, index + i);
}
return value;
}
const parameterFakeTypes = {
decimal: function(originalValue, definition, window){
const int = Math.floor(originalValue);
if (int !== originalValue){
const decimal = originalValue - int;
const rng = randomSupply.getRng(1, window);
const newDecimal = decimal * (rng(definition.pname) / 0xFFFFFFFF);
return int + newDecimal;
}
else {
return originalValue;
}
},
shift: function(originalValue, definition, window){
const value = getNumber(originalValue, definition.max, definition.pname, window);
return originalValue >>> value;
},
"-": function(originalValue, definition, window){
const value = getNumber(originalValue, definition.max, definition.pname, window) *
(definition.factor || 1);
if (value > originalValue){
return 0;
}
return originalValue - value;
}
};
const parameterChangeDefinition = {
2928: {name: "DEPTH_RANGE", type: "decimal", isArray: true},
3379: {name: "MAX_TEXTURE_SIZE", type: "shift", max: 1},
3386: {name: "MAX_VIEWPORT_DIMS", type: "shift", max: 1, isArray: true},
32883: {name: "MAX_3D_TEXTURE_SIZE", type: "shift", max: 1},
33000: {name: "MAX_ELEMENTS_VERTICES", type: "-", max: 3, factor: 50},
33001: {name: "MAX_ELEMENTS_INDICES", type: "-", max: 3, factor: 50},
33901: {name: "ALIASED_POINT_SIZE_RANGE", type: "decimal", isArray: true},
33902: {name: "ALIASED_LINE_WIDTH_RANGE", type: "decimal", isArray: true},
34024: {name: "MAX_RENDERBUFFER_SIZE", type: "shift", max: 1},
34045: {name: "MAX_TEXTURE_LOD_BIAS", type: "-", max: 1, factor: 1},
34076: {name: "MAX_CUBE_MAP_TEXTURE_SIZE", type: "shift", max: 1},
34921: {name: "MAX_VERTEX_ATTRIBS", type: "shift", max: 1},
34930: {name: "MAX_TEXTURE_IMAGE_UNITS", type: "shift", max: 1},
35071: {name: "MAX_ARRAY_TEXTURE_LAYERS", type: "shift", max: 1},
35371: {name: "MAX_VERTEX_UNIFORM_BLOCKS", type: "-", max: 1, factor: 1},
35373: {name: "MAX_FRAGMENT_UNIFORM_BLOCKS", type: "-", max: 1, factor: 1},
35374: {name: "MAX_COMBINED_UNIFORM_BLOCKS", type: "-", max: 3, factor: 1},
35375: {name: "MAX_UNIFORM_BUFFER_BINDINGS", type: "-", max: 3, factor: 1},
35376: {name: "MAX_UNIFORM_BLOCK_SIZE", type: "shift", max: 1},
35377: {name: "MAX_COMBINED_VERTEX_UNIFORM_COMPONENTS", type: "-", max: 7, factor: 10},
35379: {name: "MAX_COMBINED_FRAGMENT_UNIFORM_COMPONENTS", type: "-", max: 7, factor: 10},
35657: {name: "MAX_FRAGMENT_UNIFORM_COMPONENTS", type: "shift", max: 1},
35658: {name: "MAX_VERTEX_UNIFORM_COMPONENTS", type: "shift", max: 1},
35659: {name: "MAX_VARYING_COMPONENTS", type: "shift", max: 1},
35660: {name: "MAX_VERTEX_TEXTURE_IMAGE_UNITS", type: "shift", max: 1},
35661: {name: "MAX_COMBINED_TEXTURE_IMAGE_UNITS", type: "-", max: 1, factor: 2},
35968: {name: "MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS", type: "shift", max: 1},
35978: {name: "MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS", type: "shift", max: 1},
36203: {name: "MAX_ELEMENT_INDEX", type: "-", max: 15, factor: 1},
36347: {name: "MAX_VERTEX_UNIFORM_VECTORS", type: "shift", max: 1},
36348: {name: "MAX_VARYING_VECTORS", type: "shift", max: 1},
36349: {name: "MAX_FRAGMENT_UNIFORM_VECTORS", type: "shift", max: 1},
37154: {name: "MAX_VERTEX_OUTPUT_COMPONENTS", type: "shift", max: 1},
37157: {name: "MAX_FRAGMENT_INPUT_COMPONENTS", type: "shift", max: 1},
7936: {name: "VENDOR", fake: function(originalValue, window, prefs){
const settingValue = prefs("webGLVendor") || originalValue;
return {value: settingValue, faked: settingValue === originalValue};
}},
7937: {name: "RENDERER", fake: function(originalValue, window, prefs){
const settingValue = prefs("webGLRenderer") || originalValue;
return {value: settingValue, faked: settingValue === originalValue};
}},
37445: {name: "UNMASKED_VENDOR_WEBGL", fake: function(originalValue, window, prefs){
const settingValue = prefs("webGLUnmaskedVendor") || originalValue;
return {value: settingValue, faked: settingValue === originalValue};
}},
37446: {name: "UNMASKED_RENDERER_WEBGL", fake: function(originalValue, window, prefs){
const settingValue = prefs("webGLUnmaskedRenderer") || originalValue;
return {value: settingValue, faked: settingValue === originalValue};
}}
};
scope.initializeParameterDefinitions = function(){
Object.keys(parameterChangeDefinition).forEach(function(parameterName){
const definition = parameterChangeDefinition[parameterName];
definition.pname = parameterName;
if (!definition.fake){
definition.fake = definition.isArray?
function fake(originalValue, window){
let faked = false;
let fakedValue = [];
for (let i = 0; i < originalValue.length; i += 1){
fakedValue[i] = parameterFakeTypes[this.type](originalValue[i], this, window);
faked |= originalValue[i] === fakedValue[i];
originalValue[i] = fakedValue[i];
}
this.fake = function(originalValue){
if (faked){
for (let i = 0; i < originalValue.length; i += 1){
originalValue[i] = fakedValue[i];
}
}
return {
value: originalValue,
faked
};
};
return {
value: originalValue,
faked
};
}:
function fake(originalValue, window){
let value = parameterFakeTypes[this.type](originalValue, this, window);
let faked = value === originalValue;
this.fake = function(){
return {value, faked};
};
return {value, faked};
};
}
});
};
scope.parameterChangeDefinition = parameterChangeDefinition;
}());