我是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之后就退出了。警报将会显示,但即使我在那里设置了断点,它也会直接经过它。任何帮助都是非常感谢的。
发布于 2013-10-26 19:19:27
场景1:大量动态生成的链接(使用 )获取
//...
//@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');
}https://stackoverflow.com/questions/19529532
复制相似问题