11

I am working on a Chrome extension. I would like to modify some contents when the (very first) page load happens. I wonder if I use the method getContent() in chrome.devtools.network.onRequestFinished.addListener, how can I modify the content after it is returned from the method?

Is this method only for network/traffic observation? Can I modify the response body?

Stephan
  • 16,509
  • 7
  • 35
  • 61
pjk
  • 149
  • 1
  • 1
  • 7
  • 1
    I'm not fully understanding your question. You would like to modify things on a webpage? Or on plain data you're getting from an HTTP request? – John Woodruff Jul 02 '13 at 00:43
  • ever figure this out? – K2xL Jan 19 '14 at 18:02
  • This might help - https://stackoverflow.com/a/71250307/816213. Here's [the demo video](https://www.youtube.com/watch?v=cqJcR-RcNXg) to modify the response. – Sachin Jain Feb 24 '22 at 10:15

2 Answers2

2

This has never been possible.

See this bug in the Chromium project (from Nov. 2011):

There are a couple relevant discussions on how to work around this limitation for certain use cases:

Based on some of the discussion in the Chromium bug DB, I'd guess that modifying the content of the existing request may never be allowed for security reasons, but reading the response and redirecting might be allowed (someday when all the blocking issues are resolved).

Community
  • 1
  • 1
lobacten
  • 21
  • 2
-2

Yes, you just have to use the webRequest API, you get full control of all requests made and can modify before and after, here is the full documentation:

Chrome Extensions WebRequest API

(One caveat, you user must grant permission when installing the application)

Hope this helps, if you are looking for more particular info just reply and I can help, but the documentation is pretty good at telling you how to set up for proper event handling :)

joseeight
  • 904
  • 7
  • 10
  • It's not true, you can modify lots of thins, but the response body. – Adi Darachi Jan 10 '16 at 16:33
  • My response is 3 years old and was a note for the then experimental API, obviously Chrome has made some progress and changes since then. Thanks for everyone voting down this because the API has been changed since I posted this answer. Kudos. – joseeight Jan 14 '16 at 18:00
  • 1
    Also, since people are still wondering about this, you can use Service Workers now to modify requests: http://www.html5rocks.com/en/tutorials/service-worker/introduction/ – But this is a bit more limiting than what the WebRequest API used to allow years ago. – joseeight Jan 14 '16 at 18:08