1
0
Fork 0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2025-07-04 20:46:39 +02:00

e10s ready!

Fixes #60 and #42
This commit is contained in:
kkapsner 2016-02-13 12:28:36 +01:00
parent 35c6a82480
commit 951277e922
10 changed files with 411 additions and 178 deletions

View file

@ -5,82 +5,107 @@
(function(){
"use strict";
require("./stylePreferencePane");
const {changedFunctions} = require("./modifiedAPI");
const {when: unload} = require("sdk/system/unload");
const {check} = require("./check.js");
const {notify} = require("./notifications");
const {ask} = require("./askForPermission");
const _ = require("sdk/l10n").get;
const lists = require("./lists");
const sharedFunctions = require("./sharedFunctions");
const observers = require("sdk/system/events");
const { when: unload } = require("sdk/system/unload");
const preferences = require("sdk/simple-prefs");
const prefService = require("sdk/preferences/service");
const prefs = preferences.prefs;
function check(callingStack, url){
var match = sharedFunctions.check(callingStack, url, prefs.blockMode).match(/^(block|allow|fake|ask)(|Readout|Everything|Context)$/);
if (match){
return {
type: (match[2] === "Everything" || match[2] === "")?
["context", "readout"]:
[match[2].toLowerCase()],
mode: match[1]
};
const notificationPref = {
doShow: function(){
return prefs.showNotifications;
},
setShow: function(value){
prefs.showNotifications = value;
prefService.set("extensions.CanvasBlocker@kkapsner.de.showNotifications", prefs.showNotifications);
}
else {
return {
type: ["context", "readout"],
mode: "block"
};
}
};
// const observers = require("sdk/system/events");
// const {intercept} = require("./intercept");
// const {errorToCallingStack} = require("./callingStack.js");
// const {ask} = require("./askForPermission");
// function interceptWrapper(ev){
// intercept(ev, {
// check,
// ask: function(data){
// return ask(
// data,
// {
// _,
// prefs: function(name){
// return prefs[ev.data];
// }
// }
// );
// },
// notify: function(data, window){
// notify(
// data,
// {
// lists, _, notificationPref, window
// }
// );
// }
// });
// }
// observers.on("content-document-global-created", interceptWrapper);
// unload(function(){
// observers.off("content-document-global-created", interceptWrapper);
// });
const {Cc, Ci} = require("chrome");
var globalMM = Cc["@mozilla.org/globalmessagemanager;1"].getService(Ci.nsIMessageListenerManager);
var frameURL = require("sdk/self").data.url("frame.js?" + Math.random());
globalMM.loadFrameScript(frameURL, true);
var listeners = [];
function addMessageListener(name, func){
listeners.push({name, func});
globalMM.addMessageListener(name, func);
}
var apiNames = Object.keys(changedFunctions);
var undef;
function intercept({subject: window}){
apiNames.forEach(function(name){
var changedFunction = changedFunctions[name];
var original = window.wrappedJSObject[changedFunction.object].prototype[name];
Object.defineProperty(
window.wrappedJSObject[changedFunction.object].prototype,
name,
{
enumerable: true,
configureable: false,
get: function(){
var callingStack = sharedFunctions.errorToCallingStack(new Error());
var status = check(callingStack, window.location);
if (status.type.indexOf(changedFunction.type) !== -1){
if (status.mode === "ask"){
status.mode = ask(window, changedFunction.type, this, callingStack);
}
switch (status.mode){
case "allow":
return original;
case "fake":
notify(window, callingStack);
return changedFunction.fake || undef;
//case "block":
default:
return undef;
}
}
else {
return original;
}
}
}
);
});
}
observers.on("content-document-global-created", intercept);
unload(function(){
observers.off("content-document-global-created", intercept);
globalMM.removeDelayedFrameScript(frameURL);
globalMM.broadcastAsyncMessage("canvasBlocker-unload");
listeners.forEach(function(listener){
globalMM.removeMessageListener(listener.name, listener.func);
});
});
// messages from the frame.js
addMessageListener("canvasBlocker-check", function(ev){
var status = check(ev.data);
return status;
});
addMessageListener("canvasBlocker-notify", function(ev){
var browser = ev.target;
notify(ev.data, {lists, _, notificationPref, browser});
});
addMessageListener("canvasBlocker-pref-get", function(ev){
return prefs[ev.data];
});
addMessageListener("canvasBlocker-pref-set", function(ev){
prefs[ev.data.name] = ev.data.value;
prefService.set("extensions.CanvasBlocker@kkapsner.de." + ev.data.name, ev.data.value);
});
addMessageListener("canvasBlocker-list-match", function(ev){
return lists.get(ev.data.list).match(ev.data.url);
});
addMessageListener("canvasBlocker-list-appendTo", function(ev){
return lists.appendTo(ev.data.list, ev.data.entry);
});
addMessageListener("canvasBlocker-translate", function(ev){
return _(ev.data);
});
}());