1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-12-23 05:10:27 +01:00
CanvasBlocker/lib/modifiedDOMRectAPI.js

363 lines
10 KiB
JavaScript
Raw Normal View History

2018-09-04 23:29:58 +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";
let scope;
2018-09-04 23:29:58 +02:00
if ((typeof exports) !== "undefined"){
scope = exports;
}
else {
2019-03-12 22:24:23 +01:00
scope = require.register("./modifiedDOMRectAPI", {});
2018-09-04 23:29:58 +02:00
}
2019-12-12 00:09:53 +01:00
const extension = require("./extension");
const {checkerWrapper, setProperties, getStatusByFlag} = require("./modifiedAPIFunctions");
const {byteArrayToString: hash} = require("./hash");
2018-09-04 23:29:58 +02:00
let randomSupply = null;
2018-09-04 23:29:58 +02:00
scope.setRandomSupply = function(supply){
randomSupply = supply;
};
function getHash(domRect){
return hash(new Float64Array([domRect.x, domRect.y, domRect.width, domRect.height]));
}
function getValueHash(value){
return hash(new Float32Array([value]));
}
2018-09-04 23:29:58 +02:00
const registeredRects = new WeakMap();
function registerDOMRect(domRect, notify, window, prefs){
2019-12-12 00:09:53 +01:00
registeredRects.set(extension.getWrapped(domRect), {
2018-09-04 23:29:58 +02:00
notify: function(){
let done = false;
return function(message){
if (!done){
done = true;
notify(message);
}
};
}(),
window,
prefs
2018-09-04 23:29:58 +02:00
});
}
function getDOMRectRegistration(domRect){
2019-12-12 00:09:53 +01:00
return registeredRects.get(extension.getWrapped(domRect));
2018-09-04 23:29:58 +02:00
}
const cache = {};
2020-11-02 13:49:42 +01:00
const valueCache = [{}, {}, {}, {}, {}, {}, {}];
scope.cache = {
valueCache,
X: 0,
Y: 1,
WIDTH: 2,
HEIGHT: 3,
2020-11-02 13:49:42 +01:00
OTHER: 4,
Z: 5,
W: 6,
};
2020-11-02 13:49:42 +01:00
function getFakeValue(value, i, {window, prefs, rng}){
const valueHash = getValueHash(value);
const cache = valueCache[i];
let cachedValue = cache[valueHash];
if (typeof cachedValue === "number"){
return cachedValue;
}
if ((value * prefs("domRectIntegerFactor", window.location)) % 1 === 0){
cache[valueHash] = value;
return value;
}
else {
const fakedValue = value + 0.01 * (rng(i) / 0xffffffff - 0.5);
const fakedHash = getValueHash(fakedValue);
cache[valueHash] = fakedValue;
cache[fakedHash] = fakedValue;
return fakedValue;
}
}
scope.getFakeValue = getFakeValue;
function getFakeDomRect(window, domRect, prefs, notify){
2018-09-04 23:29:58 +02:00
const hash = getHash(domRect);
let cached = cache[hash];
2018-09-04 23:29:58 +02:00
if (!cached){
notify("fakedDOMRectReadout");
const rng = randomSupply.getRng(4, window);
2020-11-02 13:49:42 +01:00
const env = {window, prefs, rng};
cached = new (domRect instanceof window.SVGRect? window.DOMRectReadOnly: domRect.constructor)(
getFakeValue(domRect.x, 0, env),
getFakeValue(domRect.y, 1, env),
getFakeValue(domRect.width, 2, env),
getFakeValue(domRect.height, 3, env)
2018-09-04 23:29:58 +02:00
);
cache[hash] = cached;
cache[getHash(cached)] = cached;
2018-09-04 23:29:58 +02:00
}
return cached;
}
2020-11-02 13:49:42 +01:00
function getFakeDOMPoint(window, domPoint, prefs){
const env = {window, prefs, rng: randomSupply.getRng(7, window)};
return new domPoint.constructor(
getFakeValue(domPoint.x, 0, env),
getFakeValue(domPoint.y, 1, env),
getFakeValue(domPoint.z, 5, env),
getFakeValue(domPoint.w, 6, env)
);
}
function getFakeSVGPoint(window, svgPoint, prefs){
const env = {window, prefs, rng: randomSupply.getRng(2, window)};
svgPoint.x = getFakeValue(svgPoint.x, 0, env);
svgPoint.y = getFakeValue(svgPoint.y, 1, env);
return svgPoint;
}
function getFakeDOMQuad(window, domQuad, prefs, notify){
notify("fakedDOMRectReadout");
return new domQuad.constructor(
getFakeDOMPoint(window, domQuad.p1, prefs),
getFakeDOMPoint(window, domQuad.p2, prefs),
getFakeDOMPoint(window, domQuad.p3, prefs),
getFakeDOMPoint(window, domQuad.p4, prefs)
);
}
2018-09-04 23:29:58 +02:00
2019-12-10 15:07:22 +01:00
function registerCallback(args, check){
const {prefs, notify, window, original} = check;
const originalValue = args.length?
original.call(this, ...args):
2019-12-10 15:07:22 +01:00
original.call(this);
registerDOMRect(originalValue, notify, window, prefs);
return originalValue;
}
2020-11-02 13:49:42 +01:00
function fakePointCallback(args, check){
const {prefs, notify, window, original} = check;
const ret = args.length? original.call(this, ...args): original.call(this);
notify("fakedDOMRectReadout");
if (ret instanceof window.SVGPoint){
return getFakeSVGPoint(window, ret, prefs);
}
else {
return getFakeDOMPoint(window, ret, prefs);
}
}
2018-09-04 23:29:58 +02:00
scope.changedFunctions = {
getClientRects: {
object: ["Range", "Element"],
fakeGenerator: function(checker){
return function getClientRects(){
return checkerWrapper(checker, this, arguments, function(args, check){
const {prefs, notify, window, original} = check;
const ret = args.length? original.call(this, ...args): original.call(this);
2018-09-04 23:29:58 +02:00
for (let i = 0; i < ret.length; i += 1){
registerDOMRect(ret[i], notify, window, prefs);
2018-09-04 23:29:58 +02:00
}
return ret;
});
};
}
},
getBoundingClientRect: {
object: ["Range", "Element"],
fakeGenerator: function(checker){
return function getBoundingClientRect(){
2019-12-10 15:07:22 +01:00
return checkerWrapper(checker, this, arguments, registerCallback);
2018-09-04 23:29:58 +02:00
};
}
},
2020-11-02 13:49:42 +01:00
getBoxQuads: {
object: ["Document", "Element", "Text", "CSSPseudoElement"],
2018-09-04 23:29:58 +02:00
fakeGenerator: function(checker){
2020-11-02 13:49:42 +01:00
return function getBoxQuads(){
return checkerWrapper(checker, this, arguments, function(args, check){
const {prefs, notify, window, original} = check;
const ret = args.length? original.call(this, ...args): original.call(this);
for (let i = 0; i < ret.length; i += 1){
ret[i] = getFakeDOMQuad(window, ret[i], prefs, notify);
}
return ret;
});
2018-09-04 23:29:58 +02:00
};
}
},
2020-11-02 13:49:42 +01:00
// It seems only getBoxQuads creates a DOMQuad and this method is behind a flag.
// So the only way to create one is manually by the constructor and then no fingerprinting is possible.
// getBounds: {
// object: ["DOMQuad"],
// fakeGenerator: function(checker){
// return function getBounds(){
// return checkerWrapper(checker, this, arguments, registerCallback);
// };
// }
// },
2018-09-04 23:29:58 +02:00
getBBox: {
object: ["SVGGraphicsElement"],
fakeGenerator: function(checker){
return function getBBox(){
2019-12-10 15:07:22 +01:00
return checkerWrapper(checker, this, arguments, registerCallback);
2018-09-04 23:29:58 +02:00
};
}
},
2020-11-02 13:49:42 +01:00
getStartPositionOfChar: {
object: ["SVGTextContentElement"],
fakeGenerator: function(checker){
return function getStartOfChar(){
return checkerWrapper(checker, this, arguments, fakePointCallback);
};
}
},
getEndPositionOfChar: {
object: ["SVGTextContentElement"],
fakeGenerator: function(checker){
return function getEndOfChar(){
return checkerWrapper(checker, this, arguments, fakePointCallback);
};
}
},
2018-09-04 23:29:58 +02:00
getExtentOfChar: {
object: ["SVGTextContentElement"],
fakeGenerator: function(checker){
2019-12-10 15:07:22 +01:00
return function getExtentOfChar(){
return checkerWrapper(checker, this, arguments, registerCallback);
2018-09-04 23:29:58 +02:00
};
}
},
2020-11-02 13:49:42 +01:00
getPointAtLength: {
object: ["SVGGeometryElement", "SVGPathElement"],
fakeGenerator: function(checker){
return function getPointAtLength(){
return checkerWrapper(checker, this, arguments, fakePointCallback);
};
}
},
2018-09-04 23:29:58 +02:00
};
function generateChangedDOMRectPropertyGetter(property, readonly = false){
const changedGetter = {
objectGetters: readonly?
[
function(window){return window.DOMRectReadOnly && window.DOMRectReadOnly.prototype;}
]:
[
function(window){return window.DOMRect && window.DOMRect.prototype;},
2020-11-02 13:49:42 +01:00
function(window){return window.SVGRect && window.SVGRect.prototype;},
function(window){return window.DOMRectReadOnly && window.DOMRectReadOnly.prototype;}
],
name: property,
getterGenerator: function(){
2019-12-12 00:09:53 +01:00
const temp = {
get [property](){
const registration = getDOMRectRegistration(this);
if (registration){
return getFakeDomRect(
registration.window,
this,
registration.prefs,
registration.notify
2019-12-12 00:09:53 +01:00
)[property];
}
2019-12-12 00:09:53 +01:00
return this[property];
2018-09-04 23:29:58 +02:00
}
2019-12-12 00:09:53 +01:00
};
return Object.getOwnPropertyDescriptor(temp, property).get;
2018-09-04 23:29:58 +02:00
}
};
if (!readonly){
changedGetter.setterGenerator = function(window, original, prefs){
2019-12-12 00:09:53 +01:00
const temp = {
set [property](newValue){
const registration = getDOMRectRegistration(this);
if (registration){
const fakeDomRect = getFakeDomRect(window, this, prefs, registration.notify);
2019-12-12 00:09:53 +01:00
registeredRects.delete(extension.getWrapped(this));
2019-05-09 23:11:15 +02:00
["x", "y", "width", "height"].forEach((prop) => {
2019-12-12 00:09:53 +01:00
if (prop === property){
this[prop] = newValue;
}
else {
this[prop] = fakeDomRect[prop];
}
});
}
else {
original.call(this, ...arguments);
}
2018-09-04 23:29:58 +02:00
}
2019-12-12 00:09:53 +01:00
};
return Object.getOwnPropertyDescriptor(temp, property).set;
};
}
return changedGetter;
}
scope.changedGetters = [
generateChangedDOMRectPropertyGetter("x", false),
generateChangedDOMRectPropertyGetter("y", false),
generateChangedDOMRectPropertyGetter("width", false),
generateChangedDOMRectPropertyGetter("height", false),
generateChangedDOMRectPropertyGetter("left", true),
generateChangedDOMRectPropertyGetter("right", true),
generateChangedDOMRectPropertyGetter("top", true),
generateChangedDOMRectPropertyGetter("bottom", true),
2018-09-04 23:29:58 +02:00
{
objectGetters: [
function(window){
return window.IntersectionObserverEntry && window.IntersectionObserverEntry.prototype;
}
2018-09-04 23:29:58 +02:00
],
name: "intersectionRect",
getterGenerator: function(checker){
const temp = {
get intersectionRect(){
2019-12-10 15:07:22 +01:00
return checkerWrapper(checker, this, arguments, registerCallback);
2018-09-04 23:29:58 +02:00
}
};
return Object.getOwnPropertyDescriptor(temp, "intersectionRect").get;
}
},
{
objectGetters: [
function(window){
return window.IntersectionObserverEntry && window.IntersectionObserverEntry.prototype;
}
2018-09-04 23:29:58 +02:00
],
name: "boundingClientRect",
getterGenerator: function(checker){
const temp = {
get boundingClientRect(){
2019-12-10 15:07:22 +01:00
return checkerWrapper(checker, this, arguments, registerCallback);
2018-09-04 23:29:58 +02:00
}
};
return Object.getOwnPropertyDescriptor(temp, "boundingClientRect").get;
}
},
{
objectGetters: [
function(window){
return window.IntersectionObserverEntry && window.IntersectionObserverEntry.prototype;
}
2018-09-04 23:29:58 +02:00
],
name: "rootBounds",
getterGenerator: function(checker){
const temp = {
get rootBounds(){
2019-12-10 15:07:22 +01:00
return checkerWrapper(checker, this, arguments, registerCallback);
2018-09-04 23:29:58 +02:00
}
};
return Object.getOwnPropertyDescriptor(temp, "rootBounds").get;
}
}
];
2019-01-24 15:43:20 +01:00
setProperties(scope.changedFunctions, scope.changedGetters, {
type: "readout",
2019-12-10 15:07:22 +01:00
getStatus: getStatusByFlag("protectDOMRect"),
2019-01-24 15:43:20 +01:00
api: "domRect"
2018-09-04 23:29:58 +02:00
});
}());