我是Javascript的新手。我试图导航到一个页面并“刮”屏幕。我正在使用Firefox,Greasemonkey和Firebug。我试图使用location.href这可能是问题。我想导航到一个页面,解析内容,使用内容导航到其他页面。这是一个例子(我的网站不同,但我得到相同的错误/结果):
location.href='http://www.w3schools.com/html/html_examples.asp';
/* parse and find text */
location.href='http://www.w3schools.com/html/tryit.asp?filename=tryhtml_intro';
alert('finished');
无论我做什么,Firebug / Greasemonkey都会在第一个location.href之后退出。警报将显示,但即使我在那里设置了断点,它也会直接通过它。非常感谢任何帮助。
答案 0 :(得分:0)
场景1:大量动态生成的链接(使用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');
}