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

Version 0.2.0

Reenabled notifications
Code cleanup.
This commit is contained in:
kkapsner 2015-09-06 15:40:34 +02:00
parent 97e0c6b9cd
commit c10b382c93
7 changed files with 106 additions and 83 deletions

Binary file not shown.

View File

@ -6,7 +6,48 @@
var preferences = require("sdk/simple-prefs"); var preferences = require("sdk/simple-prefs");
var prefService = require("sdk/preferences/service"); var prefService = require("sdk/preferences/service");
var prefs = preferences.prefs; var prefs = preferences.prefs;
var sharedFunctions = require("./sharedFunctions");
function getDomainRegExpList(domainList){
"use strict";
var list = domainList
.split(",")
.map(function(entry){
return entry.replace(/^\s+|\s+$/g, "");
})
.filter(function(entry){
return !!entry.length;
})
.map(function(entry){
var regExp;
var domain = !!entry.match(/^[\w.]+$/);
if (domain){
regExp = new RegExp("(?:^|\\.)" + entry.replace(/([\\\+\*\?\[\^\]\$\(\)\{\}\=\!\|\.])/g, "\\$1") + "\\.?$", "i");
}
else {
regExp = new RegExp(entry, "i");
}
return {
match: function(url){
if (domain){
return url.hostname.match(regExp);
}
else {
return url.href.match(regExp);
}
}
};
});
list.match = function(url){
return this.some(function(entry){
return entry.match(url);
});
};
return list;
}
var lists = { var lists = {
white: [], white: [],
@ -15,7 +56,7 @@ var lists = {
}; };
function updateList(type){ function updateList(type){
lists[type] = sharedFunctions.getDomainRegExpList(prefs[type + "List"]); lists[type] = getDomainRegExpList(prefs[type + "List"]);
} }
Object.keys(lists).forEach(function(type){ Object.keys(lists).forEach(function(type){
preferences.on(type + "List", function(){ preferences.on(type + "List", function(){

View File

@ -5,13 +5,11 @@
(function(){ (function(){
"use strict"; "use strict";
require("./stylePreferencePane"); require("./stylePreferencePane");
// require("./disableWithoutDocumentElement");
const {changedFunctions} = require("./modifiedAPI"); const {changedFunctions} = require("./modifiedAPI");
const {notify} = require("./notifications"); const {notify} = require("./notifications");
const lists = require("./lists"); const lists = require("./lists");
const sharedFunctions = require("./sharedFunctions"); const sharedFunctions = require("./sharedFunctions");
// preferences
const observers = require("sdk/system/events"); const observers = require("sdk/system/events");
const { when: unload } = require("sdk/system/unload"); const { when: unload } = require("sdk/system/unload");
@ -20,7 +18,23 @@
const prefs = preferences.prefs; const prefs = preferences.prefs;
function checkURL(url){ function checkURL(url){
return sharedFunctions.checkURL(url, prefs.blockMode); var match = sharedFunctions.checkURL(url, prefs.blockMode).match(/^(block|allow|fake)(|Readout|Everything|Context)$/);
if (match){
return {
type: (match[2] === "Everything" || match[2] === "")?
["context", "readout"]:
[match[2].toLowerCase()],
mode: match[1]
};
}
else {
return {
type: ["context", "readout"],
mode: "block"
};
}
} }
var apiNames = Object.keys(changedFunctions); var apiNames = Object.keys(changedFunctions);
@ -39,12 +53,14 @@
configureable: false, configureable: false,
get: function(){ get: function(){
var status = checkURL(window.location); var status = checkURL(window.location);
if (status === changedFunction.mode){ if (status.type.indexOf(changedFunction.type) !== -1){
var callingStackMsg = sharedFunctions.errorToCallingStackMsg(new Error()); var callingStackMsg = sharedFunctions.errorToCallingStackMsg(new Error());
switch (status){ switch (status.mode){
case "fakeReadout": case "allow":
// notify(window, callingStackMsg); return original;
return changedFunction.fake; case "fake":
notify(window, callingStackMsg);
return changedFunction.fake || undef;
//case "block": //case "block":
default: default:
return undef; return undef;

View File

@ -5,50 +5,48 @@
"use strict"; "use strict";
function getFakeCanvas(window, original){ function getFakeCanvas(window, original){
var canvas = original.cloneNode(true); var context = window.HTMLCanvasElement.prototype.getContext.call(original, "2d");
var context = window.HTMLCanvasElement.prototype.getContext.call(canvas, "2d"); var imageData = window.CanvasRenderingContext2D.prototype.getImageData.call(context, 0, 0, original.width, original.height);
var imageData = window.CanvasRenderingContext2D.prototype.getImageData.call(context, 0, 0, canvas.width, canvas.height);
var data = imageData.data; var data = imageData.data;
for (var i = 0, l = data.length; i < l; i += 1){ for (var i = 0, l = data.length; i < l; i += 1){
var value = 0xFF - data[i]; var value = data[i];
if (value > 0x80){ if (value >= 0x80){
value = value ^ Math.floor(Math.random() * 0x40);
}
else if (value > 0x40){
value = value ^ Math.floor(Math.random() * 0x20); value = value ^ Math.floor(Math.random() * 0x20);
} }
else if (value > 0x20){ else if (value >= 0x40){
value = value ^ Math.floor(Math.random() * 0x10); value = value ^ Math.floor(Math.random() * 0x10);
} }
else if (value > 0x10){ else if (value >= 0x20){
value = value ^ Math.floor(Math.random() * 0x08); value = value ^ Math.floor(Math.random() * 0x08);
} }
else if (value > 0x08){ else if (value >= 0x10){
value = value ^ Math.floor(Math.random() * 0x04); value = value ^ Math.floor(Math.random() * 0x04);
} }
else if (value > 0x04){ else if (value >= 0x08){
value = value ^ Math.floor(Math.random() * 0x02); value = value ^ Math.floor(Math.random() * 0x02);
} }
else if (value > 0x02){ else if (value >= 0x04){
value = value ^ Math.floor(Math.random() * 0x01); value = value ^ Math.floor(Math.random() * 0x01);
} }
data[i] = 0xFF - value; data[i] = value;
} }
var canvas = original.cloneNode(true);
var context = window.HTMLCanvasElement.prototype.getContext.call(canvas, "2d");
context.putImageData(imageData, 0, 0); context.putImageData(imageData, 0, 0);
return canvas; return canvas;
} }
function getWindow(canvas){ function getWindow(canvas){
console.log(canvas);
return canvas.ownerDocument.defaultView; return canvas.ownerDocument.defaultView;
} }
// changed functions and their fakes // changed functions and their fakes
exports.changedFunctions = { exports.changedFunctions = {
getContext: { getContext: {
mode: "block", type: "context",
object: "HTMLCanvasElement" object: "HTMLCanvasElement"
}, },
toDataURL: { toDataURL: {
mode: "fakeReadout", type: "readout",
object: "HTMLCanvasElement", object: "HTMLCanvasElement",
fake: function toDataURL(){ fake: function toDataURL(){
var window = getWindow(this); var window = getWindow(this);
@ -56,7 +54,7 @@
} }
}, },
toBlob: { toBlob: {
mode: "fakeReadout", type: "readout",
object: "HTMLCanvasElement", object: "HTMLCanvasElement",
fake: function toBlob(callback){ fake: function toBlob(callback){
var window = getWindow(this); var window = getWindow(this);
@ -65,7 +63,7 @@
exportOptions: {allowCallbacks: true} exportOptions: {allowCallbacks: true}
}, },
mozGetAsFile: { mozGetAsFile: {
mode: "fakeReadout", type: "readout",
object: "HTMLCanvasElement", object: "HTMLCanvasElement",
mozGetAsFile: function mozGetAsFile(callbak){ mozGetAsFile: function mozGetAsFile(callbak){
var window = getWindow(this); var window = getWindow(this);
@ -73,7 +71,7 @@
} }
}, },
getImageData: { getImageData: {
mode: "fakeReadout", type: "readout",
object: "CanvasRenderingContext2D", object: "CanvasRenderingContext2D",
fake: function getImageData(sx, sy, sw, sh){ fake: function getImageData(sx, sy, sw, sh){
var window = getWindow(this.canvas); var window = getWindow(this.canvas);
@ -88,7 +86,7 @@
} }
}, },
readPixels: { readPixels: {
mode: "fakeReadout", type: "readout",
object: "WebGLRenderingContext", object: "WebGLRenderingContext",
fake: function readPixels(x, y, width, height, format, type, pixels){ fake: function readPixels(x, y, width, height, format, type, pixels){
var window = getWindow(this.canvas); var window = getWindow(this.canvas);

View File

@ -18,11 +18,9 @@ exports.notify = function(window, callingStackMsg){
var domain = contentURL.hostname; var domain = contentURL.hostname;
var message = _("fakedReadout").replace(/\{url\}/g, domain); var message = _("fakedReadout").replace(/\{url\}/g, domain);
// var tab = tabUtils.getTabForId(worker.tab.id); var tab = tabUtils.getTabForContentWindow(window);
// var tabBrowser = tabUtils.getTabBrowserForTab(tab); var tabBrowser = tabUtils.getTabBrowserForTab(tab);
// var browser = tabUtils.getBrowserForTab(tab); var browser = tabUtils.getBrowserForTab(tab);
var tabBrowser = tabUtils.getTabBrowser(window);
var browser = windowUtils.getOwnerBrowserWindow(window);
var notifyBox = tabBrowser.getNotificationBox(browser); var notifyBox = tabBrowser.getNotificationBox(browser);
var notification = notifyBox.getNotificationWithValue("fake-readout"); var notification = notifyBox.getNotificationWithValue("fake-readout");

View File

@ -20,51 +20,11 @@ var _ = function(name, replace){
return str; return str;
}; };
function getDomainRegExpList(domainList){
"use strict";
var list = domainList
.split(",")
.map(function(entry){
return entry.replace(/^\s+|\s+$/g, "");
})
.filter(function(entry){
return !!entry.length;
})
.map(function(entry){
var regExp;
var domain = !!entry.match(/^[\w.]+$/);
if (domain){
regExp = new RegExp("(?:^|\\.)" + entry.replace(/([\\\+\*\?\[\^\]\$\(\)\{\}\=\!\|\.])/g, "\\$1") + "\\.?$", "i");
}
else {
regExp = new RegExp(entry, "i");
}
return {
match: function(url){
if (domain){
return url.hostname.match(regExp);
}
else {
return url.href.match(regExp);
}
}
};
});
list.match = function(url){
return this.some(function(entry){
return entry.match(url);
});
};
return list;
}
function checkURL(url, blockMode){ function checkURL(url, blockMode){
"use strict"; "use strict";
if (url.protocol === "about:") { if (url.protocol === "about:" || url.protocol == "chrome:") {
return "unblock"; return "allow";
} }
var mode = "block"; var mode = "block";
@ -72,9 +32,17 @@ function checkURL(url, blockMode){
case "blockEverything": case "blockEverything":
mode = "block"; mode = "block";
break; break;
case "block":
case "blockContext":
case "blockReadout":
case "fake":
case "fakeContext":
case "fakeReadout": case "fakeReadout":
case "allow":
case "allowContext":
case "allowReadout":
if (url && lists.get("white").match(url)){ if (url && lists.get("white").match(url)){
mode = "unblock"; mode = "allow";
} }
else if (url && lists.get("black").match(url)){ else if (url && lists.get("black").match(url)){
mode = "block"; mode = "block";
@ -84,7 +52,7 @@ function checkURL(url, blockMode){
} }
break; break;
case "allowEverything": case "allowEverything":
mode = "unblock"; mode = "allow";
break; break;
default: default:
console.log("Unknown blocking mode (" + blockMode + "). Default to block everything."); console.log("Unknown blocking mode (" + blockMode + "). Default to block everything.");
@ -131,8 +99,6 @@ function errorToCallingStackMsg(error){
return msg; return msg;
} }
exports.getDomainRegExpList = getDomainRegExpList;
exports.checkURL = checkURL; exports.checkURL = checkURL;
exports.parseStackEntry = parseStackEntry; exports.parseStackEntry = parseStackEntry;
exports.errorToCallingStackMsg = errorToCallingStackMsg; exports.errorToCallingStackMsg = errorToCallingStackMsg;

View File

@ -22,6 +22,10 @@
"type": "menulist", "type": "menulist",
"value": "fakeReadout", "value": "fakeReadout",
"options": [ "options": [
{
"value": "blockEverything",
"label": "block everything"
},
{ {
"value": "blockReadout", "value": "blockReadout",
"label": "block readout API" "label": "block readout API"
@ -52,6 +56,6 @@
"main": "lib/main.js", "main": "lib/main.js",
"author": "Korbinian Kapsner", "author": "Korbinian Kapsner",
"license": "MPL 2.0", "license": "MPL 2.0",
"version": "0.1.9-Development", "version": "0.2.0-Development",
"permissions": {"private-browsing": true} "permissions": {"private-browsing": true}
} }