代码之家  ›  专栏  ›  技术社区  ›  GeoffreyF67

是否有方法将值传递给GM_xmlhttprequest?

  •  2
  • GeoffreyF67  · 技术社区  · 17 年前

    如图所示:

    How can I return a value from GM_xmlhttprequest?

    我有一个异步脚本。我想向这个函数传递一个值,这样当调用onload函数时,我就可以使用它在网页中显示。

    我面临的挑战是,每次我将这个值传递给函数时,它都会发生变化。

    例如,如果我传入“abc”、“def”、“xyz”。

    我最终会

    xyz
    xyz
    xyz
    

    而不是

    abc
    def
    xyz
    

    所以,我的问题是,我如何将一个值传递给这个函数,以便函数的每次调用都知道完成后显示什么?

    1 回复  |  直到 9 年前
        1
  •  5
  •   Community Mohan Dere    9 年前

    您正在寻找 closure :-

    var urls = {"abc": "http://somehost/aurl",
               "def": "http://somehost/otherurl",
               "ghi": "http://someotherhost/aurl" }
    
    for (var k in urls)
    {
    
        GM_xmlhttpRequest({
            method: 'GET',
            url: urls[k],
            onload: function(text) {
                return  function(xhr) {
                    //Do stuff with xhr responseText etc and the text parameter
                    alert(text)
                }
            }(k)
        }
    }
    

    这将在每个未完成的请求完成后提醒“abc”、“def”和“ghi”。

    推荐文章