1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-06-06 05:17:49 +02:00

Switch from frameScript to require("sdk/remote/...").

Fixes #98. Fixes #100.
This commit is contained in:
kkapsner 2017-01-31 21:31:55 +01:00
parent c0c16b6546
commit 602d5a6bfd
3 changed files with 74 additions and 142 deletions

View File

@ -1,93 +0,0 @@
/* jslint moz: true */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
(function(){
"use strict";
const {utils: Cu} = Components;
const { Loader, Require, unload, Module} = Components.utils.import('resource://gre/modules/commonjs/toolkit/loader.js');
var loader = Loader({
paths: {
"": "resource://gre/modules/commonjs/"
}
});
var requirer = Module("resource://canvasblocker-at-kkapsner-dot-de/data/frame.js", "./data/frame.js");
var require = Require(loader, requirer);
const {intercept} = require("../lib/intercept.js");
const {ask} = require("../lib/askForPermission.js");
// Variable to "unload" the script
var enabled = true;
function check(message){
if (enabled){
var status = sendSyncMessage(
"canvasBlocker-check",
message
);
return status[0];
}
else {
return {type: [], mode: "allow"};
}
}
function checkStack(stack){
if (enabled){
var status = sendSyncMessage(
"canvasBlocker-checkStack",
stack
);
return status[0];
}
else {
return true;
}
}
function askWrapper(data){
return ask(data, {
_: function(token){
return sendSyncMessage(
"canvasBlocker-translate",
token
)[0];
},
prefs
});
}
function notify(data){
sendAsyncMessage("canvasBlocker-notify", data);
}
function prefs(name){
return sendSyncMessage(
"canvasBlocker-pref-get",
name
)[0];
}
function interceptWrapper(ev){
if (enabled){
// window is only equal to content for the top window. For susequent
// calls (e.g. iframe windows) the new generated window has to be
// used.
var window = ev.target.defaultView;
intercept(
{subject: window},
{check, checkStack, ask: askWrapper, notify, prefs}
);
}
}
addEventListener("DOMWindowCreated", interceptWrapper);
var context = this;
addEventListener("unload", function(ev){
if (ev.target === context){
removeEventListener("DOMWindowCreated", interceptWrapper);
}
});
addMessageListener("canvasBlocker-unload", function unload(){
enabled = false;
removeEventListener("DOMWindowCreated", interceptWrapper);
removeMessageListener("canvasBlocker-unload", unload);
});
}());

67
lib/frame.js Normal file
View File

@ -0,0 +1,67 @@
/* jslint moz: true */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
(function(){
"use strict";
const {process, frames} = require("sdk/remote/child");
const {intercept} = require("./intercept.js");
const {ask} = require("./askForPermission.js");
const {check: originalCheck, checkStack: originalCheckStack} = require("./check.js");
// Variable to "unload" the script
var enabled = true;
function check(message){
if (enabled){
return originalCheck(message);
}
else {
return {type: [], mode: "allow"};
}
}
function checkStack(stack){
if (enabled){
return originalCheckStack(stack);
}
else {
return true;
}
}
const _ = require("sdk/l10n").get;
function askWrapper(data){
return ask(data, {
_,
prefs
});
}
function notify(data){
process.port.emit("canvasBlocker-notify", data);
}
const preferences = require("sdk/simple-prefs");
function prefs(name){
return preferences.prefs[name];
}
frames.forEvery(function(frame){
frame.addEventListener("DOMWindowCreated", function(ev){
if (enabled){
var subject = ev.target.defaultView;
function notify(data){
frame.port.emit("canvasBlocker-notify", data);
}
intercept(
{subject},
{check, checkStack, ask: askWrapper, notify, prefs}
);
}
});
});
process.port.on("canvasBlocker-unload", function unload(){
enabled = false;
});
}());

View File

@ -8,13 +8,11 @@
const {when: unload} = require("sdk/system/unload");
const {check, checkStack} = require("./check.js");
const {notify} = require("./notifications");
const _ = require("sdk/l10n").get;
const lists = require("./lists");
const preferences = require("sdk/simple-prefs");
const prefService = require("sdk/preferences/service");
const prefs = preferences.prefs;
const notificationPref = {
@ -30,57 +28,17 @@
}
};
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);
const {processes, frames, remoteRequire} = require("sdk/remote/parent");
remoteRequire("./frame.js", module);
var listeners = [];
function addMessageListener(name, func){
listeners.push({name, func});
globalMM.addMessageListener(name, func);
}
frames.port.on("canvasBlocker-notify", function(frame, data){
notify(data, {lists, _, notificationPref, browser: frame.frameElement});
});
unload(function(){
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-checkStack", function(ev){
return checkStack(ev.data);
});
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);
processes.port.emit("canvasBlocker-unload");
});
// show release notes
var data = require("sdk/self").data;
preferences.on("showReleaseNotes", function(){
var url = data.url("releaseNotes.txt").replace("/data/", "/");