GreaseMonkey/Javascript:分页后继续运行
GreaseMonkey/Javascript: continue running after paginating
我正在编写一个脚本来从多页输出中抓取信息。输出在 URL: http://example.com/results.php?records_per_page=100&page=1 中分页,结果为 AJAX-loaded.
我的脚本包含 waitForKeyElements 实用程序,并且仅在加载结果后才开始抓取。抓取后会触发下一页,但不会开始抓取下一页,除非我手动重新加载页面。
我的脚本精华:
// ==UserScript==
// @name example
// @namespace http://example.com
// @description example
// @version 1
// @grant GM_xmlhttpRequest
// @grant GM_getValue
// @grant GM_setValue
// @include https://example.com/results.php?record_per_page=*&page=*
// @require https://code.jquery.com/jquery-2.2.4.min.js
// ==/UserScript==
function iterateResults () {
// iterate through all content DIVs on the page
for (i = 1; i<=$('div.row_content).length; i++) {
// parse each DIV
a = parse($(div.row_content[i]);
// issue an AJAX request to my server to save the parsed object
save (a);
}
nextPage();
}
function nextPage() {
patt = /^(.*&page=)([0-9]+)(.*)$/;
match = document.location.href.match(patt);
p = 1+match[2];
nextURL=match[1]+p+match[3];
document.location = nextURL;
}
/**
* Wait until all AJAX elements have loaded
*/
function Timer() {
timer = GM_getValue("timer","")
window.clearTimeout(timer);
timer = window.setTimeout(function(){
GM_setValue("timer","");
iterateResults();
},5000);
GM_setValue("timer",timer);
}
waitForKeyElements ("div.results_lists", Timer, false);
这在 page=1 上运行良好,但在使用 document.location 加载 page=2 后停止。
我做错了什么?
我最终找到了答案,而且非常简单。这些页面是通过 AJAX 加载的,添加一个虚拟函数触发了第二个和后续页面的抓取:
window.addEventListener ("hashchange", fireOnNewPage, false);
function fireOnNewPage () {
// console.log ("Fired");
}
我正在编写一个脚本来从多页输出中抓取信息。输出在 URL: http://example.com/results.php?records_per_page=100&page=1 中分页,结果为 AJAX-loaded.
我的脚本包含 waitForKeyElements 实用程序,并且仅在加载结果后才开始抓取。抓取后会触发下一页,但不会开始抓取下一页,除非我手动重新加载页面。
我的脚本精华:
// ==UserScript==
// @name example
// @namespace http://example.com
// @description example
// @version 1
// @grant GM_xmlhttpRequest
// @grant GM_getValue
// @grant GM_setValue
// @include https://example.com/results.php?record_per_page=*&page=*
// @require https://code.jquery.com/jquery-2.2.4.min.js
// ==/UserScript==
function iterateResults () {
// iterate through all content DIVs on the page
for (i = 1; i<=$('div.row_content).length; i++) {
// parse each DIV
a = parse($(div.row_content[i]);
// issue an AJAX request to my server to save the parsed object
save (a);
}
nextPage();
}
function nextPage() {
patt = /^(.*&page=)([0-9]+)(.*)$/;
match = document.location.href.match(patt);
p = 1+match[2];
nextURL=match[1]+p+match[3];
document.location = nextURL;
}
/**
* Wait until all AJAX elements have loaded
*/
function Timer() {
timer = GM_getValue("timer","")
window.clearTimeout(timer);
timer = window.setTimeout(function(){
GM_setValue("timer","");
iterateResults();
},5000);
GM_setValue("timer",timer);
}
waitForKeyElements ("div.results_lists", Timer, false);
这在 page=1 上运行良好,但在使用 document.location 加载 page=2 后停止。
我做错了什么?
我最终找到了答案,而且非常简单。这些页面是通过 AJAX 加载的,添加一个虚拟函数触发了第二个和后续页面的抓取:
window.addEventListener ("hashchange", fireOnNewPage, false);
function fireOnNewPage () {
// console.log ("Fired");
}