From dcb878392e919997c9e941e97dde7786262f8de5 Mon Sep 17 00:00:00 2001 From: kkapsner Date: Fri, 6 Oct 2017 16:06:31 +0200 Subject: [PATCH] Simplified require and further linting. --- .eslintrc.json | 5 ++-- lib/askForPermission.js | 5 +++- lib/callingStack.js | 8 ++----- lib/check.js | 6 ++--- lib/colorStatistics.js | 8 ++++++- lib/frame.js | 8 ++++--- lib/intercept.js | 52 ++++++++++++++++++++++------------------ lib/main.js | 7 ------ lib/modifiedAPI.js | 14 ++++++++--- lib/randomSupplies.js | 10 ++++---- lib/require.js | 18 -------------- lib/webgl.js | 6 +++-- pageAction/pageAction.js | 5 +++- test/detectionTest.js | 4 +++- test/test.js | 8 +++++-- 15 files changed, 86 insertions(+), 78 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 49bf150..3b3cea4 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -21,8 +21,9 @@ "comma-spacing": ["error", { "before": false, "after": true }], "constructor-super": "warn", "eqeqeq": "error", - "max-len": ["warn", 120], - "max-lines": ["warn", 200], + "max-len": ["warn", {"code": 120, "tabWidth": 4}], + "max-lines": ["warn", {"max": 400, "skipBlankLines": true, "skipComments": true}], + "max-params": ["warn", 4], "no-const-assign": "warn", "no-this-before-super": "warn", "no-undef": "error", diff --git a/lib/askForPermission.js b/lib/askForPermission.js index 5fe1201..a55f728 100644 --- a/lib/askForPermission.js +++ b/lib/askForPermission.js @@ -56,7 +56,10 @@ if (inDOM){ canvas.scrollIntoView(); var rect = canvas.getBoundingClientRect(); - var foundEl = window.document.elementFromPoint(rect.left + rect.width / 2, rect.top + rect.height / 2); + var foundEl = window.document.elementFromPoint( + rect.left + rect.width / 2, + rect.top + rect.height / 2 + ); visible = (foundEl === canvas); } Object.defineProperty(this, "visible", {value: visible}); diff --git a/lib/callingStack.js b/lib/callingStack.js index 7ced11c..a76ddf3 100644 --- a/lib/callingStack.js +++ b/lib/callingStack.js @@ -13,14 +13,10 @@ scope = window.scope.callingStack; } - const preferences = require("sdk/simple-prefs"); - const prefs = preferences.prefs; - // Translation - var translate = require("sdk/l10n").get; var _ = function(name, replace, translateAPI){ if (!translateAPI){ - translateAPI = translate; + translateAPI = browser.i18n.getMessage; } var str = translateAPI(name) || name; @@ -76,7 +72,7 @@ toString: function(translateAPI){ var msg = ""; msg += "\n\n" + _("sourceOutput", undefined, translateAPI) + ": "; - if (prefs.showCompleteCallingStack){ + if (settings.showCompleteCallingStack){ msg += callers.reduce(function(stack, c){ return stack + "\n\t" + _("stackEntryOutput", c, translateAPI); }, ""); diff --git a/lib/check.js b/lib/check.js index a9a1c68..5f22877 100644 --- a/lib/check.js +++ b/lib/check.js @@ -14,13 +14,11 @@ } const lists = require("./lists"); - const preferences = require("sdk/simple-prefs"); - const prefs = preferences.prefs; const {parseErrorStack} = require("./callingStack"); const logging = require("./logging"); scope.check = function check({url, errorStack}){ - var match = checkBoth(errorStack, url, prefs.blockMode).match( + var match = checkBoth(errorStack, url, settings.blockMode).match( /^(block|allow|fake|ask)(|Readout|Everything|Context|Input|Internal)$/ ); if (match){ @@ -41,7 +39,7 @@ }; function checkBoth(errorStack, url, blockMode){ - if (prefs.enableStackList && errorStack && checkStack(errorStack)){ + if (settings.enableStackList && errorStack && checkStack(errorStack)){ return "allow"; } else { diff --git a/lib/colorStatistics.js b/lib/colorStatistics.js index 7758419..7f33ad1 100644 --- a/lib/colorStatistics.js +++ b/lib/colorStatistics.js @@ -27,7 +27,13 @@ var index = String.fromCharCode(r, g, b, a); var color = this.colors[index]; if (!color){ - color = {index, color: [r, g, b, a], count: 0, previousColor: this.minBoundary, nextColor: this.minBoundary.nextColor}; + color = { + index, + color: [r, g, b, a], + count: 0, + previousColor: this.minBoundary, + nextColor: this.minBoundary.nextColor + }; this.numberOfColors += 1; this.minBoundary.nextColor = color; color.nextColor.previousColor = color; diff --git a/lib/frame.js b/lib/frame.js index 5d846f5..cc134a7 100644 --- a/lib/frame.js +++ b/lib/frame.js @@ -34,10 +34,9 @@ return true; } } - const _ = require("sdk/l10n").get; function askWrapper(data){ return ask(data, { - _, + _: browser.i18n.getMessage, prefs }); } @@ -159,7 +158,10 @@ if (data["canvasBlocker-unload"]){ enabled = false; } - if (data.hasOwnProperty("canvasBlocker-sendNotifications") && data["canvasBlocker-sendNotifications"] === tabId){ + if ( + data.hasOwnProperty("canvasBlocker-sendNotifications") && + data["canvasBlocker-sendNotifications"] === tabId + ){ notice("sending notifications:", notifications); browser.runtime.sendMessage({ sender: tabId, diff --git a/lib/intercept.js b/lib/intercept.js index 726db2e..632a756 100644 --- a/lib/intercept.js +++ b/lib/intercept.js @@ -18,7 +18,6 @@ setRandomSupply(randomSupplies.nonPersistent); var apiNames = Object.keys(changedFunctions); var undef; - var exportFunction = require("chrome").Cu.exportFunction; function setRandomSupplyByType(type){ switch (type){ case "persistent": @@ -86,6 +85,29 @@ } var funcStatus = changedFunction.getStatus(this, siteStatus); + function notifyCallback(messageId){ + notify({ + url, + errorStack: error.stack, + messageId, + timestamp: new Date(), + functionName: name, + dataURL: + prefs("storeImageForInspection") && + prefs("showNotifications")? + ( + this instanceof HTMLCanvasElement? + this.toDataURL(): + ( + this.canvas instanceof HTMLCanvasElement? + this.canvas.toDataURL(): + false + ) + ): + false + }); + } + if (funcStatus.active){ if (funcStatus.mode === "ask"){ funcStatus.mode = ask({ @@ -100,28 +122,12 @@ return original; case "fake": setRandomSupplyByType(prefs("rng")); - var fake = changedFunction.fakeGenerator(prefs, function(messageId){ - notify({ - url, - errorStack: error.stack, - messageId, - timestamp: new Date(), - functionName: name, - dataURL: - prefs("storeImageForInspection") && - prefs("showNotifications")? - ( - this instanceof HTMLCanvasElement? - this.toDataURL(): - ( - this.canvas instanceof HTMLCanvasElement? - this.canvas.toDataURL(): - false - ) - ): - false - }); - }, window, original); + var fake = changedFunction.fakeGenerator( + prefs, + notifyCallback, + window, + original + ); switch (fake){ case true: return original; diff --git a/lib/main.js b/lib/main.js index b909bc0..e303ca4 100644 --- a/lib/main.js +++ b/lib/main.js @@ -143,13 +143,6 @@ }); }); - // warning("TODO: register unload events - do not know how - there seems to be no way with a WebExtension"); - // old code - // const {when: unload} = require("sdk/system/unload"); - // unload(function(){ - // processes.port.emit("canvasBlocker-unload"); - // }); - browser.runtime.onInstalled.addListener(function(){ message("CanvasBlocker installed"); browser.storage.local.get("storageVersion").then(function(data){ diff --git a/lib/modifiedAPI.js b/lib/modifiedAPI.js index 4550a78..3d89667 100644 --- a/lib/modifiedAPI.js +++ b/lib/modifiedAPI.js @@ -34,7 +34,11 @@ source = new window.wrappedJSObject.ImageData(0, 0); } else if (context instanceof window.CanvasRenderingContext2D){ - imageData = window.CanvasRenderingContext2D.prototype.getImageData.call(context, 0, 0, context.canvas.width, context.canvas.height); + imageData = window.CanvasRenderingContext2D.prototype.getImageData.call( + context, + 0, 0, + context.canvas.width, context.canvas.height + ); source = imageData.data; } else { @@ -365,11 +369,15 @@ }, object: ["WebGLRenderingContext", "WebGL2RenderingContext"], fakeGenerator: function(prefs, notify, window, original){ - return function readPixels(x, y, width, height, format, type, pixels){ + return function readPixels(x, y, width, height, format, type, pixels){ // eslint-disable-line max-params if (!this || canvasSizeShouldBeFaked(this.canvas, prefs)){ notify.call(this, "fakedReadout"); var fakeCanvas = getFakeCanvas(window, this.canvas, prefs); - var {context} = copyCanvasToWebgl(window, fakeCanvas, this instanceof window.WebGLRenderingContext? "webgl": "webgl2"); + var {context} = copyCanvasToWebgl( + window, + fakeCanvas, + this instanceof window.WebGLRenderingContext? "webgl": "webgl2" + ); return original.apply(context, window.Array.from(arguments)); } else { diff --git a/lib/randomSupplies.js b/lib/randomSupplies.js index e49b433..dc53014 100644 --- a/lib/randomSupplies.js +++ b/lib/randomSupplies.js @@ -44,7 +44,9 @@ // create the (sub-)domains random numbers if not existing persistentRnd[domain] = new Uint8Array(128); window.crypto.getRandomValues(persistentRnd[domain]); - browser.runtime.sendMessage({"canvasBlocker-new-domain-rnd": {domain, rnd: Array.from(persistentRnd[domain])}}); + browser.runtime.sendMessage({ + "canvasBlocker-new-domain-rnd": {domain, rnd: Array.from(persistentRnd[domain])} + }); } return persistentRnd[domain]; }; @@ -75,7 +77,7 @@ }, getPixelRng: function(length, window, ignoredColors){ var rng = this.getRng(length, window); - return function(r, g, b, a, i){ + return function(r, g, b, a, i){ // eslint-disable-line max-params var index = String.fromCharCode(r, g, b, a); if (ignoredColors[index]){ return [r, g, b, a]; @@ -101,7 +103,7 @@ return function getConstantPixelRng(length, window, ignoredColors){ var rng = scope.nonPersistent.getRng(1024, window); - return function(r, g, b, a, i){ + return function(r, g, b, a, i){ // eslint-disable-line max-params var index = String.fromCharCode(r, g, b, a); if (ignoredColors[index]){ return [r, g, b, a]; @@ -147,7 +149,7 @@ }, getPixelRng: function(length, window, ignoredColors){ var rng = this.getRng(length, window); - return function(r, g, b, a, i){ + return function(r, g, b, a, i){ // eslint-disable-line max-params var index = String.fromCharCode(r, g, b, a); if (ignoredColors[index]){ return [r, g, b, a]; diff --git a/lib/require.js b/lib/require.js index b2d9d9f..3465c5a 100644 --- a/lib/require.js +++ b/lib/require.js @@ -1,4 +1,3 @@ -/* global settings exportFunction */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ @@ -10,11 +9,6 @@ function require(module){ var scopeName = module.substr(2).replace(/\..+/, ""); return window.scope[scopeName]; } - else if (module === "chrome"){ - return { - Cu: {exportFunction} - }; - } else if (module === "sdk/simple-prefs"){ return { prefs: settings, @@ -29,18 +23,6 @@ function require(module){ } }; } - else if (module === "sdk/l10n"){ - return { - get: function(key){ - return browser.i18n.getMessage(key); - } - }; - } - else if (module === "sdk/url"){ - return { - URL - }; - } throw new ReferenceError("Unable to get non relative module " + module + "!"); } diff --git a/lib/webgl.js b/lib/webgl.js index 808058f..3911f1f 100644 --- a/lib/webgl.js +++ b/lib/webgl.js @@ -32,7 +32,8 @@ var program = context.createProgram(); var shader = context.createShader(context.VERTEX_SHADER); - var vertex = "attribute vec4 a_position;\nattribute vec2 a_texCoord;\nvarying vec2 v_texCoord;\nvoid main(){\n\tgl_Position = a_position;\n\tv_texCoord = a_texCoord;\n}"; + var vertex = "attribute vec4 a_position;\nattribute vec2 a_texCoord;\nvarying vec2 v_texCoord;\n" + + "void main(){\n\tgl_Position = a_position;\n\tv_texCoord = a_texCoord;\n}"; context.shaderSource(shader, vertex); context.compileShader(shader); success = context.getShaderParameter(shader, context.COMPILE_STATUS); @@ -44,7 +45,8 @@ context.attachShader(program, shader); shader = context.createShader(context.FRAGMENT_SHADER); - var fragmenter = "precision mediump float;\nuniform sampler2D u_image;\nvarying vec2 v_texCoord;\nvoid main(){\n\tgl_FragColor = texture2D(u_image, v_texCoord);\n}"; + var fragmenter = "precision mediump float;\nuniform sampler2D u_image;\nvarying vec2 v_texCoord;\n" + + "void main(){\n\tgl_FragColor = texture2D(u_image, v_texCoord);\n}"; context.shaderSource(shader, fragmenter); context.compileShader(shader); success = context.getShaderParameter(shader, context.COMPILE_STATUS); diff --git a/pageAction/pageAction.js b/pageAction/pageAction.js index aecea36..4bc28ea 100644 --- a/pageAction/pageAction.js +++ b/pageAction/pageAction.js @@ -122,7 +122,10 @@ Promise.all([ data["canvasBlocker-notifications"].forEach(function(notification){ verbose(notification); notification.url = new URL(notification.url); - domainNotification(notification.url.hostname, notification.messageId).addNotification(new Notification(notification)); + domainNotification( + notification.url.hostname, + notification.messageId + ).addNotification(new Notification(notification)); }); } }); diff --git a/test/detectionTest.js b/test/detectionTest.js index c03c074..66352fb 100644 --- a/test/detectionTest.js +++ b/test/detectionTest.js @@ -53,7 +53,9 @@ addTest("function length", function(log){ addTest("function code", function(log){ "use strict"; - if (!CanvasRenderingContext2D.prototype.getImageData.toString().match(/^\s*function getImageData\s*\(\)\s*\{\s*\[native code\]\s*\}\s*$/)){ + if (!CanvasRenderingContext2D.prototype.getImageData.toString().match( + /^\s*function getImageData\s*\(\)\s*\{\s*\[native code\]\s*\}\s*$/ + )){ log("unexpected function code:", CanvasRenderingContext2D.prototype.getImageData.toString()); return true; } diff --git a/test/test.js b/test/test.js index 9484fc6..48a39b1 100644 --- a/test/test.js +++ b/test/test.js @@ -20,8 +20,12 @@ show(document.getElementById("top"), topTest()); show(document.getElementById("iframe"), iframeTest()); } - document.querySelector("#top button").addEventListener("click", function(){show(document.getElementById("top"), topTest());}); - document.querySelector("#iframe button").addEventListener("click", function(){show(document.getElementById("iframe"), iframeTest());}); + document.querySelector("#top button").addEventListener("click", function(){ + show(document.getElementById("top"), topTest()); + }); + document.querySelector("#iframe button").addEventListener("click", function(){ + show(document.getElementById("iframe"), iframeTest()); + }); }()); function draw(canvas){