简体   繁体   English

使用javascript加载XUL资源

[英]Load XUL resource using javascript

How can I go about loading XUL resources using javascript? 如何使用javascript加载XUL资源? I tried to search on mdn but couldn't find any examples. 我试图搜索mdn但找不到任何例子。

The motivation here is that I would like to create a overlay over elements which don't have an id attribute. 这里的动机是我想在没有id属性的元素上创建叠加层。

Say for example I have the following xul file: 比方说,我有以下xul文件:

<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

<button label="Test button" oncommand="alert('Hello World!');"/>

</window>

I would like to load this xul file using javascript, grab the button and append it to another element. 我想使用javascript加载这个xul文件,抓住按钮并将其附加到另一个元素。

Help would be appreciated. 帮助将不胜感激。

You should use the regular DOM APIs. 您应该使用常规DOM API。

You can still overlay the element somewhere where an overlay is possible and later .appendChild() it where appropriate. 您仍然可以在可能覆盖的地方覆盖元素,然后在适当的时候覆盖.appendChild()

Or create it entirely using the DOM API, which is likely the better and faster way: 或者使用DOM API完全创建它,这可能是更好,更快的方式:

// XUL namespace is implied in XUL overlay scripts.
var btn = document.createElement("button");
btn.setAttribute("label", "Test Button");
btn.addEventListener("command", function() { alert("Hello World!"); }, false);
someElement.appendChild(btn);

If you want to load a local javascript file, then there is a dedicated helper for that: 如果你想加载一个本地javascript文件,那么有一个专门的帮助:

//Check this for how the url should look like :
//https://developer.mozilla.org/en/mozIJSSubScriptLoader
function loadScript( url)
{
  var loader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"].getService(Components.interfaces.mozIJSSubScriptLoader); 
  //The magic happens here
  loader.loadSubScript( url ); 
}

If you want to load a local HTML file inside your XUL application, then I dont think that is possible ( any more ) due to security risks. 如果你想在你的XUL应用程序中加载一个本地HTML文件,那么由于安全风险我不认为这是可能的(任何更多)。

I'm not 100% sure if i understand your question completely, but i'll try to give my 2 cents. 如果我完全理解你的问题,我不是百分百肯定,但我会试着给我2美分。

My approach doesn't help you getting an element from a different xul file from the one you are using, i don't really know even if that is possible. 我的方法无法帮助您从正在使用的文件中获取不同xul文件中的元素,即使可能,我也不知道。

What i usually do when i want to use a big elements multiple times, like when i want to create for example richlistboxitems, what i do is: 当我想多次使用大元素时,我通常会做什么,比如当我想创建例如richlistboxitems时,我所做的是:

I create a template for the items: 我为项目创建了一个模板:

<richlistbox id="richlistbox"/>
<richlistitem id="richListItemTemplate" hidden="true">
   <listcell class="classOne" label="one" width="50"/>
   <listcell class="classTwo" label="two" width="80"/>
</richlistitem>

Note that i have the template hidden. 请注意,我隐藏了模板。

Then when i load the panel or page that contains the richlistbox, i dynamically fill them out doing: 然后,当我加载包含richlistbox的面板或页面时,我动态填写它们:

for(var i = 0; i < elements.length; i++) {
   var item = document.getElementById("richListItemTemplate").cloneNode(true);
   item.removeAttribute("id");
   item.removeAttribute("hidden");
   item.getElementsByClassName("classOne")[0].setAttribute("label",elements.value);
   item.getElementsByClassName("classTwo")[0].setAttribute("label",elements.value);

   document.getElementById("richlistbox").appendChild(item);
}

This way you are cloning the original element, removing the id, but you can access it's child elements through className that is unique inside that element. 这样您就可以克隆原始元素,删除id,但是您可以通过className访问它的子元素,该元素在该元素中是唯一的。

Maybe you can do the same to your button. 也许你可以对你的按钮做同样的事情。 You create a buttonTemplate with id="myButtonTemplate" and hidden="true" , and when you want to append it to an element, you do a buttonTemplate.cloneNode(true) (true because you want all the child elements), you customize it the way you want and then append it to the element that you want. 你创建一个id="myButtonTemplate"hidden="true"buttonTemplate ,当你想将它附加到一个元素时,你做一个buttonTemplate.cloneNode(true)(因为你想要所有的子元素都是true),你自定义它以你想要的方式然后将它附加到你想要的元素。

Hope this helps you. 希望这对你有所帮助。

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

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