From 096db123d71e41123bc172c0057f62ce4c1bcfb3 Mon Sep 17 00:00:00 2001 From: kkapsner Date: Mon, 11 Dec 2017 12:47:43 +0100 Subject: [PATCH] Added setting for minimal colors Fixes #162 --- _locales/de/messages.json | 9 +++++++++ _locales/en/messages.json | 9 +++++++++ lib/colorStatistics.js | 25 +++++++++++++++++++++++++ lib/modifiedAPI.js | 8 +++++++- lib/settingDefinitions.js | 4 ++++ options/settingsDisplay.js | 14 ++++++++++++++ releaseNotes.txt | 3 ++- 7 files changed, 70 insertions(+), 2 deletions(-) diff --git a/_locales/de/messages.json b/_locales/de/messages.json index 61d2fa5..11dcc92 100644 --- a/_locales/de/messages.json +++ b/_locales/de/messages.json @@ -311,6 +311,15 @@ "description": "" }, + "minColors_title": { + "message": "Minimale Anzahl an Farben", + "description": "" + }, + "minColors_description": { + "message": "Anzahl der Farben, die ein Canvas aufweisen muss, damit es vorgetäuscht wird. Dies ist ein Parameter, der die Detektion des Addons erschweren soll.\nACHTUNG: Dies kann die Sicherheit des Addons beeinträchtigen. Deswegen wird stark empfohlen, diesen Wert nicht über 10 zu setzen.", + "description": "" + }, + "fakeAlphaChannel_title": { "message": "Alpha-Kanal auch vortäuschen", "description": "" diff --git a/_locales/en/messages.json b/_locales/en/messages.json index e812114..bb6d02e 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -311,6 +311,15 @@ "description": "" }, + "minColors_title": { + "message": "Minimal number of colors", + "description": "" + }, + "minColors_description": { + "message": "Number of colors that a canvas must have to be faked.\nCAUTION: This can lower the safety of the addon, therefore it is highly recommended not to set this value above 10.", + "description": "" + }, + "fakeAlphaChannel_title": { "message": "Fake the alpha channel", "description": "" diff --git a/lib/colorStatistics.js b/lib/colorStatistics.js index 7f33ad1..2bb8df4 100644 --- a/lib/colorStatistics.js +++ b/lib/colorStatistics.js @@ -82,4 +82,29 @@ } return statistic; }; + scope.hasMoreColors = function hasMoreColors(rawData, threshold, statistic){ + if (statistic){ + return statistic.numberOfColors > threshold; + } + else { + var colors = Object.create(null); + var count = 0; + for (var i = 0, l = rawData.length; i < l; i += 4){ + var index = String.fromCharCode( + rawData[i + 0], + rawData[i + 1], + rawData[i + 2], + rawData[i + 3] + ); + if (!Object.prototype.hasOwnProperty.call(colors, index)){ + colors[index] = true; + count += 1; + if (count > threshold){ + return true; + } + } + } + return count > threshold; + } + }; }()); \ No newline at end of file diff --git a/lib/modifiedAPI.js b/lib/modifiedAPI.js index 6a6154d..7e23c12 100644 --- a/lib/modifiedAPI.js +++ b/lib/modifiedAPI.js @@ -79,10 +79,16 @@ var l = desc.length; var ignoredColors = {}; + var statistic; if (prefs("ignoreFrequentColors")){ - var statistic = colorStatistics.compute(source); + statistic = colorStatistics.compute(source); ignoredColors = statistic.getMaxColors(prefs("ignoreFrequentColors")); } + if (prefs("minColors")){ + if (!colorStatistics.hasMoreColors(source, prefs("minColors"), statistic)){ + return original; + } + } var rng = randomSupply.getPixelRng(l, window, ignoredColors); var fakeAlphaChannel = prefs("fakeAlphaChannel"); diff --git a/lib/settingDefinitions.js b/lib/settingDefinitions.js index 628ffdd..b045acd 100644 --- a/lib/settingDefinitions.js +++ b/lib/settingDefinitions.js @@ -67,6 +67,10 @@ name: "ignoreFrequentColors", defaultValue: 0 }, + { + name: "minColors", + defaultValue: 0 + }, { name: "fakeAlphaChannel", defaultValue: false diff --git a/options/settingsDisplay.js b/options/settingsDisplay.js index efd96e4..866bd2c 100644 --- a/options/settingsDisplay.js +++ b/options/settingsDisplay.js @@ -143,6 +143,20 @@ } ] }, + { + "name": "minColors", + "displayDependencies": [ + { + "blockMode": ["fakeReadout"], + "displayAdvancedSettings": [true] + }, + { + "blockMode": ["askReadout", "ask"], + "askDenyMode": ["fake"], + "displayAdvancedSettings": [true] + } + ] + }, { "name": "fakeAlphaChannel", "displayDependencies": [ diff --git a/releaseNotes.txt b/releaseNotes.txt index d604e6b..8f90ab5 100644 --- a/releaseNotes.txt +++ b/releaseNotes.txt @@ -8,7 +8,8 @@ Version 0.4.3: new features: - reset settings - new white random generator - creates output similar to Tor browser - - blockMode and showNotifications can now be chosen url specific + - blockMode and showNotifications can now be chosen url specific + - new setting to ignore canvas with only few colors fixes: - page action was not always showing