Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Block and Modify AJAX requests using webRequest

I have a chrome extension and am currently using the webrequest api. My question is simple, is it possible to block and modify an AJAX request with the webrequest api? If not is there a work around to this?

PS. I am trying to inject pieces of HTML into a GMail email but the only solution I have found so far involves detecting all the various types of composes and then inject my HTML. This method is fine except its unstable, only about 70% of the time the insertion works.

If there is a better way, do let me know, thanks!

EDIT

Found a few alternate solutions if anyone needs it.

  • Override the default XMLHttpRequest.send method to broadcast an event. You can modify the request before it gets sent out.
  • Override the default form submit method and intercept all form submits before they are submitted (modify the form data)
like image 476
1337holiday Avatar asked Oct 03 '22 23:10

1337holiday


2 Answers

There is currently no way to access or modify the response body using the webRequest api. There is an open issue in chromium requesting that feature. They have recently implemented the ability of accessing the request body, so there may be a chance they will also add this too.

You may have better luck with techniques that modify the XMLHttpRequest object, as described in this answer. Here you have a step by step case study where the AJAX response is intercepted and modified.

like image 103
rsanchez Avatar answered Oct 25 '22 19:10

rsanchez


Although you can detect XMLHttpRequests by explicitly setting the type property for chrome.webRequest to xmlhttprequest, you will only be able to block asynchronous requests.

Synchronous XMLHttpRequests made from your extension are hidden from blocking event handlers in order to prevent deadlocks. Also the webRequest API will only expose calls that are made to URi patterns defined in the permissions area of your manifest.

like image 34
QFDev Avatar answered Oct 25 '22 19:10

QFDev