$(document).ajaxComplete不会在内容脚本中触发 [英] $(document).ajaxComplete doesn't fire inside of content script

查看:205
本文介绍了$(document).ajaxComplete不会在内容脚本中触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google Chrome扩展程序为我经常访问的网站提供一些其他功能,如果AJAX请求成功,我会尝试重新运行脚本。由于网站的性质,这是必要的。

I'm working on a Google Chrome extension to offer some additional functionality to a website I frequent, which has me trying to make a script run again if ever an AJAX request is successful. This is necessary due to the nature of the site.

接下来:我想做的就是简单地说,只要AJAX请求成功就触发一个函数。因此,我在相关网站上制作了以下内容并在控制台中运行:

Moving on: what I want to do is, simply put, to trigger a function whenever an AJAX request is successful. I therefore fashioned the following and ran it in the console while on the relevant website:

$(document).ajaxSuccess(function() {
   console.log('AJAX success');
});

然后我继续使用该网站,每次通过AJAX加载新内容时我确实做到了通过 .ajaxSuccess 获得确认,到目前为止一切都很好。

I then proceeded to use the website and each time new content was loaded by means of AJAX I did indeed get confirmation of it by .ajaxSuccess, all good so far.

然后我开始将所有各种各样的点点滴滴放在一起,使其成为一个合适的Chrome扩展程序。一切仍然有用,除了一件事:当 .ajaxSuccess 位在内容脚本中时(我确定已正确执行等等)网站上的AJAX请求没有更长时间触发它。

I then went ahead and started putting all my various bits and pieces together to make it into a proper Chrome extension. Everything still works save one thing: when the .ajaxSuccess bit is in a content script (which I've made sure is properly executed and so forth) AJAX requests on the website no longer triggers it.

我试图理解它,但我没有太多运气。我觉得扩展可能需要一些特殊权限来访问与AJAX相关的东西,尽管我不知道那可能是什么。截至目前,扩展程序的权限如下:

I've tried to make sense out of it but I haven't much luck. I feel it might be possible the extension requires some special permission to access AJAX related things, though I wouldn't know what that might be. As of currently the permissions of the extension are the following:

"permissions": [
    "storage", "tabs", "http://website.com/*"
]


推荐答案

我最终制定了一个有点不愉快的解决方法:

I ended up fashioning a somewhat unpleasant workaround akin to this:

var DOMTimeout = null;
    $('.class').bind('DOMNodeInserted', function() {
        if(DOMTimeout)
            clearTimeout(DOMTimeout);

    DOMTimeout = setTimeout(function() { console.log('AJAX probably added something'); }, 150);
});

这篇关于$(document).ajaxComplete不会在内容脚本中触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆