我正在尝试编写一个适用于 Gmail 的 Greasemonkey 脚本。我知道如何创建响应用户单击收件箱链接或刷新链接的 javascript。我的问题是 Gmail 会定期用新对话刷新收件箱,而我无法捕获此事件。有没有办法在 javascript 中捕获周期性的 Ajax 事件?
Ben Mills
问问题
1257 次
2 回答
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);
}
于 2009-03-05T00:30:52.933 回答
1
我在上面尝试了 Miles 的出色建议,但不幸的是它不起作用,因为在我有机会在我的 Greasemonkey 脚本中更改它之前,Gmail 已经调用了原始的 setTimeout 函数。
我唯一能做的就是以某种方式对 Gmail 在定期刷新收件箱时所做的更改做出反应。我发现添加或删除节点时会触发几个与 DOM 相关的事件:
http://www.w3.org/TR/DOM-Level-3-Events/events.html#event-DOMNodeInserted
由于 Gmail 正在使用我最新的电子邮件更新 DOM,因此我可以侦听这些 DOM 事件(我正在使用 DOMNodeInserted)并对更改做出反应。
它并不优雅,但它有效。
于 2009-03-06T03:11:54.707 回答