使用 Firefox 附加 SDK 访问 XUL 元素

发布于 2024-12-14 12:01:32 字数 1066 浏览 3 评论 0原文

我正在尝试使用附加 SDK 操作 Firefox 附加页面中的 XUL 元素。我不介意使用较低级别的模块。我使用 DOM 检查器来查看附加页面的结构。附加页面看起来像这样:

#document
--page (id='addons-page', windowtype='Addons:Manager', etc.)
----...
----hbox
----hbox
----etc.

所以我在exports.main 中尝试了这段代码:

let delegate = {
  onTrack: function(window) {
    console.log('window is being tracked: ' + window); // outputs [object ChromeWindow

    let doc = window.document;
    var addOnPage = doc.getElementById('addons-page');
    console.log(window.document.page);  // outputs undefined
    console.log(addOnPage);             // outputs null

    var xulElements = window.document.getElementsByClassName('addon-control');

    console.log('our elements: ' + xulElements); // outputs [object HTMLCollection]
    console.log('our elements length: ' + xulElements.length); // outputs length of 0
  }
};
var tracker = new winUtils.WindowTracker(delegate);

第一个问题是窗口跟踪器仅在首次启动Firefox 时打开。如何让它监听并等待附加页面打开?

第二个问题(可能与第一个问题相关)是获取元素似乎不起作用(xulElements.length 为 0)。

有什么想法吗?

I'm trying to manipulate the XUL elements in the Firefox add-on page using the Add-on SDK. I wouldn't mind using lower-level modules. I used DOM inspector to see the structure for the add-on page. It looks like this for the add-on page:

#document
--page (id='addons-page', windowtype='Addons:Manager', etc.)
----...
----hbox
----hbox
----etc.

So I tried this bit of code in exports.main:

let delegate = {
  onTrack: function(window) {
    console.log('window is being tracked: ' + window); // outputs [object ChromeWindow

    let doc = window.document;
    var addOnPage = doc.getElementById('addons-page');
    console.log(window.document.page);  // outputs undefined
    console.log(addOnPage);             // outputs null

    var xulElements = window.document.getElementsByClassName('addon-control');

    console.log('our elements: ' + xulElements); // outputs [object HTMLCollection]
    console.log('our elements length: ' + xulElements.length); // outputs length of 0
  }
};
var tracker = new winUtils.WindowTracker(delegate);

The first problem is that the window tracker only open when Firefox is first started. How can I get it to listen and wait for the add-on page to be opened?

The second problem (probably related to the first) is that getting the elements doesn't seem to be working (xulElements.length is 0).

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狠疯拽 2024-12-21 12:01:32

这里有两个问题:

  1. 附加组件管理器通常不会作为单独的窗口打开,因此使用 WindowTracker 是没有意义的。它是加载到浏览器中的页面。
  2. 您在窗口有机会加载之前访问该窗口,因此您看不到任何元素也就不足为奇了。

鉴于 < code>page-mod 模块 似乎不适用于此页面,正在监听 chrome-document-global-created 通知 可能是最好的解决方案。这段代码对我有用:

var observers = require("observer-service");
observers.add("chrome-document-global-created", function(wnd)
{
  if (wnd.location.href == "about:addons")
  {
    // Wait for the window to load before accessing it
    wnd.addEventListener("load", function()
    {
      console.log(wnd.document.getElementsByClassName('addon-control').length);
    }, false);
  }
});

Two issues here:

  1. Add-on Manager doesn't usually open as a separate window so using WindowTracker is pointless. It is a page loaded into the browser.
  2. You access the window before it has a chance to load to it isn't surprising that you don't see any elements.

Given that page-mod module doesn't seem to work for this page, listening to the chrome-document-global-created notification is probably the best solution. This code works for me:

var observers = require("observer-service");
observers.add("chrome-document-global-created", function(wnd)
{
  if (wnd.location.href == "about:addons")
  {
    // Wait for the window to load before accessing it
    wnd.addEventListener("load", function()
    {
      console.log(wnd.document.getElementsByClassName('addon-control').length);
    }, false);
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文