纯JavaScript中的ajaxComplete

xoa*_*ail 2 javascript jquery google-chrome-extension

我正在使用Content Script构建chrome扩展。

我有一段代码,使用jQuery在页面上所有ajax请求成功后注入DOM元素。没有jQuery,如何重新创建它?请注意,我无法修改页面上的任何ajax请求。

if(window.jQuery){
jQuery( document ).ajaxComplete(function( event, xhr, settings ) {
    for(var i =0; i< $jq('div.handwave').length; i++){
        if($($('div.handwave')[i]).children('.done').length < 1){
            $(document).find('div.handwave').eq(i).append(wind);
        }
    }
});
}
Run Code Online (Sandbox Code Playgroud)

这可能吗?

Phi*_*hil 6

更新资料

如果要编写Chrome扩展程序,则可能应该使用chrome.webRequestAPI。参见https://developer.chrome.com/extensions/webRequest


您可以覆盖提出AJAX请求所需的现有方法之一,例如XMLHttpRequest.prototype.send添加自己的加载事件侦听器。例如

(function() {
    const send = XMLHttpRequest.prototype.send
    XMLHttpRequest.prototype.send = function() { 
        this.addEventListener('load', function() {
            console.log('global handler', this.responseText)
            // add your global handler here
        })
        return send.apply(this, arguments)
    }
})()
Run Code Online (Sandbox Code Playgroud)

如评论中所述,这不会涵盖fetchAPI。