1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-12-22 21:00:23 +01:00

181 lines
5.4 KiB
JavaScript
Raw Normal View History

2017-11-07 19:51:49 +01:00
/* eslint no-console: off */
2017-08-13 23:44:31 +02:00
(function(){
2017-10-03 15:35:31 +02:00
"use strict";
2018-07-12 00:42:34 +02:00
function hashToString(hash){
var chunks = [];
(new Uint32Array(hash)).forEach(function(num){
chunks.push(num.toString(16));
});
return chunks.map(function(chunk){
return "0".repeat(8 - chunk.length) + chunk;
}).join("");
}
function show(container, {url, imageData, isPointInPath}){
2017-11-24 17:06:43 +01:00
var display = container.querySelector(".display");
display.src = url;
display.title = url;
2017-08-13 23:44:31 +02:00
var buffer = new TextEncoder("utf-8").encode(url);
2018-07-12 00:42:34 +02:00
Promise.all([
crypto.subtle.digest("SHA-256", buffer),
crypto.subtle.digest("SHA-256", imageData.data)
]).then(function(hashes){
container.querySelector(".hash").textContent =
hashToString(hashes[0]) + " / " +
hashToString(hashes[1]);
2017-08-13 23:44:31 +02:00
});
container.querySelector(".isPointInPath").textContent = isPointInPath;
2017-08-13 23:44:31 +02:00
}
if (location.search !== "?notInitial"){
try {show(document.getElementById("top"), topTest());}
catch (e){console.error(e);}
try {show(document.getElementById("iframe"), iframeTest(document.querySelector("#iframe iframe")));}
catch (e){console.error(e);}
try {show(document.getElementById("iframe2"), iframeTest(document.querySelector("#iframe2 iframe")));}
catch (e){console.error(e);}
try {show(document.getElementById("iframe3"), iframeTest(document.querySelector("#iframe3 iframe")));}
catch (e){console.error(e);}
2019-05-22 23:46:56 +02:00
try {show(document.getElementById("iframe4"), dynamicIframeTest1());}
catch (e){console.error(e);}
try {show(document.getElementById("iframe5"), dynamicIframeTest2());}
catch (e){console.error(e);}
try {show(document.getElementById("iframe6"), dynamicIframeTest3());}
catch (e){console.error(e);}
2017-08-13 23:44:31 +02:00
}
document.querySelector("#top button").addEventListener("click", function(){
show(document.getElementById("top"), topTest());
});
document.querySelector("#iframe button").addEventListener("click", function(){
show(document.getElementById("iframe"), iframeTest(document.querySelector("#iframe iframe")));
});
document.querySelector("#iframe2 button").addEventListener("click", function(){
show(document.getElementById("iframe2"), iframeTest(document.querySelector("#iframe2 iframe")));
});
document.querySelector("#iframe3 button").addEventListener("click", function(){
show(document.getElementById("iframe3"), iframeTest(document.querySelector("#iframe3 iframe")));
});
2019-05-22 23:46:56 +02:00
document.querySelector("#iframe4 button").addEventListener("click", function(){
show(document.getElementById("iframe4"), dynamicIframeTest1());
});
document.querySelector("#iframe5 button").addEventListener("click", function(){
show(document.getElementById("iframe5"), dynamicIframeTest2());
});
document.querySelector("#iframe6 button").addEventListener("click", function(){
show(document.getElementById("iframe6"), dynamicIframeTest3());
});
2017-08-13 23:44:31 +02:00
}());
function draw(canvas){
2017-10-03 15:35:31 +02:00
"use strict";
2017-08-13 23:44:31 +02:00
canvas.setAttribute("width", 220);
canvas.setAttribute("height", 30);
var fp_text = "BrowserLeaks,com <canvas> 10";
2017-10-03 15:35:31 +02:00
var ctx = canvas.getContext("2d");
2017-08-13 23:44:31 +02:00
ctx.textBaseline = "top";
ctx.font = "14px 'Arial'";
ctx.textBaseline = "alphabetic";
ctx.fillStyle = "#f60";
ctx.fillRect(125, 1, 62, 20);
ctx.fillStyle = "#069";
ctx.fillText(fp_text, 2, 15);
ctx.fillStyle = "rgba(102, 204, 0, 07)";
ctx.fillText(fp_text, 4, 17);
return ctx;
}
function getIsPointInPath(ctx){
"use strict";
ctx.beginPath();
ctx.moveTo(20, 19);
ctx.lineTo(40, 19);
ctx.lineTo(30, 30);
ctx.closePath();
ctx.stroke();
return ctx.isPointInPath(30, 19);
}
2017-08-13 23:44:31 +02:00
function topTest(){
2017-10-03 15:35:31 +02:00
"use strict";
2017-08-13 23:44:31 +02:00
// create window canvas
2017-10-03 15:35:31 +02:00
var canvas = document.createElement("canvas");
2017-08-13 23:44:31 +02:00
// draw image in window canvas
var ctx = draw(canvas);
return {
2018-07-12 00:42:34 +02:00
imageData: ctx.getImageData(0, 0, canvas.width, canvas.height),
url: canvas.toDataURL(),
isPointInPath: getIsPointInPath(ctx)
};
2017-08-13 23:44:31 +02:00
}
2019-05-22 23:46:56 +02:00
function copyToDifferentDocumentTest(otherDocument){
2017-10-03 15:35:31 +02:00
"use strict";
2017-08-13 23:44:31 +02:00
// create window canvas
2017-10-03 15:35:31 +02:00
var canvas = document.createElement("canvas");
2017-08-13 23:44:31 +02:00
// draw image in window canvas
draw(canvas);
2019-05-22 23:46:56 +02:00
// create other canvas and context
var otherCanvas = otherDocument.createElement("canvas");
otherCanvas.setAttribute("width", 220);
otherCanvas.setAttribute("height", 30);
var otherContext = otherCanvas.getContext("2d");
2017-08-13 23:44:31 +02:00
2019-05-22 23:46:56 +02:00
// copy image from window canvas to iframe context
otherContext.drawImage(canvas, 0, 0);
2017-08-13 23:44:31 +02:00
return {
2019-05-22 23:46:56 +02:00
imageData: otherContext.getImageData(0, 0, otherCanvas.width, otherCanvas.height),
url: otherCanvas.toDataURL(),
isPointInPath: getIsPointInPath(otherContext)
};
2019-05-22 23:46:56 +02:00
}
function iframeTest(iframe){
"use strict";
return copyToDifferentDocumentTest(iframe.contentDocument);
}
function dynamicIframeTest1(){
"use strict";
var length = frames.length;
var iframe = document.createElement("iframe");
document.body.appendChild(iframe);
var iframeWindow = frames[length];
document.body.removeChild(iframe);
return copyToDifferentDocumentTest(iframeWindow.document);
}
function dynamicIframeTest2(){
"use strict";
var length = window.length;
var iframe = document.createElement("iframe");
document.body.appendChild(iframe);
var iframeWindow = window[length];
document.body.removeChild(iframe);
return copyToDifferentDocumentTest(iframeWindow.document);
}
function dynamicIframeTest3(){
"use strict";
var length = window.length;
var div = document.createElement("div");
document.body.appendChild(div);
div.innerHTML = "<iframe></iframe>";
var iframeWindow = window[length];
document.body.removeChild(div);
return copyToDifferentDocumentTest(iframeWindow.document);
2017-08-13 23:44:31 +02:00
}