在Firefox无重启加载项中,如何在新窗口打开时运行代码(监听窗口打开)?

时间:2015-03-21 01:15:00

标签: javascript firefox-addon firefox-addon-restartless

我开始构建一个无重启的Firefox插件,我在设置bootstrap.js时遇到了问题。每个人似乎都同意bootstrap.js的核心几乎是样板代码,沿着这些方向:

const Cc = Components.classes;
const Ci = Components.interfaces;

function startup() {
  let wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
  let windows = wm.getEnumerator("navigator:browser");
  while (windows.hasMoreElements()) {
    let domWindow = windows.getNext().QueryInterface(Ci.nsIDOMWindow); 
    // then can control what happens with domWindow.document
  } 
}
function shutdown() {}
function install() {}
function uninstall() {}

此代码有效,我可以控制现有窗口中的内容。例如,domWindow.alert("text")成功创建标准提示,说明" text"在每个当前打开的窗口上。

但是,我找不到任何允许我在新窗口中执行操作的代码;即脚本运行后创建的那些。处理新窗口的创建和控制它们的正确方法是什么,以至于我可以获得另一个"文本"在创建时提醒一个人?

编辑:使用nsWindowMediator类和MDN的代码示例,我现在有了这个:

var windowListener = {
onOpenWindow: function (aWindow) {
  try {
    let domWindow = aWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
    domWindow.addEventListener("load", function () {
      domWindow.removeEventListener("load", arguments.callee, false);
      //window has now loaded now do stuff to it
      domWindow.alert("text");
    }, false);
  } catch (err) {
    Services.prompt.alert(null, "Error", err);
  }
},
onCloseWindow: function (aWindow) {},
onWindowTitleChange: function (aWindow, aTitle) {}
};

function startup(aData, aReason) {
  // Load into any existing windows
  try {
    let wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
    let windows = wm.getEnumerator("navigator:browser");
    while (windows.hasMoreElements()) {
      let domWindow = windows.getNext().QueryInterface(Ci.nsIDOMWindow);
      loadIntoWindow(domWindow);
    }
  } catch (err) {
    Services.prompt.alert(null, "Error", err);
  }

  Services.wm.addListener(windowListener);
}

然而,onOpenWindow调用仍然没有输出 - " text"警报不会出现,错误警告也不会在catch块中发出警报。我可以确认onOpenWindow实际上正在输入;如果我在onOpenWindow的开头放置Services.prompt.alert(),我会在创建新窗口时收到警报。不幸的是,我得到了无限循环的警报,我不明白为什么。

2 个答案:

答案 0 :(得分:2)

  

但是,我找不到任何允许我在新窗口中执行操作的代码

使用XPCOM对象时,通常需要研究它们的接口,这些接口通常位于MDN上。在这种情况下,您的起点为nsIWindowMediator,因为这是您在第5行中使用的服务。

正如您所看到的,它具有addListener函数,该函数采用实现nsIWindowMediatorListener的参数。页面上有一个代码示例。

但是,我们暂时假设没有代码示例。您可以在MDN上搜索界面,但它没有列出。下一步将是searching MXR for the .idl idl = interface description language

一旦你获得了接口契约,你可以或多或少地在javascript中实现它,至少对于听众而言。实现您自己的xpcom服务将是a little more complicated

Searching the addon sdk通常也会提供一些提示。在这种情况下,他们似乎没有使用.addListener,但该文件暗示了另一个有趣的服务,而这又可以在MDN上找到:nsIWindowWatcher

基本上,如果您正在编写无重新启动的插件,那么您需要通过firefox的内脏进行翻找,并且必须做一些侦探工作才能找到您需要的确切组件。如果你想要更方便的东西我会推荐addon sdk,它提供了更有条理但也更受限制的commonly used APIs

组合

答案 1 :(得分:2)

  

但是,我找不到任何允许我在新窗口中执行操作的代码;即脚本运行后创建的那些。处理新窗口的创建和控制它们的正确方法是什么,以至于在创建窗口时我可以从一个窗口获得另一个“文本”警报?

打开每个窗口时采取行动的正确方法是使用addListener()中的nsIWindowMediator。下面的示例代码执行此操作。 nsIWindowMediator包含在Services.jsm中,可通过Services.wm.addListener(WindowListener)访问。要使用窗口侦听器,您必须向其传递nsIWindowMediatorListenerref2)对象。 nsIWindowMediatorListener包含三个键:onOpenWindowonCloseWindowonWindowTitleChange。每个都应定义为在适当的事件发生时调用的函数。

How to convert an overlay extension to restartless”中的MDN文档Step 9: bootstrap.js包含基本bootstrap.js的示例,该示例将在函数loadIntoWindow(window)中为每个当前打开的运行代码浏览器窗口以及将来打开的任何浏览器窗口。我在几个不同的附加组件中使用了从中修改过的代码。该示例与您已使用的代码基本相似。这个例子是(稍加修改):

const Ci = Components.interfaces;

Components.utils.import("resource://gre/modules/Services.jsm");

function startup(data,reason) {
    // Load this add-ons module(s):
    Components.utils.import("chrome://myAddon/content/myModule.jsm");
    // Do whatever initial startup stuff is needed for this add-on.
    //   Code is in module just loaded.
    myModule.startup();  

    // Make changes to the Firefox UI to hook in this add-on
    forEachOpenWindow(loadIntoWindow);
    // Listen for any windows that open in the future
    Services.wm.addListener(WindowListener);
}

function shutdown(data,reason) {
    if (reason == APP_SHUTDOWN)
        return;

    // Unload the UI from each window
    forEachOpenWindow(unloadFromWindow);
    // Stop listening for new windows to open.
    Services.wm.removeListener(WindowListener);

    // Do whatever shutdown stuff you need to do on add-on disable
    myModule.shutdown();  

    // Unload the module(s) loaded specific to this extension.
    // Use the same URL for your module(s) as when loaded:
    Components.utils.unload("chrome://myAddon/content/myModule.jsm"); 

    // HACK WARNING: The Addon Manager does not properly clear all add-on related caches
    //               on update. In order to fully update images and locales, their
    //               caches need clearing here.
    Services.obs.notifyObservers(null, "chrome-flush-caches", null);
}

function install(data,reason) { }

function uninstall(data,reason) { }

function loadIntoWindow(window) {
    /* call/move your UI construction function here */
}

function unloadFromWindow(window) {
    /* call/move your UI tear down function here */
}

function forEachOpenWindow(todo) {
    // Apply a function to all open browser windows
    var windows = Services.wm.getEnumerator("navigator:browser");
    while (windows.hasMoreElements())
        todo(windows.getNext().QueryInterface(Ci.nsIDOMWindow));
}

var WindowListener = {
    onOpenWindow: function(xulWindow) {
        var window = xulWindow.QueryInterface(Ci.nsIInterfaceRequestor)
                              .getInterface(Ci.nsIDOMWindow);
        function onWindowLoad() {
            window.removeEventListener("load",onWindowLoad);
            // Only add UI changes if this is a browser window
            if (window.document.documentElement.getAttribute("windowtype") 
                                                                == "navigator:browser")
                loadIntoWindow(window);
        }
        window.addEventListener("load",onWindowLoad);
    },
    onCloseWindow: function(xulWindow) { },
    onWindowTitleChange: function(xulWindow, newTitle) { }
};

虽然您可能希望在 bootstrap.js 代码中执行更多操作,但上述内容组合得相当好,并且会将所有代码加载到Firefox UI中{ {1}}并在loadIntoWindow(window)内卸载用户界面。但是,应该注意的是,您应该只添加/删除一次的UI元素(例如,澳大利亚小部件,如按钮)和其他元素(例如,对Firefox DOM的直接更改)必须在每个窗口中添加一次。

  

不幸的是,我收到了无限循环的警报,我不明白为什么。

此示例与您当前使用的内容之间的一个显着差异是对已打开的窗口类型的测试。这样做是为了让我们只对新打开的窗口进行操作,这些窗口是浏览器窗口,而不是所有新打开的窗口:

unloadFromWindow(window)

您描述的获取无限循环if (window.document.documentElement.getAttribute("windowtype") == "navigator:browser") loadIntoWindow(window); 弹出窗口的问题是由于未检查以确保您仅在浏览器窗口上操作而导致的。 alert()弹出窗口是一个窗口。因此,您为打开的每个alert()窗口调用alert(),当然,只需打开另一个alert()窗口,您可以在其中调用alert()。这是你的无限循环。

其他参考文献:
1. Working with windows in chrome code

相关问题