从基于xul的Firefox扩展中加载隐藏的iframe中的多个页面 [英] Load multiple pages in a hidden iframe from a xul-based firefox extension
本文介绍了从基于xul的Firefox扩展中加载隐藏的iframe中的多个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从一个基于xul的firefox插件,我需要:
- 以编程方式创建一个不可见的iframe(一次)
- 在插件运行时重复使用它来加载多个URL
- 在每个URL加载后访问返回的HTML
问题:我只能得到任何创建的iframe的第一个页面加载,以触发onload或DOMContentLoaded事件。对于后续的URL,不会触发事件。
注意:如果可以的话,我也可以使用hiddenDOMWindow本身...
代码:
var url = ['http://en.wikipedia.org/wiki/Internet','http:// en.wikipedia.org/wiki/IPv4','http://en.wikipedia.org/wiki/Multicast'];
visitPage(urls.pop());
函数visitPage(url){
var XUL_NS =http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul;
var hiddenWindow = Components.classes [@ mozilla.org/appshell/appShellService;1\"].getService
(Components.interfaces.nsIAppShellService).hiddenDOMWindow;
var doc = hiddenWindow.document,iframe = doc.getElementById(my-iframe);
if(!iframe)
{
iframe = doc.createElement(iframe);
// OR:iframe = doc.createElementNS(XUL_NS,iframe);
iframe.setAttribute(id,my-iframe);
iframe.setAttribute('style','display:none');
iframe.addEventListener(DOMContentLoaded,function(e){
dump('DOMContentLoaded:'+ e.originalTarget.location.href);
visitPage(urls.pop ());
});
doc.documentElement.appendChild(iframe);
}
iframe.src = url;
解决方案
有一些陷阱: / p>
-
hiddenWindow
在不同的平台上有所不同。这是Mac上的XUL和HTML的其他部分。
- 您应该使用
.setAttribute(src,url);
来可靠地导航。
下面的例子适用于我(Mac,Win7):
var urls = [
'http://en.wikipedia.org/wiki/Internet',
'http://en.wikipedia.org/wiki/ IPv4',
'http://en.wikipedia.org/wiki/Multicast'
];
var hiddenWindow = Components.classes [@ mozilla.org/appshell/appShellService;1]。
getService(Components.interfaces.nsIAppShellService)。
hiddenDOMWindow;
函数visitPage(url){
var iframe = hiddenWindow.document.getElementById(my-iframe);
if(!iframe){
//总是使用html。隐藏的窗口可能是XUL(Mac)
//或只是html(其他平台)。
iframe = hiddenWindow.document。
createElementNS(http://www.w3.org/1999/xhtml,iframe);
iframe.setAttribute(id,my-iframe);
iframe.addEventListener(DOMContentLoaded,function(e){
console.log(DOMContentLoaded:+
e.originalTarget.location);
var u = url。 ();
visitPage(u);
}
});
//确保实际上还有一些东西需要加载。
hiddenWindow.document.documentElement.appendChild(iframe);
}
//使用.setAttribute()可靠地导航iframe。
iframe.setAttribute(src,url);
}
visitPage(urls.pop());
不要重新载入 hiddenWindow
本身,或者你会打破很多其他的代码。
From a xul-based firefox addon, I need to:
- programmatically create an invisible iframe (once)
- reuse it to load multiple URLs as the addon runs
- access the returned HTML after each URL loads
Problem: I can only get the first page-load for any created iframe to trigger an 'onload' or 'DOMContentLoaded' event. For subsequent URLs, there is no event triggered.
Note: I'm also fine with using the hiddenDOMWindow itself if this is possible...
Code:
var urls = ['http://en.wikipedia.org/wiki/Internet', 'http://en.wikipedia.org/wiki/IPv4', 'http://en.wikipedia.org/wiki/Multicast' ];
visitPage(urls.pop());
function visitPage(url) {
var XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
var hiddenWindow = Components.classes["@mozilla.org/appshell/appShellService;1"].getService
(Components.interfaces.nsIAppShellService).hiddenDOMWindow;
var doc = hiddenWindow.document, iframe = doc.getElementById("my-iframe");
if (!iframe)
{
iframe = doc.createElement("iframe");
//OR: iframe = doc.createElementNS(XUL_NS,"iframe");
iframe.setAttribute("id", "my-iframe");
iframe.setAttribute('style', 'display: none');
iframe.addEventListener("DOMContentLoaded", function (e) {
dump('DOMContentLoaded: '+e.originalTarget.location.href);
visitPage(urls.pop());
});
doc.documentElement.appendChild(iframe);
}
iframe.src = url;
}
解决方案
There are some traps:
- The
hiddenWindow
differs between platforms. It is XUL on Mac, and HTML else. - You should use
.setAttribute("src", url);
to reliably navigate.
The following works for me (Mac, Win7):
var urls = [
'http://en.wikipedia.org/wiki/Internet',
'http://en.wikipedia.org/wiki/IPv4',
'http://en.wikipedia.org/wiki/Multicast'
];
var hiddenWindow = Components.classes["@mozilla.org/appshell/appShellService;1"].
getService(Components.interfaces.nsIAppShellService).
hiddenDOMWindow;
function visitPage(url) {
var iframe = hiddenWindow.document.getElementById("my-iframe");
if (!iframe) {
// Always use html. The hidden window might be XUL (Mac)
// or just html (other platforms).
iframe = hiddenWindow.document.
createElementNS("http://www.w3.org/1999/xhtml", "iframe");
iframe.setAttribute("id", "my-iframe");
iframe.addEventListener("DOMContentLoaded", function (e) {
console.log("DOMContentLoaded: " +
e.originalTarget.location);
var u = urls.pop();
// Make sure there actually was something left to load.
if (u) {
visitPage(u);
}
});
hiddenWindow.document.documentElement.appendChild(iframe);
}
// Use .setAttribute() to reliably navigate the iframe.
iframe.setAttribute("src", url);
}
visitPage(urls.pop());
Don't reload the hiddenWindow
itself, or you will break lots of other code.
这篇关于从基于xul的Firefox扩展中加载隐藏的iframe中的多个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文