0

我正在构建一个GreaseMonkey测试脚本,每次访问特定站点时都会生成GM_xmlhttpRequest。 GM_xmlhttpRequest只应该在找到的第一个“文档”(父窗口)上触发,它应该忽略iframe和其他子窗口(我不需要iframe的url)。GM_xmlhttpRequest - 父窗口和Iframe窗口上的触发器 - 只能在父窗口上触发

一些想法和可能的解决方案:

1)我试图插入在GM_xmlhttpRequest的onload回调休息。结果:脚本不响应。 FireBug中没有错误消息。 (我想休息只能在循环。)

onload: function(response) { 
    if (response.status == 200) break; 
} 

2)前插入一个的addEventListener /在GM_xmlhttpRequest后: 结果:脚本不响应。 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); 
    } 
}); 

回答

0

这就是所谓的frame buster script

if (window === parent) { 
    // Trigger! 
} 
+0

伟大的作品!谢谢.. :-) – 2010-01-22 15:01:44