简体   繁体   English

Firefox插件:隐藏 <browser /> 在XUL叠加中?

[英]Firefox Addons: Hidden <browser /> in XUL Overlay?

I'm trying to load and manipulate a hidden <browser /> tag in my overlay (part of my addon's functionality) in my Firefox Addon. 我正在尝试在Firefox插件中的叠加层(插件功能的一部分)中加载和操作隐藏的<browser />标记。 But, I can't access any of the elements I add in my overlay from document . 但是,我无法访问我从document添加到叠加层中的任何元素。

For example, this isn't working: 例如,这不起作用:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="chrome://foxy_bucks/skin/overlay.css" type="text/css"?>
<!DOCTYPE overlay SYSTEM "chrome://foxy_bucks/locale/overlay.dtd">
<overlay id="foxy_bucks-overlay" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
    <browser id="bContainer" src="http://google.com/"></browser>
    <script type="text/javascript">
        window.addEventListener("load", function(){
            alert(document.bContainer.src);
        }, false);
    </script>
</overlay>

Could someone point me into the right direction? 有人可以指出我正确的方向吗?

Overlays always have to extend an existing element. 叠加层始终必须扩展现有元素。 If you have a tag at the top level of an overlay with an ID that doesn't yet exist in the document then this element is simply ignored ( <script> tags being a noteworthy exception from the rule). 如果您在覆盖层的顶层有一个标签,而该标签的ID在文档中尚不存在,则该元素将被忽略( <script>标签是该规则中值得注意的例外)。 This happens in your case, the ID bContainer doesn't exist in the document you are overlaying so your <browser> tag is simply ignored. 在您的情况下会发生这种情况,您要覆盖的文档中不存在ID bContainer ,因此您的<browser>标记将被忽略。 This mechanism allows for example having content for the Firefox and SeaMonkey Tools menu in the same overlay - this menu has a different ID in Firefox and SeaMonkey so the section overlaying the SeaMonkey menu is simply ignored in Firefox and vice versa. 例如,此机制允许在同一叠加中包含Firefox和SeaMonkey Tools菜单的内容-此菜单在Firefox和SeaMonkey中具有不同的ID,因此在Firefox中,忽略SeaMonkey菜单的部分将被忽略,反之亦然。

If you want to add an element to the document then you need to overlay its root element. 如果要向文档中添加元素,则需要覆盖其根元素。 For the Firefox browser window it would look like this (note that main-window is the ID of the root element): 对于Firefox浏览器窗口,它将如下所示(请注意, main-window是根元素的ID):

<overlay id="foxy_bucks-overlay" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <window id="main-window">
    <browser id="bContainer" src="http://google.com/"></browser>
  </window>
  ...
</overlay>

A side-note: to access an element by its ID you need to use document.getElementById() : 旁注:要通过元素ID访问元素,您需要使用document.getElementById()

alert(document.getElementById("bContainer").src);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM