1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-11-01 02:48:44 +01:00
CanvasBlocker/test/cspTest.js

65 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-12-14 21:22:18 +01:00
function draw(canvas){
"use strict";
canvas.setAttribute("width", 220);
canvas.setAttribute("height", 30);
2019-12-16 19:27:28 +01:00
const fp_text = "BrowserLeaks,com <canvas> 10";
2019-12-14 21:22:18 +01:00
2019-12-16 19:27:28 +01:00
const ctx = canvas.getContext("2d");
2019-12-14 21:22:18 +01: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 test(window){
"use strict";
// create window canvas
2019-12-16 19:27:28 +01:00
const canvas = document.createElement("canvas");
2019-12-14 21:22:18 +01:00
// draw image in window canvas
draw(canvas);
return window.HTMLCanvasElement.prototype.toDataURL.call(canvas);
}
2019-12-16 19:27:28 +01:00
async function hash(string){
2019-12-14 21:22:18 +01:00
"use strict";
2019-12-16 19:27:28 +01:00
const buffer = new TextEncoder("utf-8").encode(string);
const hash = await crypto.subtle.digest("SHA-256", buffer);
const chunks = [];
(new Uint32Array(hash)).forEach(function(num){
chunks.push(num.toString(16));
2019-12-14 21:22:18 +01:00
});
2019-12-16 19:27:28 +01:00
return chunks.map(function(chunk){
return "0".repeat(8 - chunk.length) + chunk;
}).join("");
2019-12-14 21:22:18 +01:00
}
2019-12-16 19:27:28 +01:00
const addLine = function(){
2019-12-14 21:22:18 +01:00
"use strict";
2019-12-16 19:27:28 +01:00
const output = document.getElementById("results");
2019-12-14 21:22:18 +01:00
return function(text){
2019-12-16 19:27:28 +01:00
const line = document.createElement("div");
2019-12-14 21:22:18 +01:00
line.textContent = text;
output.appendChild(line);
};
}();
addLine("window name at start: " + window.name);
window.name = "CanvasBlocker CSP test";
addLine("window name after set: " + window.name);
2019-12-16 19:27:28 +01:00
(async function(){
2019-12-14 21:22:18 +01:00
"use strict";
2019-12-16 19:27:28 +01:00
const hashValue = await hash(test(window));
addLine("canvas hash: " + hashValue);
}());