我正在编写一个Greasemonkey用户脚本来遍历几个网页,从每个网页中收集一些html。
然而,在我移动到系列页面中的第二个页面之后,文档对象仍然引用第一页,即使我等到第二页已完全加载。如何更新或刷新文档对象以使其引用新页面?
这是我的完整脚本,其中文档对象始终引用第一页:
(function($) {
//create a button
myButton = document.createElement("input");
myButton.type = "button";
myButton.value = "Click here";
myButton.id = "display";
placeHolder = document.getElementById("someId");
placeHolder.parentNode.insertBefore(myButton, placeHolder.nextSibling);
//function to be called when button is clicked
$("#display").click(function(){
//harvest some html on page 1 and save it
my_list = document.getElementsByClassName("myClass")[0];
my_list_html=my_list.innerHTML;
localStorage.setItem('page1', my_list_html);
//find a certain URL on page 1
var nextPageLink = $(".myClass2").first().find("a");
//go to page 2
actuateLink(nextPageLink[0]);
//once page 2 has loaded, harvest some html on page 2
(function poll(){
if(document.readyState === "complete")
{
my_list2 = document.getElementsByClassName("myClass")[0]; //this should be the document object of page 2, but in fact it's still the document object of page 1
my_list_html2=my_list2.innerHTML;
console.log('Content from page 1:',localStorage.getItem('page1')); // this prints the html harvested from page 1
console.log('Content from page 2:',my_list_html2); // this also prints the html harvested from page 1
}
else
{
setTimeout(poll,500);
}
})();
});
//function for following a link
function actuateLink(link)
{
window.location.href = link.href;
}
})(jQuery);
(我正在从不同的页面中获取html,然后将它们组合在一起并在一个页面中显示它。编辑以澄清我正在运行带有firefox的Greasemonkey扩展的脚本。)