使用document.location.href加载一组URL's,然后仅在加载页面时执行JS
Load a set of URL's with document.location.href and then execute JS only if the page was loaded
我正在做一个FireFox扩展。我有一个URL数组。我想要的是:用document.location.href
重定向我拥有的所有URL的页面,然后,用我拥有的XPath,计算页面拥有的元素数量。
问题是,我必须等待页面加载完成,然后才能使用XPath执行count方法。这是我的代码:
var actualResult = 0;
actualResult = content.document.evaluate('count('+taskModeler.ui.getStatus()+')', content.document, null, XPathResult.NUMBER_TYPE, null).numberValue;
for(var i=0; i<taskModeler.ui.getPageContent().length;i++) {
content.document.location.href = taskModeler.ui.getPageContent()[i].getAttribute("href");
//only if the page was loaded execute the next line
actualResult+=content.document.evaluate('count('+taskModeler.ui.getStatus()+')',
content.document, null, XPathResult.NUMBER_TYPE, null).numberValue;
}
getPageContent()
返回一个数组,其中包含URL的的集合。对于每个URL,我都必须加载页面以访问文档,然后计算返回XPath的次数。但是,如果页面没有加载,count方法不起作用,因为返回的是实际页面的结果,而不是content.document.location
中加载的页面。
我该怎么做呢?
您需要使用进度侦听器读取此
const STATE_START = Components.interfaces.nsIWebProgressListener.STATE_START;
const STATE_STOP = Components.interfaces.nsIWebProgressListener.STATE_STOP;
var myListener =
{
QueryInterface: function(aIID)
{
if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
aIID.equals(Components.interfaces.nsISupports))
return this;
throw Components.results.NS_NOINTERFACE;
},
onStateChange: function(aWebProgress, aRequest, aFlag, aStatus)
{
// If you use myListener for more than one tab/window, use
// aWebProgress.DOMWindow to obtain the tab/window which triggers the state change
f (aFlag & Components.interfaces.nsIWebProgressListener.STATE_IS_DOCUMENT) {
{
if(aFlag & STATE_STOP)
{
// This fires when the load page finishes
}
}
},
onLocationChange: function(aProgress, aRequest, aURI)
{
// This fires when the location bar changes; that is load event is confirmed
// or when the user switches tabs. If you use myListener for more than one tab/window,
// use aProgress.DOMWindow to obtain the tab/window which triggered the change.
},
// For definitions of the remaining functions see related documentation
onProgressChange: function(aWebProgress, aRequest, curSelf, maxSelf, curTot, maxTot) { },
onStatusChange: function(aWebProgress, aRequest, aStatus, aMessage) { },
onSecurityChange: function(aWebProgress, aRequest, aState) { }
}
如果你需要更多的帮助,告诉我。
相关文章:
- AngularJS加载JSON数据,然后从中解析/加载HTML
- 先在Angular中加载配置文件,然后再加载其他文件
- 加载文件,然后调用回调函数
- 如何更改javascript的一个变量,然后运行;“加载”;而无需重新加载页面
- 下拉重新加载页面,然后检索值
- 先加载特定的图像,然后再加载页面的其余部分
- 加载页面,然后在页面加载时执行 javascript
- Angular js,在选择元素上,我想 POST 数据以将其保存在数据库中,然后我想使用 PUT 更新它而无需重新加载
- 使用 jQuery 加载 JSON,然后将其显示给查看器
- 淡出加载然后淡入不起作用
- 可以进行现场重新加载,然后触发点击功能
- 在RadWindow中加载新页面,然后从加载的窗体中关闭窗口
- 加载在JSBN中创建的RSA公钥,然后加密消息
- 加载时可以单击按钮#1,然后需要单击按钮#2两次.按钮#1也需要点击两次
- 地图,rails 4.2,javascript,鼠标悬停,只工作一次(或两次).然后在重新加载之后
- 如何在jQuery中进行页面加载,然后点击
- removeChild,然后再次添加以前删除的Child以重新加载并清理它
- jQuery UI#Accordion加载然后fadeIN..怎样
- 中间的页脚加载然后粘在底部
- 加载然后换行,但是换行在jQuery中不起作用