1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-11-09 22:58:52 +01:00
CanvasBlocker/test/test.js

160 lines
4.3 KiB
JavaScript
Raw Normal View History

2020-01-06 15:15:04 +01:00
/* globals testAPI, canvasAPI */
2017-08-13 23:44:31 +02:00
(function(){
2017-10-03 15:35:31 +02:00
"use strict";
2019-12-16 19:27:28 +01:00
async function show(container, {url, imageData, isPointInPath}){
const display = container.querySelector(".display");
2020-10-26 14:17:34 +01:00
switch (display.nodeName){
case "IMG":
display.src = url;
break;
case "CANVAS": {
display.height = imageData.height;
display.width = imageData.width;
const context = display.getContext("2d");
context.putImageData(imageData, 0, 0);
}
break;
}
2017-11-24 17:06:43 +01:00
display.title = url;
2019-12-16 19:27:28 +01:00
const hashes = await Promise.all([
2020-01-06 15:15:04 +01:00
testAPI.hash(url),
imageData? testAPI.hash(imageData.data): ""
2019-12-16 19:27:28 +01:00
]);
container.querySelector(".hash").textContent =
2020-01-06 15:15:04 +01:00
hashes[0] + " / " +
hashes[1];
if (typeof isPointInPath === "boolean"){
container.querySelector(".isPointInPath").textContent = isPointInPath;
}
2017-08-13 23:44:31 +02:00
}
2020-01-06 15:15:04 +01:00
function iframeTest(testNode){
const iframe = testNode.querySelector("iframe");
return canvasAPI.fingerprint(iframe.contentWindow);
}
const tests = {
top: function(){return canvasAPI.fingerprint();},
2020-10-26 14:17:34 +01:00
getImageDataTest: function(){return canvasAPI.fingerprint();},
2020-01-06 15:15:04 +01:00
iframe: iframeTest,
iframe2: iframeTest,
iframe3: iframeTest,
iframe4: dynamicIframeTest1,
iframe5: dynamicIframeTest2,
iframe6: dynamicIframeTest3,
windowOpen: async function(testNode, initial){
if (initial){
return new Promise(function(resolve){
window.addEventListener("click", function windowOpenTest(){
window.removeEventListener("click", windowOpenTest);
const newWindow = window.open("/");
try{
resolve(canvasAPI.fingerprint(newWindow));
}
catch (error){
console.error(error);
}
newWindow.close();
});
});
2019-12-02 22:57:11 +01:00
}
2020-01-06 15:15:04 +01:00
else {
const newWindow = window.open("/");
const fingerprint = canvasAPI.fingerprint(newWindow);
newWindow.close();
return fingerprint;
2019-12-02 22:57:11 +01:00
}
},
blob: async function(testNode){
const canvas = document.createElement("canvas");
canvasAPI.draw(canvas);
return new Promise(function(resolve, reject){
canvas.toBlob(async function(blob){
resolve({url: await testAPI.readBlob(blob)});
});
});
},
offscreen: async function(testNode){
if (typeof OffscreenCanvas === "undefined"){
throw "not supported";
}
return offscreenTest();
},
offscreenWorker: async function(testNode){
if (
typeof OffscreenCanvas === "undefined" ||
typeof Worker === "undefined"
){
throw "not supported";
}
const canvasAPIUrl = new URL("./canvasAPI.js", location);
const testAPIUrl = new URL("./testAPI.js", location);
return testAPI.runInWorker(offscreenTest, [], [canvasAPIUrl, testAPIUrl]);
},
2020-01-06 15:15:04 +01:00
};
Object.keys(tests).forEach(async function(testName){
const testNode = document.getElementById(testName);
const callback = tests[testName];
if (location.search !== "?notInitial"){
try {
show(testNode, await callback(testNode, true));
}
catch (error){
testNode.querySelector(".hash").innerHTML =
"<i>Error while computing: " + (error.message || error) + "</i>";
console.error(testName, error);
}
2020-01-06 15:15:04 +01:00
}
testNode.querySelector("button").addEventListener("click", async function(){
show(testNode, await callback(testNode));
2019-12-02 22:57:11 +01:00
});
});
2017-08-13 23:44:31 +02:00
}());
2019-05-22 23:46:56 +02:00
function dynamicIframeTest1(){
"use strict";
2019-12-16 19:27:28 +01:00
const length = frames.length;
const iframe = document.createElement("iframe");
2019-05-22 23:46:56 +02:00
document.body.appendChild(iframe);
2019-12-16 19:27:28 +01:00
const iframeWindow = frames[length];
2019-05-22 23:46:56 +02:00
document.body.removeChild(iframe);
2020-01-06 15:15:04 +01:00
return canvasAPI.fingerprint(iframeWindow);
2019-05-22 23:46:56 +02:00
}
function dynamicIframeTest2(){
"use strict";
2019-12-16 19:27:28 +01:00
const length = window.length;
const iframe = document.createElement("iframe");
2019-05-22 23:46:56 +02:00
document.body.appendChild(iframe);
2019-12-16 19:27:28 +01:00
const iframeWindow = window[length];
2019-05-22 23:46:56 +02:00
document.body.removeChild(iframe);
2020-01-06 15:15:04 +01:00
return canvasAPI.fingerprint(iframeWindow);
2019-05-22 23:46:56 +02:00
}
function dynamicIframeTest3(){
"use strict";
2019-12-16 19:27:28 +01:00
const length = window.length;
const div = document.createElement("div");
2019-05-22 23:46:56 +02:00
document.body.appendChild(div);
div.innerHTML = "<iframe></iframe>";
2019-12-16 19:27:28 +01:00
const iframeWindow = window[length];
2019-05-22 23:46:56 +02:00
document.body.removeChild(div);
2020-01-06 15:15:04 +01:00
return canvasAPI.fingerprint(iframeWindow);
}
async function offscreenTest(){
"use strict";
const offscreenCanvas = new OffscreenCanvas(220, 30);
canvasAPI.draw(offscreenCanvas);
const blob = offscreenCanvas.convertToBlob?
await offscreenCanvas.convertToBlob():
await offscreenCanvas.toBlob();
return {url: await testAPI.readBlob(blob)};
2017-08-13 23:44:31 +02:00
}