| Index: lib/appIntegration.js |
| =================================================================== |
| --- a/lib/appIntegration.js |
| +++ b/lib/appIntegration.js |
| @@ -1,14 +1,25 @@ |
| /* 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/. */ |
| +let {hook} = require("hooks"); |
| let functionHooks = new WeakMap(); |
| -let {hook} = require("hooks"); |
| + |
| +exports.removeFromWindow = function(window) |
| +{ |
| + if (functionHooks.has(window)) |
| + { |
| + let unhook = functionHooks.get(window); |
| + unhook(); |
| + functionHooks.delete(window); |
| + } |
| +}; |
| + |
| let {application} = require("info"); |
|
Thomas Greiner
2012/09/28 08:20:13
please move that up to where hook and functionHook
|
| switch (application) |
| { |
| case "firefox": |
| { |
| // Firefox |
| exports.isKnownWindow = function(window) window.document.documentElement.getAttribute("windowtype") == "navigator:browser"; |
| @@ -17,35 +28,26 @@ switch (application) |
| exports.getBrowser = function(window) "gBrowser" in window ? window.gBrowser : null; |
| exports.applyToWindow = function(window, corrector) |
| { |
| let urlbar = exports.getURLBar(window); |
| if (urlbar && urlbar.handleCommand && !functionHooks.has(window)) |
| { |
| // Handle new URLs being entered |
| - let unhook = hook(urlbar, "handleCommand", function() { |
| + let unhook = hook(urlbar, "handleCommand", function() |
| + { |
| let correction = corrector(window, urlbar.value); |
| if (correction) |
| urlbar.value = correction; |
| }); |
| functionHooks.set(window, unhook); |
| } |
| }; |
| - exports.removeFromWindow = function(window) |
| - { |
| - if (functionHooks.has(window)) |
| - { |
| - let unhook = functionHooks.get(window); |
| - unhook(); |
| - functionHooks.delete(window); |
| - } |
| - }; |
| - |
| exports.openInfobar = function(window, id, message, buttons, persistence) |
| { |
| let browser = exports.getBrowser(window); |
| let infobar = browser.getNotificationBox(); |
| let notif = infobar.getNotificationWithValue(id); |
| if (notif) |
| { |
| @@ -57,76 +59,73 @@ switch (application) |
| id, |
| require("info").addonRoot + "icon64.png", |
| infobar.PRIORITY_INFO_HIGH, |
| buttons |
| ); |
| notif.persistence = persistence; |
| }; |
| - exports.loadURI = function(uri) |
| + exports.loadURI = function(window, uri) |
| { |
| - exports.getBrowser(Services.wm.getMostRecentWindow("navigator:browser")).loadURI(uri); |
| + exports.getBrowser(window).loadURI(uri); |
| }; |
| break; |
| } |
| case "seamonkey": |
| { |
| let eventListeners = new WeakMap(); |
| - |
| + |
| // SeaMonkey |
| exports.isKnownWindow = function(window) window.document.documentElement.getAttribute("windowtype") == "navigator:browser"; |
| exports.getURLBar = function(window) "gURLBar" in window ? window.gURLBar : null; |
| exports.getBrowser = function(window) "gBrowser" in window ? window.gBrowser : null; |
| exports.applyToWindow = function(window, corrector) |
| { |
| let urlbar = exports.getURLBar(window); |
| let goButton = window.document.getElementById("go-button-container"); |
| - |
| + |
| if (urlbar && urlbar._fireEvent && !functionHooks.has(window)) |
| { |
| function correctURL() |
| { |
| let correction = corrector(window, urlbar.value); |
| if (correction) |
| urlbar.value = correction; |
| } |
| - |
| - let unhook = hook(urlbar, "_fireEvent", function(eventType) { |
| + |
| + let unhook = hook(urlbar, "_fireEvent", function(eventType) |
| + { |
| if (eventType == "textentered") |
| { |
| correctURL(); |
| } |
| }); |
| functionHooks.set(window, unhook); |
| - |
| + |
| if (goButton) |
| { |
| goButton.addEventListener("command", correctURL, true); |
| eventListeners.set(window, { |
| "listener": correctURL, |
| "element": goButton |
| }); |
| } |
| } |
| }; |
| + let basicRemove = exports.removeFromWindow; |
| exports.removeFromWindow = function(window) |
| { |
| - if (functionHooks.has(window)) |
| - { |
| - let unhook = functionHooks.get(window); |
| - unhook(); |
| - functionHooks.delete(window); |
| - } |
| - |
| + basicRemove(window); |
| + |
| if (eventListeners.has(window)) |
| { |
| let eventListener = eventListeners.get(window); |
| eventListener.element.removeEventListener("command", eventListener.listener, true); |
| } |
| }; |
| exports.openInfobar = function(window, id, message, buttons, persistence) |
| @@ -145,160 +144,147 @@ switch (application) |
| id, |
| require("info").addonRoot + "icon64.png", |
| infobar.PRIORITY_INFO_HIGH, |
| buttons |
| ); |
| notif.persistence = persistence; |
| }; |
| - exports.loadURI = function(uri) |
| + exports.loadURI = function(window, uri) |
| { |
| - exports.getBrowser(Services.wm.getMostRecentWindow("navigator:browser")).loadURI(uri); |
| + exports.getBrowser(window).loadURI(uri); |
| }; |
| break; |
| } |
| case "fennec": |
| { |
| // XUL Fennec |
| exports.isKnownWindow = function(window) window.document.documentElement.getAttribute("windowtype") == "navigator:browser"; |
| exports.getURLBar = function(window) null; |
| exports.getBrowser = function(window) null; |
| exports.applyToWindow = function(window, corrector) |
| { |
| - if ("BrowserUI" in window && window.BrowserUI.goToURI && !("urlfixerOldHandler" in window.BrowserUI.goToURI)) |
| + if ("BrowserUI" in window && window.BrowserUI.goToURI && !functionHooks.has(window)) |
| { |
| // Handle new URLs being entered |
| - let oldHandler = window.BrowserUI.goToURI; |
| - window.BrowserUI.goToURI = function(url) |
| + let unhook = hook(window.BrowserUI, "goToURI", function(url) |
| { |
| url = url || this._edit.value; |
| - try |
| - { |
| - let correction = corrector(window, url); |
| - if (correction) |
| - url = correction; |
| - } |
| - catch(e) |
| - { |
| - Cu.reportError(e); |
| - } |
| - oldHandler.call(this, url); |
| - } |
| - window.BrowserUI.goToURI.urlfixerOldHandler = oldHandler; |
| - window.BrowserUI.goToURI.toString = doNotRecompile; |
| + |
| + let correction = corrector(window, url); |
| + if (correction) |
| + url = correction; |
| + |
| + return [url]; |
| + }); |
| + functionHooks.set(window, unhook); |
| } |
| }; |
| - exports.removeFromWindow = function(window) |
| + exports.openInfobar = function(window, id, message, buttons, persistence) |
| { |
| - if ("BrowserUI" in window && window.BrowserUI.goToURI && "urlfixerOldHandler" in window.BrowserUI.goToURI) |
| - window.BrowserUI.goToURI = window.BrowserUI.goToURI.urlfixerOldHandler; |
| - }; |
| + if ("getNotificationBox" in window) |
| + { |
| + let infobar = window.getNotificationBox(); |
| + let notification = infobar.getNotificationWithValue(id); |
| - exports.openInfobar = function() |
| - { |
| - // TODO: Implement infobar |
| + if (notification) |
| + { |
| + infobar.removeNotification(notification); |
| + } |
| + |
| + notification = infobar.appendNotification( |
| + message, |
| + id, |
| + require("info").addonRoot + "icon64.png", |
| + infobar.PRIORITY_INFO_HIGH, |
| + buttons |
| + ); |
| + notification.persistence = persistence; |
| + } |
| }; |
| exports.loadURI = function(window, uri) |
| { |
| - // TODO: Implement infobar |
| + if ("BrowserUI" in window && "goToURI" in window.BrowserUI) |
| + { |
| + window.BrowserUI.goToURI(uri); |
| + } |
| }; |
| break; |
| } |
| case "fennec2": |
| { |
| // Native Fennec |
| exports.isKnownWindow = function(window) window.document.documentElement.getAttribute("windowtype") == "navigator:browser"; |
| exports.getURLBar = function(window) null; |
| exports.getBrowser = function(window) null; |
| exports.applyToWindow = function(window, corrector) |
| { |
| - if ("BrowserApp" in window && window.BrowserApp.observe && !("urlfixerOldHandler" in window.BrowserApp.observe)) |
| + if ("BrowserApp" in window && window.BrowserApp.observe && !functionHooks.has(window)) |
| { |
| - let oldHandler = window.BrowserApp.observe; |
| - let oldFunc = null; |
| - let handler = function() |
| + let innerUnhook = null; |
| + function cleanup() |
| { |
| - let params = Array.prototype.slice.apply(arguments); |
| - try |
| - { |
| - let correction = corrector(window, params[0]); |
| - if (correction) |
| - params[0] = correction; |
| - } |
| - catch(e) |
| - { |
| - Cu.reportError(e); |
| - } |
| - return oldFunc.apply(this, params); |
| - }; |
| + if (innerUnhook) |
| + innerUnhook(); |
| - window.BrowserApp.observe = function(subject, topic, data) |
| + innerUnhook = null; |
| + } |
| + |
| + let unhook = hook(window.BrowserApp, "observe", function(subject, topic, data) |
| { |
| // Huge hack: we replace addTab/loadURI when the observer is |
| // triggered. This seems to be the only way to know that the calls |
| // originate from user input. |
| let method = null; |
| if (topic == "Tab:Add") |
| method = "addTab"; |
| else if (topic == "Tab:Load") |
| method = "loadURI"; |
| if (method) |
| { |
| - oldFunc = this[method]; |
| - this[method] = handler; |
| + innerUnhook = hook(this, method, function() |
| + { |
| + let params = Array.prototype.slice.apply(arguments); |
| + let correction = corrector(window, params[0]); |
| + if (correction) |
| + params[0] = correction; |
| + return params; |
| + }); |
| } |
| - |
| - try |
| - { |
| - oldHandler.apply(this, arguments); |
| - } |
| - finally |
| - { |
| - if (method) |
| - this[method] = oldFunc; |
| - } |
| - }; |
| - window.BrowserApp.observe.urlfixerOldHandler = oldHandler; |
| - window.BrowserApp.observe.toString = doNotRecompile; |
| + }, cleanup); |
| + functionHooks.set(window, unhook); |
| } |
| }; |
| - exports.removeFromWindow = function(window) |
| - { |
| - if ("BrowserApp" in window && window.BrowserApp.observe && "urlfixerOldHandler" in window.BrowserApp.observe) |
| - window.BrowserApp.observe = window.BrowserApp.observe.urlfixerOldHandler; |
| - }; |
| - |
| exports.openInfobar = function(window, id, message, buttons, persistence) |
| { |
| if ("BrowserApp" in window && "selectedTab" in window.BrowserApp) |
| { |
| window.NativeWindow.doorhanger.show(message, id, buttons, window.BrowserApp.selectedTab.id, |
| { |
| persistence: persistence |
| } |
| ); |
| } |
| }; |
| - exports.loadURI = function(uri) |
| + exports.loadURI = function(window, uri) |
| { |
| - let window = Services.wm.getMostRecentWindow("navigator:browser"); |
| if ("BrowserApp" in window && "loadURI" in window.BrowserApp) |
| window.BrowserApp.loadURI(uri); |
| }; |
| break; |
| } |
| default: |
| { |