【问题标题】:How to capture the Ajax event that periodically refreshes the gmail inbox如何捕获定期刷新 gmail 收件箱的 Ajax 事件
【发布时间】:2010-10-11 09:15:45
【问题描述】:

我正在尝试编写一个适用于 Gmail 的 Greasemonkey 脚本。我知道如何创建响应用户单击收件箱链接或刷新链接的 javascript。我的问题是 Gmail 会定期用新对话刷新收件箱,我无法捕获此事件。有没有办法在 javascript 中捕获周期性的 Ajax 事件?

【问题讨论】:

    标签: javascript gmail greasemonkey


    【解决方案1】:

    您可以尝试用您自己的函数替换 window.setTimeout 函数(也可能是 window.setInterval):

    window._setTimeout = window.setTimeout;
    window.setTimeout = function(func, delay) {
        return window._setTimeout(function() {
            // Your code goes here, before the client function is called
            alert('A timeout event just fired!');
    
            if (typeof func == 'string') {
                eval(func);
            } else {
                func();
            }
        }, delay);
    }
    

    【讨论】:

    • 这是个好主意。不幸的是,到目前为止它对我不起作用。我已经尝试替换 window.setTimeout 和 window.setInterval 并且我没有收到任何警报。我想知道在我在 Greasemonkey 脚本中替换它们之前,Gmail 是否已经调用了这些函数。
    • 我什至通过在 Greasmonkey 脚本中调用它们来测试替换 setTimeout 和 setInterval 函数是否有效。正如预期的那样,我看到了警报。我的问题似乎是在 Greasemonkey 脚本运行之前,Gmail 已经调用了 setTimeout 或 setInterval。
    【解决方案2】:

    我在上面尝试了 Miles 的出色建议,但不幸的是它不起作用,因为在我有机会在我的 Greasemonkey 脚本中更改之前,Gmail 已经调用了原始的 setTimeout 函数。

    我唯一能做的就是以某种方式对 Gmail 在定期刷新收件箱时所做的更改做出反应。我发现添加或删除节点时会触发几个与 DOM 相关的事件:

    http://www.w3.org/TR/DOM-Level-3-Events/events.html#event-DOMNodeInserted

    由于 Gmail 正在使用我最新的电子邮件更新 DOM,我可以监听这些 DOM 事件(我正在使用 DOMNodeInserted)并对更改做出反应。

    它并不优雅,但它确实有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-19
      • 2012-07-19
      • 1970-01-01
      • 2011-05-30
      • 1970-01-01
      相关资源
      最近更新 更多