2016-08-06 19:17:36 +02:00
|
|
|
/* 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/. */
|
|
|
|
(function(){
|
|
|
|
"use strict";
|
|
|
|
|
2017-06-25 22:33:12 +02:00
|
|
|
var scope;
|
|
|
|
if ((typeof exports) !== "undefined"){
|
|
|
|
scope = exports;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
window.scope.randomSupplies = {};
|
|
|
|
scope = window.scope.randomSupplies;
|
|
|
|
}
|
|
|
|
|
2017-01-31 20:00:02 +01:00
|
|
|
function getDomain(window){
|
2017-02-01 11:15:05 +01:00
|
|
|
if (!window.location.href || window.location.href === "about:blank"){
|
2017-01-31 20:00:02 +01:00
|
|
|
if (window !== window.parent){
|
|
|
|
return getDomain(window.parent);
|
|
|
|
}
|
|
|
|
else if (window.opener){
|
|
|
|
return getDomain(window.opener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return window.location.host;
|
|
|
|
}
|
2017-09-23 23:34:29 +02:00
|
|
|
|
|
|
|
var persistentRnd = Object.create(null);
|
2017-02-01 10:49:33 +01:00
|
|
|
const getPersistentRnd = (function(){
|
2017-02-10 17:37:35 +01:00
|
|
|
|
2017-06-25 22:33:12 +02:00
|
|
|
browser.runtime.onMessage.addListener(function(data){
|
|
|
|
if (data["canvasBlocker-set-domain-rnd"]){
|
|
|
|
var {domain, rnd} = data["canvasBlocker-set-domain-rnd"];
|
|
|
|
persistentRnd[domain] = new Uint8Array(rnd);
|
|
|
|
}
|
|
|
|
if (data["canvasBlocker-clear-domain-rnd"]){
|
|
|
|
persistentRnd = Object.create(null);
|
|
|
|
}
|
2017-02-10 17:37:35 +01:00
|
|
|
});
|
2017-02-01 10:49:33 +01:00
|
|
|
|
|
|
|
return function getPersistentRnd(window){
|
2017-01-31 20:00:02 +01:00
|
|
|
var domain = getDomain(window);
|
2016-08-06 19:17:36 +02:00
|
|
|
if (!persistentRnd[domain]){
|
2016-08-15 13:27:28 +02:00
|
|
|
// create the (sub-)domains random numbers if not existing
|
2016-08-06 19:17:36 +02:00
|
|
|
persistentRnd[domain] = new Uint8Array(128);
|
2016-08-15 13:27:28 +02:00
|
|
|
window.crypto.getRandomValues(persistentRnd[domain]);
|
2017-10-06 16:06:31 +02:00
|
|
|
browser.runtime.sendMessage({
|
|
|
|
"canvasBlocker-new-domain-rnd": {domain, rnd: Array.from(persistentRnd[domain])}
|
|
|
|
});
|
2016-08-06 19:17:36 +02:00
|
|
|
}
|
2017-02-01 10:49:33 +01:00
|
|
|
return persistentRnd[domain];
|
2017-10-03 15:35:31 +02:00
|
|
|
};
|
2017-02-01 10:49:33 +01:00
|
|
|
}());
|
2017-06-25 22:33:12 +02:00
|
|
|
scope.persistent = {
|
2017-08-08 08:29:42 +02:00
|
|
|
name: "persistent",
|
2017-09-23 23:34:29 +02:00
|
|
|
setDomainRnd: function(domain, rnd){
|
|
|
|
persistentRnd[domain] = new Uint8Array(rnd);
|
|
|
|
},
|
2017-02-01 10:49:33 +01:00
|
|
|
getRng: function(length, window){
|
|
|
|
var bitSet = getPersistentRnd(window);
|
2016-08-06 19:17:36 +02:00
|
|
|
|
|
|
|
return function(value, i){
|
2016-08-15 13:27:28 +02:00
|
|
|
// use the last 7 bits from the value for the index of the
|
|
|
|
// random number
|
2016-08-06 19:17:36 +02:00
|
|
|
var index = value & 0x7F;
|
2016-08-15 13:27:28 +02:00
|
|
|
|
|
|
|
// use the last 3 bits from the position and the first bit from
|
|
|
|
// from the value to get bit to use from the random number
|
2016-08-06 19:17:36 +02:00
|
|
|
var bitIndex = ((i & 0x03) << 1) | (value >>> 7);
|
2016-08-15 13:27:28 +02:00
|
|
|
|
|
|
|
// extract the bit
|
2016-08-06 19:17:36 +02:00
|
|
|
var bit = (bitSet[index] >>> bitIndex) & 0x01;
|
2016-08-15 13:27:28 +02:00
|
|
|
|
2017-01-18 08:46:45 +01:00
|
|
|
// XOR the bit and the value to alter the last bit of it... or not
|
2016-08-06 19:17:36 +02:00
|
|
|
return value ^ bit;
|
|
|
|
};
|
2017-08-07 08:49:49 +02:00
|
|
|
},
|
|
|
|
getPixelRng: function(length, window, ignoredColors){
|
|
|
|
var rng = this.getRng(length, window);
|
2017-10-06 16:06:31 +02:00
|
|
|
return function(r, g, b, a, i){ // eslint-disable-line max-params
|
2017-08-07 08:49:49 +02:00
|
|
|
var index = String.fromCharCode(r, g, b, a);
|
|
|
|
if (ignoredColors[index]){
|
|
|
|
return [r, g, b, a];
|
2017-08-07 17:43:57 +02:00
|
|
|
}
|
2017-08-07 08:49:49 +02:00
|
|
|
var baseIndex = i * 4;
|
|
|
|
return [
|
|
|
|
rng(r, baseIndex + 0),
|
|
|
|
rng(g, baseIndex + 1),
|
|
|
|
rng(b, baseIndex + 2),
|
|
|
|
rng(a, baseIndex + 3)
|
|
|
|
];
|
2017-10-03 15:35:31 +02:00
|
|
|
};
|
2016-08-06 19:17:36 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-07 17:43:57 +02:00
|
|
|
scope.constant = {
|
2017-08-08 08:29:42 +02:00
|
|
|
name: "constant",
|
2017-08-07 17:43:57 +02:00
|
|
|
getRng: function(length, window){
|
|
|
|
return scope.nonPersistent.getRng(length, window);
|
|
|
|
},
|
|
|
|
getPixelRng: (function(){
|
|
|
|
var colors = Object.create(null);
|
|
|
|
return function getConstantPixelRng(length, window, ignoredColors){
|
|
|
|
var rng = scope.nonPersistent.getRng(1024, window);
|
|
|
|
|
2017-10-06 16:06:31 +02:00
|
|
|
return function(r, g, b, a, i){ // eslint-disable-line max-params
|
2017-08-07 17:43:57 +02:00
|
|
|
var index = String.fromCharCode(r, g, b, a);
|
|
|
|
if (ignoredColors[index]){
|
|
|
|
return [r, g, b, a];
|
|
|
|
}
|
|
|
|
var color = colors[index];
|
|
|
|
if (!color){
|
|
|
|
color = [
|
|
|
|
rng(r, 0),
|
|
|
|
rng(g, 0),
|
|
|
|
rng(b, 0),
|
|
|
|
rng(a, 0)
|
|
|
|
];
|
|
|
|
colors[index] = color;
|
|
|
|
}
|
|
|
|
return color;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}())
|
|
|
|
};
|
|
|
|
|
2017-06-25 22:33:12 +02:00
|
|
|
scope.nonPersistent = {
|
2017-08-08 08:29:42 +02:00
|
|
|
name: "nonPersistent",
|
2016-08-06 19:17:36 +02:00
|
|
|
getRng: function(length, window){
|
2016-08-15 13:27:28 +02:00
|
|
|
// Initialize the random number batch creation
|
2016-08-06 19:17:36 +02:00
|
|
|
var randomI = 65536;
|
|
|
|
var randomNumbers = new Uint8Array(Math.min(65536, length));
|
|
|
|
|
|
|
|
return function(value, i){
|
|
|
|
if (randomI >= randomNumbers.length){
|
2016-08-15 13:27:28 +02:00
|
|
|
// refill the random number bucket if empty
|
2016-08-06 19:17:36 +02:00
|
|
|
randomI = 0;
|
|
|
|
if (length - i < 65536){
|
|
|
|
randomNumbers = new Uint8Array(length - i);
|
|
|
|
}
|
|
|
|
window.crypto.getRandomValues(randomNumbers);
|
|
|
|
}
|
|
|
|
var rnd = randomNumbers[randomI];
|
2016-08-15 13:27:28 +02:00
|
|
|
randomI += 1;
|
|
|
|
|
2017-01-18 08:46:45 +01:00
|
|
|
// XOR the last bit to alter it... or not
|
|
|
|
return value ^ (rnd & 0x01);
|
2016-08-06 19:17:36 +02:00
|
|
|
};
|
2017-08-07 08:49:49 +02:00
|
|
|
},
|
|
|
|
getPixelRng: function(length, window, ignoredColors){
|
|
|
|
var rng = this.getRng(length, window);
|
2017-10-06 16:06:31 +02:00
|
|
|
return function(r, g, b, a, i){ // eslint-disable-line max-params
|
2017-08-07 08:49:49 +02:00
|
|
|
var index = String.fromCharCode(r, g, b, a);
|
|
|
|
if (ignoredColors[index]){
|
|
|
|
return [r, g, b, a];
|
|
|
|
}
|
|
|
|
var baseIndex = i * 4;
|
|
|
|
return [
|
|
|
|
rng(r, baseIndex + 0),
|
|
|
|
rng(g, baseIndex + 1),
|
|
|
|
rng(b, baseIndex + 2),
|
|
|
|
rng(a, baseIndex + 3)
|
|
|
|
];
|
2017-10-03 15:35:31 +02:00
|
|
|
};
|
2016-08-06 19:17:36 +02:00
|
|
|
}
|
|
|
|
};
|
2017-10-03 15:35:31 +02:00
|
|
|
}());
|