mirror of
https://github.com/kkapsner/CanvasBlocker
synced 2025-01-03 02:21:53 +01:00
Code cleanup und simplification
This commit is contained in:
parent
74e86d5d12
commit
feb16823c1
@ -5,6 +5,7 @@
|
|||||||
var blockMode = {
|
var blockMode = {
|
||||||
getContext: {
|
getContext: {
|
||||||
status: "block",
|
status: "block",
|
||||||
|
askText: "askForPermission",
|
||||||
askStatus: {
|
askStatus: {
|
||||||
askOnce: false,
|
askOnce: false,
|
||||||
alreadyAsked: false,
|
alreadyAsked: false,
|
||||||
@ -13,6 +14,7 @@
|
|||||||
},
|
},
|
||||||
readAPI: {
|
readAPI: {
|
||||||
status: "allow",
|
status: "allow",
|
||||||
|
askText: "askForReadoutPermission",
|
||||||
askStatus: {
|
askStatus: {
|
||||||
askOnce: false,
|
askOnce: false,
|
||||||
alreadyAsked: false,
|
alreadyAsked: false,
|
||||||
@ -22,42 +24,6 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
var undef;
|
var undef;
|
||||||
var originalGetContext = unsafeWindow.HTMLCanvasElement.prototype.getContext;
|
|
||||||
Object.defineProperty(
|
|
||||||
unsafeWindow.HTMLCanvasElement.prototype,
|
|
||||||
"getContext",
|
|
||||||
{
|
|
||||||
enumerable: true,
|
|
||||||
configureable: false,
|
|
||||||
get: exportFunction(function(){
|
|
||||||
switch (blockMode.getContext.status){
|
|
||||||
case "allow":
|
|
||||||
// console.log("allow");
|
|
||||||
return originalGetContext;
|
|
||||||
case "ask":
|
|
||||||
// console.log("ask");
|
|
||||||
var status = blockMode.getContext.askStatus;
|
|
||||||
var allow;
|
|
||||||
if (status.askOnce && status.alreadyAsked){
|
|
||||||
// console.log("already asked");
|
|
||||||
allow = status.answer;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// console.log("asking");
|
|
||||||
allow = window.confirm(_("askForPermission"));
|
|
||||||
status.alreadyAsked = true;
|
|
||||||
status.answer = allow;
|
|
||||||
}
|
|
||||||
return allow? originalGetContext: undef;
|
|
||||||
case "block":
|
|
||||||
default:
|
|
||||||
// console.log("block");
|
|
||||||
return undef;
|
|
||||||
}
|
|
||||||
}, unsafeWindow)
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
var randomImage = (function(){
|
var randomImage = (function(){
|
||||||
var length = Math.floor(20 + Math.random() * 100);
|
var length = Math.floor(20 + Math.random() * 100);
|
||||||
var bytes = "";
|
var bytes = "";
|
||||||
@ -67,18 +33,24 @@
|
|||||||
return bytes;
|
return bytes;
|
||||||
}());
|
}());
|
||||||
|
|
||||||
// Readout API blocking
|
// changed functions
|
||||||
var fakeFunctions = {
|
var changedFunctions = {
|
||||||
|
getContext: {
|
||||||
|
mode: blockMode.getContext,
|
||||||
|
object: unsafeWindow.HTMLCanvasElement
|
||||||
|
},
|
||||||
toDataURL: {
|
toDataURL: {
|
||||||
|
mode: blockMode.readAPI,
|
||||||
object: unsafeWindow.HTMLCanvasElement,
|
object: unsafeWindow.HTMLCanvasElement,
|
||||||
func: function(){
|
fake: function(){
|
||||||
var type = arguments[0] || "image/png";
|
var type = arguments[0] || "image/png";
|
||||||
return "data:" + type + ";base64," + btoa(randomImage);
|
return "data:" + type + ";base64," + btoa(randomImage);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
toBlob: {
|
toBlob: {
|
||||||
|
mode: blockMode.readAPI,
|
||||||
object: unsafeWindow.HTMLCanvasElement,
|
object: unsafeWindow.HTMLCanvasElement,
|
||||||
func: function(callback){
|
fake: function(callback){
|
||||||
var type = arguments[0] || "image/png";
|
var type = arguments[0] || "image/png";
|
||||||
var blob = new window.Blob(randomImage, {type: type});
|
var blob = new window.Blob(randomImage, {type: type});
|
||||||
callback(blob);
|
callback(blob);
|
||||||
@ -86,12 +58,13 @@
|
|||||||
exportOptions: {allowCallbacks: true}
|
exportOptions: {allowCallbacks: true}
|
||||||
},
|
},
|
||||||
mozGetAsFile: {
|
mozGetAsFile: {
|
||||||
object: unsafeWindow.HTMLCanvasElement,
|
mode: blockMode.readAPI,
|
||||||
func: undef
|
object: unsafeWindow.HTMLCanvasElement
|
||||||
},
|
},
|
||||||
getImageData: {
|
getImageData: {
|
||||||
|
mode: blockMode.readAPI,
|
||||||
object: unsafeWindow.CanvasRenderingContext2D,
|
object: unsafeWindow.CanvasRenderingContext2D,
|
||||||
func: function(sx, sy, sw, sh){
|
fake: function(sx, sy, sw, sh){
|
||||||
var imageData = new window.ImageData(sw, sh);
|
var imageData = new window.ImageData(sw, sh);
|
||||||
var l = sw * sh * 4;
|
var l = sw * sh * 4;
|
||||||
for (var i = 0; i < l; i += 1){
|
for (var i = 0; i < l; i += 1){
|
||||||
@ -104,40 +77,38 @@
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Object.keys(fakeFunctions).forEach(function(name){
|
Object.keys(changedFunctions).forEach(function(name){
|
||||||
var fakeFunction = fakeFunctions[name];
|
var changedFunction = changedFunctions[name];
|
||||||
var original = fakeFunction.object.prototype[name];
|
var original = changedFunction.object.prototype[name];
|
||||||
Object.defineProperty(
|
Object.defineProperty(
|
||||||
fakeFunction.object.prototype,
|
changedFunction.object.prototype,
|
||||||
name,
|
name,
|
||||||
{
|
{
|
||||||
enumerable: true,
|
enumerable: true,
|
||||||
configureable: false,
|
configureable: false,
|
||||||
get: exportFunction(function(){
|
get: exportFunction(function(){
|
||||||
var status = blockMode.readAPI.status;
|
var status = changedFunction.mode.status;
|
||||||
if (status === "ask"){
|
if (status === "ask"){
|
||||||
var askStatus = blockMode.readAPI.askStatus;
|
var askStatus = changedFunction.mode.askStatus;
|
||||||
var allow;
|
|
||||||
if (askStatus.askOnce && askStatus.alreadyAsked){
|
if (askStatus.askOnce && askStatus.alreadyAsked){
|
||||||
// console.log("already asked");
|
// console.log("already asked");
|
||||||
allow = askStatus.answer;
|
status = askStatus.answer;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// console.log("asking");
|
// console.log("asking");
|
||||||
allow = window.confirm(_("askForReadoutPermission"));
|
status = window.confirm(_(changedFunction.mode.askText))? "allow": "block";
|
||||||
askStatus.alreadyAsked = true;
|
askStatus.alreadyAsked = true;
|
||||||
askStatus.answer = allow;
|
askStatus.answer = status;
|
||||||
}
|
}
|
||||||
status = allow? "allow": "block";
|
|
||||||
}
|
}
|
||||||
switch (status){
|
switch (status){
|
||||||
case "allow":
|
case "allow":
|
||||||
return original;
|
return original;
|
||||||
case "fake":
|
case "fake":
|
||||||
return fakeFunction.func? exportFunction(
|
return changedFunction.fake? exportFunction(
|
||||||
fakeFunction.func,
|
changedFunction.fake,
|
||||||
unsafeWindow,
|
unsafeWindow,
|
||||||
fakeFunction.exportOptions
|
changedFunction.exportOptions
|
||||||
): undef;
|
): undef;
|
||||||
case "block":
|
case "block":
|
||||||
default:
|
default:
|
||||||
@ -202,7 +173,7 @@
|
|||||||
blockMode.getContext.status = "allow";
|
blockMode.getContext.status = "allow";
|
||||||
blockMode.readAPI.status = "allow";
|
blockMode.readAPI.status = "allow";
|
||||||
});
|
});
|
||||||
self.port.on("detach", function(force){
|
self.port.on("detach", function(){
|
||||||
blockMode.getContext.status = "allow";
|
blockMode.getContext.status = "allow";
|
||||||
blockMode.readAPI.status = "allow";
|
blockMode.readAPI.status = "allow";
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user