场景一:大量动态生成的链接(使用GM_xmlhttpRequest获取)
//...
//@include http://www.w3schools.com/html/html_examples.asp
//@grant GM_xmlhttpRequest
//...
var urls = [];
//parse text to generate some links on the fly and store them in urls[]
var i = 0, numUrls = urls.length, reportEntries = [], count = 0;
for(; i < numUrls; i++) {
GM_xmlhttpRequest({
method: 'GET',
url: urls[i],
onload: function(response) {
var returnedHtml = response.responseText;
//extract more information from returnedHtml and store it in reportEntries[i]
if(++count >= numUrls) {
//print reportEntries[] to form a report
alert('finished');
}
}
})
}
注意:如果您需要将报告作为文本文件保存到本地磁盘,Greasemonkey 不是一个可行的选择,因为它没有打开本地文件的权限。尽管如此,您仍可以将其保存到 pastebin.com 等在线存储中。
场景 2:静态链接数量有限
//...
//@include http://some.landing/page
//@include http://www.w3schools.com/html/html_examples.asp
//@include http://www.w3schools.com/html/tryit.asp?filename=tryhtml_intro
//...
if('http://some.landing/page' == location.href) {
location.href = 'http://www.w3schools.com/html/html_examples.asp';
}
else if('http://www.w3schools.com/html/html_examples.asp' == location.href) {
/* parse and find text */
location.href='http://www.w3schools.com/html/tryit.asp?filename=tryhtml_intro';
}
else if('http://www.w3schools.com/html/tryit.asp?filename=tryhtml_intro' == location.href) {
alert('finished');
}