我不明白为什么 Greasemonkey 脚本中的 GM_xmlhttpRequest 的 URL 不会改变

发布于 2024-10-14 07:55:42 字数 1026 浏览 1 评论 0原文

我遇到了一个非常令人沮丧的问题,我希望有人可以帮助我。这是我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生生漫 2024-10-21 07:55:42

似乎 url 没有在 closure 中捕获,因此除了第一个 GM_xmlhttpRequest 运行之外,其他所有运行都未定义。

修改 SendData(),如下所示:

function SendData(url)
{
    var moreSubstantial = url + " ";

    GM_xmlhttpRequest(
    {
        method:     'GET',
        url:        moreSubstantial,

应该足够了。


或者,您可以按顺序获取页面。将 parse() 更改为:

function parse (details)
{
    var element     = $(details);
    var coll        = element.find("#my valid selector");
    var TargetPages = coll.map (function() {return this.href;} );

    (function getNextPage (J)
    {
        var PageURL = TargetPages[J];

        GM_xmlhttpRequest
        ( {
            method:     "GET",
            url:        PageURL,
            headers:    {
                            'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
                            'Accept': 'application/atom+xml,application/xml,text/xml',
                        },
            onload:     function (responseDetails)
                        {
                            doSomething (responseDetails.responseText);

                            if (--J >= 0)
                                getNextPage (J);
                        }
        } );
    } ) (TargetPages.length - 1);
 }

It seems as though url is not getting captured in a closure, so it's undefined for all but the first GM_xmlhttpRequest run.

Modifying SendData(), like so:

function SendData(url)
{
    var moreSubstantial = url + " ";

    GM_xmlhttpRequest(
    {
        method:     'GET',
        url:        moreSubstantial,

should be enough.


Or, you can get the pages sequentially. Change parse() to something like:

function parse (details)
{
    var element     = $(details);
    var coll        = element.find("#my valid selector");
    var TargetPages = coll.map (function() {return this.href;} );

    (function getNextPage (J)
    {
        var PageURL = TargetPages[J];

        GM_xmlhttpRequest
        ( {
            method:     "GET",
            url:        PageURL,
            headers:    {
                            'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
                            'Accept': 'application/atom+xml,application/xml,text/xml',
                        },
            onload:     function (responseDetails)
                        {
                            doSomething (responseDetails.responseText);

                            if (--J >= 0)
                                getNextPage (J);
                        }
        } );
    } ) (TargetPages.length - 1);
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文