这是代码块。 Facebook页面会一直加载,直到找到字符串“结果结尾”,然后该函数退出。有时这需要30多分钟的加载时间,因此我想像这样
如果(找到“结果结尾”字符串或5分钟过去了)
因此,如果较早找到字符串,这将限制为5分钟或更短的时间。谢谢
var nodes = [].slice.call(document.getElementsByClassName('_64f'));
var bottomNode = nodes.filter(function isBottomNode(el) {
return !!(
el &&
el.textContent &&
el.textContent.indexOf('End of Results')=== 0
);
});
return !!bottomNode.length;
}
答案 0 :(得分:0)
抱歉,我的空闲时间很短。
请尝试以下代码。我在其中添加了一些重要说明作为内联注释(您可以通过单击Run code snippet
按钮来检查结果):
window.onload=function(){
//This is the element that 'End Of Results' will add to it
var mC = document.getElementById('must-capture');
var ti2=setTimeout(function(){
alert("timeout!! not found!");
}, 10000/*for 5 minutes use 5*60*1000*/);
var obs=new MutationObserver(function(list){
var fnd=0;
list.every(function(c){
Array.prototype.every.call(c.addedNodes, function(n){
if(n.innerHTML.indexOf("End Of Results")>-1){
obs.disconnect()
fnd=1;
return false;
}
});
if(fnd) return false;
});
if(fnd) {
clearTimeout(ti2);
//put the codes you need here...
console.log("It's just same time that 'End Of Results' is ready to be added!!");
}
});
obs.observe(mC, { characterData: true, childList: true, subtree: true });
//TIP:
//Now we need to simulate adding new contents to page with specified intervals. one of this contents is same "End Of results" text. (this part is not needed in final project and will happen automatically as you said in your original question):
var i=0,
texts=["hello", "salam", "test", "sample", "tavakkol", "bttola", "End Of Results"];
var ti=setInterval(function(){
var d=document.createElement("div");
d.innerHTML=texts[i++];
mC.appendChild(d);
if(i==7) clearInterval(ti);
}, 500);
};
<!-- The element that 'End Of Results' will be added to it.
this must be the wrapper element that desired text will be added to it later. -->
<div id='must-capture'></div>
但是,如果您需要代码在IE9上运行,或者由于任何原因想要使用timers
,则必须将两个计时器组合在一起。一个用于短暂延迟(例如,每2秒)运行的程序,您必须在此处运行上述代码(用于查找子项并检查现有的“结果结尾”文本)。在它的主体中,您必须调用另一个计时器的clearTimeout
,您必须创建它才能在5分钟后运行。
答案 1 :(得分:0)
仅关注5分钟延迟。您可以使用javascript setTimeout()延迟5分钟。
var nodes = [].slice.call(document.getElementsByClassName('_64f'));
var bottomNode = nodes.filter(function isBottomNode(el, isForced = false) {
return !!(
(el && el.textContent && el.textContent.indexOf('End of Results')=== 0)
|| isForced === true
);
});
return !!bottomNode.length;
}
这是现在的设置超时时间,
var isForced = false;
setTimeout(function(){
isForced = true;
}, 60*5);
// When calling the function
isBottomNode(el, isForced);