Firefox 47 - Application.activeWindow无法在插件中运行

时间:2016-06-13 08:44:59

标签: javascript firefox firefox-addon

升级到Firefox 47后,Application.activeWindow无效。

崩溃在:

 var doc = Application.activeWindow.activeTab.document;  

知道为什么吗?自从过去两年以来,插件一直运作良好。如果它已弃用(现在已删除),则如何在XUL插件中获取文档对象。

2 个答案:

答案 0 :(得分:1)

原因是

Application.activeWindow

在Firefox 47中不再有效。而不是

  • var doc = Application.activeWindow.activeTab.document;

我们也可以使用

    var chromeWindow = Services.wm.getMostRecentWindow('navigator:browser');
    var doc = chromeWindow.content.document; 

做同样的工作。

因此,将Application.activeWindow.activeTab.document替换为content.document可解决此问题。

我可以在Firefox 40中找到上述弃用警告页面

https://developer.mozilla.org/en-US/docs/Mozilla/Tech/Toolkit_API/FUEL/fuelIWindow

答案 1 :(得分:0)

您可以使用以下命令获取最近活动窗口的活动选项卡的文档:

if (window === null || typeof window !== "object") {
    //If you do not already have a window reference, you need to obtain one:
    //  Add/remove a "/" to comment/un-comment the code appropriate for your add-on type.
    /* Add-on SDK:
    var window = require('sdk/window/utils').getMostRecentBrowserWindow();
    //*/
    //* Overlay and bootstrap (from almost any context/scope):
    var window=Components.classes["@mozilla.org/appshell/window-mediator;1"]
                         .getService(Components.interfaces.nsIWindowMediator)
                         .getMostRecentWindow("navigator:browser");        
    //*/
}
if (typeof document === "undefined") {
    //If there is no document defined, get it
    var document = window.content.document;
}

这几乎适用于任何环境。

您已在评论中说明此代码是为响应按钮中的command事件而运行的。鉴于它是在偶数处理程序中,您可以使用:

var window = event.view;
var document = window.content.document;

document将成为事件发生窗口中当前标签的HTML文档。

对于上述任何一种情况,您可以获得一些可能有用的其他对象:

let gBrowser = window.gBrowser;
let primeDocument = window.document;//The primary document container for that Firefox window.
let mainXULWindow = window.document.getElementById("main-window");