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
1 Answer
Reset to default 8Update
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
版权声明:本文标题:jquery - ajaxComplete in pure JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741233733a2362591.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论