如何覆盖引导加载项中的另一个扩展XUL文件(或者修改XUL文件的DOM)? [英] How to overlay an XUL file from another extension in a bootstrapped add-on (or otherwise modify the DOM of the XUL file)?

查看:147
本文介绍了如何覆盖引导加载项中的另一个扩展XUL文件(或者修改XUL文件的DOM)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加一些DOM元素到另一个扩展的XUL文件。

该扩展程序允许通过单击添加到Firefox的工具栏按钮来打开自己。点击这个按钮打开一个新的选项卡,其中包含扩展名部分的XUL文件的chrome地址。

我已经创建了一些动态的DOM元素,并将它们添加到Firefox窗口中的元素上(例如之前提到的工具栏),通过在加载项加载时添加它们并使用窗口加载事件侦听器的窗口打开后,我的插件加载(使用 document.createElement() element.appendChild() ),但我不知道如何将动态元素添加到此XUL文件的最佳方式,该文件可以在同一个窗口中多次打开和关闭。



有没有什么方法可以在每个会话中覆盖一次XUL文件(我猜不是)?否则,我是否需要设置某种类型的页面加载侦听器,每次加载页面时都会触发,然后检查地址以查看它是否与XUL文件的地址相匹配,如果是,则执行覆盖。是否有可能监听一个特定的地址?

解决方案

我最终添加了一个 load 侦听器到 gBrowser 来侦听页面加载。侦听器为chrome XUL文件地址检查 event.originalTarget.location ,并在匹配上添加DOM元素。当我的插件被启用时,我使用 gBrowser.browsers.length gBrowser.getBrowserAtIndex()循环所有在窗口中打开的标签检查XUL是否已经打开。

为了清理,我将每个添加到XUL文件的DOM元素的引用存储在 addedByMyAddon 我添加到选项卡的内容文档的数组属性。当我的加载项被禁用时,我再次通过打开的选项卡寻找XUL文件,然后删除 addedByMyAddon 属性中的所有内容,然后删除 addedByMyAddon 属性。


I want to add some DOM elements to another extension's XUL file.

That extension allows itself to be "opened" by clicking on a toolbar button that it adds to Firefox. Clicking on this button opens a new tab with the chrome address of an XUL file that is part of the extension.

I have already created some DOM elements dynamically and added them to elements on the Firefox window (e.g. the previously mentioned toolbar) by both adding them when my add-on is loaded and using window "load" event listeners for windows opened after my add-on is loaded (using document.createElement() and element.appendChild()), but I am not sure about the best way to add dynamic elements to this XUL file that can be opened and closed multiple times in the same window.

Is there any way to overlay the XUL file one time per session (I am guessing not)? Otherwise, do I need to set up some kind of page load listener that fires every time a page loads and then checks the address to see if it matches the XUL file's address and does the overlay if so? Is it possible to listen for a specific address?

解决方案

I ended up adding a load listener to gBrowser to listen for page loads. The listener checks event.originalTarget.location for the chrome XUL file address and adds the DOM elements on a match. When my add-on is enabled, I use gBrowser.browsers.length and gBrowser.getBrowserAtIndex() to loop through all open tabs in a window to check if the XUL is already open.

For clean up, I store a reference to each DOM element added to the XUL file in a addedByMyAddon array property that I add to the tab's content document. When my add-on is disabled, I loop through the open tabs again looking the for the XUL file and then delete everything in the addedByMyAddon property and then delete the addedByMyAddon property.

这篇关于如何覆盖引导加载项中的另一个扩展XUL文件(或者修改XUL文件的DOM)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆