5

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)
1337holiday
  • 1,924
  • 1
  • 24
  • 42

2 Answers2

2

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.

QFDev
  • 8,668
  • 14
  • 58
  • 85
  • Right, I already am blocking requests from mail.google.com, specifically XMLHttpRequests as you stated. However is it possibly to modify the responseBody of these requests with webrequest? I have tried and it does not seem to be something that is supported. – 1337holiday Jun 01 '13 at 21:05
  • It is my understanding that you can only modify the responseHeaders object using the blocking method. There is a pending feature request for exactly the sort of control you want https://code.google.com/p/chromium/issues/detail?id=104058 – QFDev Jun 03 '13 at 10:50
  • The key part is "Synchronous XMLHttpRequests **made from your extension**", so you can process synchronous XHRs from other origins. And the question makes it clear that it's page's own requests. – Xan Nov 29 '14 at 22:23
2

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.

Community
  • 1
  • 1
rsanchez
  • 14,467
  • 1
  • 35
  • 46
  • I had a feeling it wasn't possible, really hope they implement it soon but thanks for the great links, should be an interesting read! – 1337holiday Jun 02 '13 at 17:45
  • @rsanchez, But the question asker only wants to block isn't it? which is doable no? – Pacerier Jun 15 '17 at 05:35