GM_xmlhttpRequest - 在父窗口和 Iframe 窗口上触发 - 应该仅在父窗口上触发
我正在构建一个 GreaseMonkey 测试脚本,每次访问特定站点时都会生成 GM_xmlhttpRequest 。 GM_xmlhttpRequest 应该只在找到的第一个“文档”(父窗口)上触发,并且应该忽略 iframe 和其他子窗口(我不想要 iframe 的 url)。
一些想法和可能的解决方案:
1)我尝试在 GM_xmlhttpRequest onload-callback 中插入一个中断。结果:脚本没有响应。 FireBug 中没有错误消息。 (我猜break只能在循环中起作用。)
onload: function(response) {
if (response.status == 200) break;
}
2)在GM_xmlhttpRequest之前/之后插入一个addEventListener: 结果:脚本没有响应。 FireBug 中没有错误消息。
document.addEventListener("load",
// (Insert the GM_xmlhttpRequest code here - see below)
,false);
3)想法:第一次成功请求后GM_xmlhttpRequest是否可以“取消”?无论是在 onload 部分,还是在脚本之后(例如 document.removeEventListener 取消 document.addEventListener)。
4)思路:GreaseMonkey能识别父窗口吗?所以脚本只在父窗口中运行?
另外,我不想将脚本作为同步调用,因为它会减慢速度。
// ==UserScript==
// @name GreaseMonkey xmlhttpRequest test
// @include http://www.jp.dk/*
// ==/UserScript==
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);
}
});
I am building a GreaseMonkey test script that makes a GM_xmlhttpRequest each time a specific site is visited. GM_xmlhttpRequest should only trigger on the first "document" found (the parent window) and it should ignore iframes and other child windows (I don't want the url for the iframes).
Some thoughts and possible solutions:
1) I tried to insert a break in the GM_xmlhttpRequest onload-callback. Result: The script does not respond. No error message in FireBug. (I guess break only works in loops.)
onload: function(response) {
if (response.status == 200) break;
}
2) Insert an addEventListener before/after the GM_xmlhttpRequest:
Result: The script does not respond. No error message in FireBug.
document.addEventListener("load",
// (Insert the GM_xmlhttpRequest code here - see below)
,false);
3) Idea: Can the GM_xmlhttpRequest be "cancelled" after the first successful request? Either in the onload-part, or after the script (like document.removeEventListener cancels document.addEventListener).
4) Idea: Can GreaseMonkey identify the parent window? so the script only runs in the parent window?
Also, I would prefer not to make the script as a synchronous call since it will slow things down.
// ==UserScript==
// @name GreaseMonkey xmlhttpRequest test
// @include http://www.jp.dk/*
// ==/UserScript==
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);
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这称为帧破坏脚本:
This is called frame buster script: