Pha*_*une 2 synchronization greasemonkey gm-xmlhttprequest tampermonkey
我试图接到一个GM_xmlhttpRequest同步行为的电话,但我不能像我期望的那样让它工作:
function myFunction (arg) {
var a;
GM_xmlhttpRequest ( {
method: "GET",
url: "http://example.com/sample/url",
synchronous: true,
onload: function (details) {
a = details.responseText;
}
} );
return a;
}
b = myFunction ();
alert (b);
Run Code Online (Sandbox Code Playgroud)
我从来没有得到任何回报b; 这是未定义的.我在这里缺少一些步骤吗?
我使用的是Greasemonkey的v0.9.13和Firefox的v9.0.1.
在Google中偶然发现了这个话题.
同步GM_xmlhttpRequest返回结果,而不是在onload-callback中执行它.
所以这是对的:
var details = GM_xmlhttpRequest({
method:"GET",
url:"http://site.com/sample/url",
synchronous: true
});
a = details.responseText;
Run Code Online (Sandbox Code Playgroud)
你在开头创建var"a",永远不要填充它并返回它.因此,它是未定义的.