我不明白为什么 Greasemonkey 脚本中的 GM_xmlhttpRequest 的 URL 不会改变
我遇到了一个非常令人沮丧的问题,我希望有人可以帮助我。这是我的 Greasemonkey 脚本的一部分,我不明白为什么异步请求总是发送到同一个 URL。
function parse(details) {
var element = $(details);
var coll = element.find("#my valid selector");
$.each(coll, function(index, href) {
SendData(href);
});
}
function SendData(url) {
GM_xmlhttpRequest ({
method: 'GET',
url: url,
headers: {
'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
'Accept': 'application/atom+xml,application/xml,text/xml',
},
onload: function(responseDetails) {
doSomething(responseDetails.responseText);
}
});
}
当我启动 Fiddler 时,我可以看到无论我的收藏中有多少项目,它都会发出相同的请求。无论第一个链接是什么,所有请求都会发送到该链接。我已验证 parse 方法每次都会成功地将不同的链接传递给 SendData 函数,但请求始终发送到集合中的第一个 URL。
我认为我所拥有的与我在此处找到的类似,但也许我'我缺少一些东西。任何帮助将不胜感激。
I'm having a really frustrating problem I hope someone can help me with. Here is a piece of my Greasemonkey script, I can't figure out why the asynchronous requests are always sent to the same URL.
function parse(details) {
var element = $(details);
var coll = element.find("#my valid selector");
$.each(coll, function(index, href) {
SendData(href);
});
}
function SendData(url) {
GM_xmlhttpRequest ({
method: 'GET',
url: url,
headers: {
'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
'Accept': 'application/atom+xml,application/xml,text/xml',
},
onload: function(responseDetails) {
doSomething(responseDetails.responseText);
}
});
}
When I fire up Fiddler, I can see that it makes the same request no matter how many items are in my collection. Whatever the first link is, all requests are made to the that link. I have verified that the parse method successfully passes a different link to the SendData function every time, but the requests are always made to the first URL in the collection.
I thought what I had was similar to what I found here, but maybe I'm missing something. Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎
url
没有在 closure 中捕获,因此除了第一个GM_xmlhttpRequest
运行之外,其他所有运行都未定义。修改
SendData()
,如下所示:应该足够了。
或者,您可以按顺序获取页面。将
parse()
更改为:It seems as though
url
is not getting captured in a closure, so it's undefined for all but the firstGM_xmlhttpRequest
run.Modifying
SendData()
, like so:should be enough.
Or, you can get the pages sequentially. Change
parse()
to something like: