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,9 +394,11 @@
){ ){
const protectedAPIFeatures = prefs("protectedAPIFeatures"); const protectedAPIFeatures = prefs("protectedAPIFeatures");
if ( if (
!protectedAPIFeatures.hasOwnProperty(name + " @ " + changedGetter.api) || protectedAPIFeatures.hasOwnProperty(name + " @ " + changedGetter.api) &&
protectedAPIFeatures[name + " @ " + changedGetter.api] !protectedAPIFeatures[name + " @ " + changedGetter.api]
){ ){
return;
}
switch (functionStatus.mode){ switch (functionStatus.mode){
case "ask": case "block": case "fake": case "ask": case "block": case "fake":
descriptor.value = changedGetter.valueGenerator({ descriptor.value = changedGetter.valueGenerator({
@ -417,7 +419,6 @@
break; break;
} }
} }
}
else { else {
logging.error("Try to fake non getter property:", changedGetter); logging.error("Try to fake non getter property:", changedGetter);
} }

View File

@ -8,7 +8,7 @@
const colorStatistics = require("./colorStatistics"); const colorStatistics = require("./colorStatistics");
const logging = require("./logging"); const logging = require("./logging");
const extension = require("./extension"); const extension = require("./extension");
const {copyCanvasToWebgl} = require("./webgl"); const webgl = require("./webgl");
const {checkerWrapper} = require("./modifiedAPIFunctions"); const {checkerWrapper} = require("./modifiedAPIFunctions");
let randomSupply = null; let randomSupply = null;
@ -182,98 +182,10 @@
scope.setRandomSupply = function(supply){ scope.setRandomSupply = function(supply){
randomSupply = 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(); const canvasContextType = new WeakMap();
// changed functions and their fakes // changed functions and their fakes
scope.changedFunctions = { scope.changedFunctions = {
@ -592,7 +504,7 @@
if (!this || canvasSizeShouldBeFaked(this.canvas, prefs)){ if (!this || canvasSizeShouldBeFaked(this.canvas, prefs)){
notify("fakedReadout"); notify("fakedReadout");
const fakeCanvas = getFakeCanvas(window, this.canvas, prefs); const fakeCanvas = getFakeCanvas(window, this.canvas, prefs);
const {context} = copyCanvasToWebgl( const {context} = webgl.copyCanvasToWebgl(
window, window,
fakeCanvas, fakeCanvas,
this instanceof window.WebGLRenderingContext? "webgl": "webgl2" this instanceof window.WebGLRenderingContext? "webgl": "webgl2"
@ -616,51 +528,13 @@
}, },
object: ["WebGLRenderingContext", "WebGL2RenderingContext"], object: ["WebGLRenderingContext", "WebGL2RenderingContext"],
fakeGenerator: function(checker){ fakeGenerator: function(checker){
Object.keys(parameterChangeDefinition).forEach(function(parameterName){ webgl.initializeParameterDefinitions();
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};
};
}
});
return function getParameter(pname){ return function getParameter(pname){
return checkerWrapper(checker, this, arguments, function(args, check){ return checkerWrapper(checker, this, arguments, function(args, check){
const {prefs, notify, window, original} = check; const {prefs, notify, window, original} = check;
const originalValue = original.apply(this, window.Array.from(args)); const originalValue = original.apply(this, window.Array.from(args));
if (parameterChangeDefinition[pname]){ if (webgl.parameterChangeDefinition[pname]){
const definition = parameterChangeDefinition[pname]; const definition = webgl.parameterChangeDefinition[pname];
const {value, faked} = definition.fake(originalValue, window, prefs); const {value, faked} = definition.fake(originalValue, window, prefs);
if (faked){ if (faked){
notify("fakedReadout"); notify("fakedReadout");

View File

@ -206,22 +206,9 @@
objectGetters: [function(window){return window.MediaQueryList && window.MediaQueryList.prototype;}], objectGetters: [function(window){return window.MediaQueryList && window.MediaQueryList.prototype;}],
name: "matches", name: "matches",
getterGenerator: function(checker){ getterGenerator: function(checker){
const temp = { function getAlteredMedia(originalMedia, prefs, window){
get matches(){
return checkerWrapper(checker, this, arguments, function(args, check){
const {prefs, notify, window, original} = check;
const originalValue = original.apply(this, window.Array.from(args));
const screenSize = prefs("screenSize", window.location);
if (
(
screenSize.match(/\s*\d+\s*x\s*\d+\s*$/) ||
prefs("fakeMinimalScreenSize", window.location)
) &&
this.media.match(/device-(width|height)/)
){
const dimensions = getScreenDimensions(prefs, window); const dimensions = getScreenDimensions(prefs, window);
const originalMedia = this.media; return originalMedia.replace(
const alteredMedia = this.media.replace(
/\(\s*(?:(min|max)-)?device-(width|height):\s+(\d+\.?\d*)px\s*\)/, /\(\s*(?:(min|max)-)?device-(width|height):\s+(\d+\.?\d*)px\s*\)/,
function(m, type, dimension, value){ function(m, type, dimension, value){
value = parseFloat(value); value = parseFloat(value);
@ -262,6 +249,22 @@
) + "px)"; ) + "px)";
} }
); );
}
const temp = {
get matches(){
return checkerWrapper(checker, this, arguments, function(args, check){
const {prefs, notify, window, original} = check;
const originalValue = original.apply(this, window.Array.from(args));
const screenSize = prefs("screenSize", window.location);
if (
(
screenSize.match(/\s*\d+\s*x\s*\d+\s*$/) ||
prefs("fakeMinimalScreenSize", window.location)
) &&
this.media.match(/device-(width|height)/)
){
const originalMedia = this.media;
const alteredMedia = getAlteredMedia(originalMedia, prefs, window);
if (alteredMedia !== originalMedia){ if (alteredMedia !== originalMedia){
const alteredQuery = window.matchMedia(alteredMedia); const alteredQuery = window.matchMedia(alteredMedia);
const fakedValue = original.call(alteredQuery); const fakedValue = original.call(alteredQuery);

View File

@ -165,7 +165,9 @@
}; };
scope.initializeUrlContainer = function(eventHandler){ scope.initializeUrlContainer = function(eventHandler){
if (scope.urlContainer){ if (!scope.urlContainer){
return;
}
scope.urlContainer.on(function({newValue, oldValue}){ scope.urlContainer.on(function({newValue, oldValue}){
newValue.forEach(function(urlSetting){ newValue.forEach(function(urlSetting){
let regExp; let regExp;
@ -235,6 +237,5 @@
}); });
}); });
}); });
}
}; };
}()); }());

View File

@ -16,7 +16,10 @@
scope.getMessages = function(settingDefinition){ scope.getMessages = function(settingDefinition){
const messages = []; const messages = [];
if (settingDefinition){ if (!settingDefinition){
return messages;
}
messages.push(settingDefinition.name + "_title"); messages.push(settingDefinition.name + "_title");
messages.push(settingDefinition.name + "_description"); messages.push(settingDefinition.name + "_description");
if (settingDefinition.urlSpecific){ if (settingDefinition.urlSpecific){
@ -31,15 +34,13 @@
} }
if (settingDefinition.inputs){ if (settingDefinition.inputs){
settingDefinition.inputs.forEach(function(input){ settingDefinition.inputs.forEach(function(input){
if (input){ if (input && input.options){
if (input.options){
input.options.forEach(function(option){ input.options.forEach(function(option){
if (option !== null){ if (option !== null){
messages.push(input.name + "_options." + option); messages.push(input.name + "_options." + option);
} }
}); });
} }
}
}); });
} }
if (settingDefinition.action){ if (settingDefinition.action){
@ -50,7 +51,6 @@
messages.push(action.name + "_label"); messages.push(action.name + "_label");
}); });
} }
}
return messages; return messages;
}; };

View File

@ -109,4 +109,142 @@
return {webGlCanvas, context}; 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;
}()); }());