admin管理员组

文章数量:1287597

I am building a chrome extension using Content Script.

I have a piece of code that injects DOM elements upon success of all ajax request on the page using jQuery. How can you recreate this without jQuery? Please note that I cannot modify any ajax requests on the page.

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);
        }
    }
});
}

Is this possible?

I am building a chrome extension using Content Script.

I have a piece of code that injects DOM elements upon success of all ajax request on the page using jQuery. How can you recreate this without jQuery? Please note that I cannot modify any ajax requests on the page.

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);
        }
    }
});
}

Is this possible?

Share Improve this question edited Jul 27, 2017 at 2:31 xoail asked Jul 27, 2017 at 2:21 xoailxoail 3,0645 gold badges39 silver badges71 bronze badges 17
  • 3 To be clear-- jQuery is just a library written in JavaScript-- anything possible with jQuery is possible with JavaScript because under the hood it is JavaScript. – Alexander Nied Commented Jul 27, 2017 at 2:22
  • 1 Possible duplicate of AJAX Complete Handler in Pure JS – Hemant Pawar Commented Jul 27, 2017 at 2:24
  • 1 Yes you do. Simply use XMLHttpRequest instead. – Obsidian Age Commented Jul 27, 2017 at 2:27
  • 3 I was able to do is listen in all successful ajax responses - No, you're able to listen to all jquery successful responses, not all ajax responses, because AJAX is not just a jquery concept - i.e if some code uses XMLHttpRequest or fetch, then jQuery has no knowledge of it ... so, if all your AJAX is written in jQuery, you already need jQuery, so use jQuery – Jaromanda X Commented Jul 27, 2017 at 2:30
  • 1 @xoail you may want to look at developer.mozilla/en/docs/Web/API/XMLHttpRequest/… – Hemant Pawar Commented Jul 27, 2017 at 2:32
 |  Show 12 more ments

1 Answer 1

Reset to default 8

Update

If you're writing a Chrome extension, you should probably use the chrome.webRequest API. See https://developer.chrome./extensions/webRequest


You can override one of the existing methods required to make an AJAX request such as XMLHttpRequest.prototype.send to add your own load event listener. For example

(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)
    }
})()

As mentioned in the ments, this won't cover the fetch API.

本文标签: jqueryajaxComplete in pure JavaScriptStack Overflow