1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-07-07 17:19:17 +02:00
CanvasBlocker/data/inject.js

59 lines
1.6 KiB
JavaScript
Raw Normal View History

2014-08-01 12:03:23 +02:00
var getContext = unsafeWindow.HTMLCanvasElement.prototype.getContext;
var askFunctionName = Math.random().toString(16);
2014-07-31 03:05:51 +02:00
2014-08-04 17:02:42 +02:00
function checkPDF(blocking){
if (unsafeWindow.document.contentType.match(/\/pdf$/i)){
self.port.emit("isPDF", blocking);
return true;
2014-08-04 17:02:42 +02:00
}
return false;
2014-07-31 03:05:51 +02:00
}
2014-08-04 17:02:42 +02:00
function block(force){
if (force || !checkPDF("block")){
// consoe.log("block");
delete unsafeWindow.HTMLCanvasElement.prototype[askFunctionName];
unsafeWindow.HTMLCanvasElement.prototype.getContext = null;
}
}
function ask(force){
if (force || !checkPDF("ask")){
// console.log("ask");
Object.defineProperty(
unsafeWindow.HTMLCanvasElement.prototype,
askFunctionName,
{
value: getContext,
enumerabe: false
}
);
unsafeWindow.HTMLCanvasElement.prototype.getContext = new unsafeWindow.Function(function(){
var oldBorder = this.style.border;
this.style.border = "2px dashed red";
var confirmText =
this.parentNode?
"Do you want to allow the red bordered <canvas>?":
"Do you want to allow an invisibe <canvas>?";
var allow = confirm(confirmText);
this.style.border = oldBorder;
if (allow){
this.getContext = this["askFunctionName"];
return this["askFunctionName"].apply(this, arguments);
}
else {
return null;
}
}.toString().replace(/^function\s*\(\)\s*\{|\}\s*$/g, "").replace(/askFunctionName/g, askFunctionName));
}
2014-08-01 12:03:23 +02:00
}
2014-07-31 03:05:51 +02:00
function unblock(){
2014-08-01 12:03:23 +02:00
// console.log("unblock");
2014-07-31 03:05:51 +02:00
unsafeWindow.HTMLCanvasElement.prototype.getContext = getContext;
}
2014-08-01 12:03:23 +02:00
ask();
2014-07-31 03:05:51 +02:00
self.port.on("block", block);
2014-08-01 12:03:23 +02:00
self.port.on("ask", ask);
2014-07-31 03:05:51 +02:00
self.port.on("unblock", unblock);
self.port.on("detach", unblock);