From 602d5a6bfda943cd71613daabe89acd38be65b4d Mon Sep 17 00:00:00 2001 From: kkapsner Date: Tue, 31 Jan 2017 21:31:55 +0100 Subject: [PATCH] Switch from frameScript to require("sdk/remote/..."). Fixes #98. Fixes #100. --- data/frame.js | 93 --------------------------------------------------- lib/frame.js | 67 +++++++++++++++++++++++++++++++++++++ lib/main.js | 56 ++++--------------------------- 3 files changed, 74 insertions(+), 142 deletions(-) delete mode 100644 data/frame.js create mode 100644 lib/frame.js diff --git a/data/frame.js b/data/frame.js deleted file mode 100644 index 991a9b9..0000000 --- a/data/frame.js +++ /dev/null @@ -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); - }); -}()); \ No newline at end of file diff --git a/lib/frame.js b/lib/frame.js new file mode 100644 index 0000000..11722d8 --- /dev/null +++ b/lib/frame.js @@ -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; + }); +}()); \ No newline at end of file diff --git a/lib/main.js b/lib/main.js index 7a06838..acaa7f0 100644 --- a/lib/main.js +++ b/lib/main.js @@ -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/", "/");