代码之家  ›  专栏  ›  技术社区  ›  Kristoffer Bohmann

gm xmlhttprequest-父窗口和iframe窗口上的触发器-应仅在父窗口上触发

  •  0
  • Kristoffer Bohmann  · 技术社区  · 15 年前

    我正在构建一个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",
    // (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);
        }
    });
    
    1 回复  |  直到 15 年前
        1
  •  0
  •   NVI    15 年前

    这叫做 frame buster script :

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