Skip to content

Instantly share code, notes, and snippets.

@step135
Created June 15, 2021 18:37
Show Gist options
  • Save step135/e06d7dcf6d3f37a1fd57e81d1db693e9 to your computer and use it in GitHub Desktop.
Save step135/e06d7dcf6d3f37a1fd57e81d1db693e9 to your computer and use it in GitHub Desktop.
var background = (function () {
var tmp = {};
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
for (var id in tmp) {
if (tmp[id] && (typeof tmp[id] === "function")) {
if (request.path === "background-to-page") {
if (request.method === id) tmp[id](request.data);
}
}
}
});
/* */
return {
"receive": function (id, callback) {tmp[id] = callback},
"send": function (id, data) {chrome.runtime.sendMessage({"path": "page-to-background", "method": id, "data": data})}
}
})();
var inject = function () {
const toBlob = HTMLCanvasElement.prototype.toBlob;
const toDataURL = HTMLCanvasElement.prototype.toDataURL;
const getImageData = CanvasRenderingContext2D.prototype.getImageData;
//
var nsfy = function (canvas, context) {
var rn = 1 < 6;
if (context) {
const shift = {
'r': rn ? 220 : Math.floor(Math.random() * 10) - 5,
'g': rn ? 111 : Math.floor(Math.random() * 10) - 5,
'b': rn ? 220 : Math.floor(Math.random() * 10) - 5,
'a': rn ? 3 : Math.floor(Math.random() * 10) - 5
};
//
const width = canvas.width;
const height = canvas.height;
if (width && height) {
const imageData = getImageData.apply(context, [0, 0, width, height]);
for (let i = 0; i < height; i++) {
for (let j = 0; j < width; j++) {
const n = ((i * (width * 4)) + (j * 4));
imageData.data[n + 0] = imageData.data[n + 0] + shift.r;
imageData.data[n + 1] = imageData.data[n + 1] + shift.g;
imageData.data[n + 2] = imageData.data[n + 2] + shift.b;
imageData.data[n + 3] = imageData.data[n + 3] + shift.a;
}
}
//
window.top.postMessage("canvas-fingerprint-defender-alert", '*');
context.putImageData(imageData, 0, 0);
}
}
};
//
Object.defineProperty(HTMLCanvasElement.prototype, "toBlob", {
"value": function () {
nsfy(this, this.getContext("2d"));
return toBlob.apply(this, arguments);
}
});
//
Object.defineProperty(HTMLCanvasElement.prototype, "toDataURL", {
"value": function () {
nsfy(this, this.getContext("2d"));
return toDataURL.apply(this, arguments);
}
});
//
Object.defineProperty(CanvasRenderingContext2D.prototype, "getImageData", {
"value": function () {
nsfy(this.canvas, this);
return getImageData.apply(this, arguments);
}
});
//
document.documentElement.dataset.cbscriptallow = true;
};
var script_1 = document.createElement("script");
script_1.textContent = "(" + inject + ")()";
document.documentElement.appendChild(script_1);
script_1.remove();
if (document.documentElement.dataset.cbscriptallow !== "true") {
var script_2 = document.createElement("script");
script_2.textContent = `{
const iframes = [...window.top.document.querySelectorAll("iframe[sandbox]")];
for (var i = 0; i < iframes.length; i++) {
if (iframes[i].contentWindow) {
if (iframes[i].contentWindow.CanvasRenderingContext2D) {
iframes[i].contentWindow.CanvasRenderingContext2D.prototype.getImageData = CanvasRenderingContext2D.prototype.getImageData;
}
if (iframes[i].contentWindow.HTMLCanvasElement) {
iframes[i].contentWindow.HTMLCanvasElement.prototype.toBlob = HTMLCanvasElement.prototype.toBlob;
iframes[i].contentWindow.HTMLCanvasElement.prototype.toDataURL = HTMLCanvasElement.prototype.toDataURL;
}
}
}
}`;
//
window.top.document.documentElement.appendChild(script_2);
script_2.remove();
}
window.addEventListener("message", function (e) {
if (e.data && e.data === "canvas-fingerprint-defender-alert") {
background.send("fingerprint", {"host": document.location.host});
}
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment