Can a Chrome extension monitor XHR events (or other events) from the page it's running against?

跟風遠走 提交于 2019-12-23 11:55:21

问题


I'm building a Chrome extension that manipulates the content of the page, but the content I'm interested in only shows up when the user has clicked a button and the data has then been loaded from an Ajax call.

At the moment the extension monitors the page with a SetTimeout but it's clumsy.

Can an extension know when an Ajax call has been initiated, and when it ended? or can the extension somehow receive events from the page?


回答1:


I don't know an easy way to monitor XHR requests, but you can monitor any changes being made to a page structure by listening to DOMSubtreeModified event. So when ajax call is done and elements are added/removed/changed on a page - you will be notified.

document.addEventListener("DOMSubtreeModified", function(event){
        //something has changed
});


来源:https://stackoverflow.com/questions/4277908/can-a-chrome-extension-monitor-xhr-events-or-other-events-from-the-page-its-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!