我正在构建一个GreaseMonkey测试脚本
GM_xmlhttpRequest
每次访问特定站点时。gm_xmlhttpRequest应该只在找到的第一个“文档”(父窗口)上触发,它应该忽略iframes和其他子窗口(我不需要iframes的URL)。
一些想法和可能的解决方案:
1)我尝试在gm xmlhttprequest onload回调中插入一个中断。结果:脚本没有响应。Firebug中没有错误消息。(我想只有在循环中才可以使用break。)
onload: function(response) {
if (response.status == 200) break;
}
2)在gm畗xmlhttpRequest之前/之后插入addEventListener:
结果:脚本没有响应。Firebug中没有错误消息。
document.addEventListener("load",
,false);
3)想法:在第一次成功请求之后,GM_xmlhttpRequest是否可以被“取消”?在onload部分,或在脚本之后(如document.removeEventListener取消document.addEventListener)。
4)想法:GreaseMonkey能识别父窗口吗?所以脚本只在父窗口中运行?
另外,我不希望将脚本作为同步调用,因为它会减慢速度。
GM_xmlhttpRequest({
method: "POST",
url: "http://localhost/do_stuff",
data: "url=" + document.location.href,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
onload: function(response) {
if (response.responseText)
alert("GreaseMonkey said: " + response.responseText);
}
});