Want to build a chrome extension that can intercept network request and rewrite the response body

43 views
Skip to first unread message

Long

unread,
Jun 28, 2024, 5:41:31 AM (2 days ago) Jun 28
to Chromium Extensions
Hey ya'll,

I am currently looking for a way to build a chrome extension that can rewrite the response of certain network calls (like how we have it in the network tab -> override content). Is there a chrome extension that is best suited for that?

Is it possible to intercept the request with chrome.declarativeNetworkRequest and then use the redirect api to a server on a background script to return json?

i.e Browser calls get fakeSite/items -> chrome extension intercepts it and redirects the response to myBackgroundScript -> myBackgroundSCript returns mockedItem payload.

I've been reading mixed replies on google if this is possible or not, want to double check before going down this route.

Long

unread,
Jun 28, 2024, 5:44:27 AM (2 days ago) Jun 28
to Chromium Extensions, Long
or in another words, how does requestly.io does it 

woxxom

unread,
Jun 28, 2024, 10:18:03 AM (2 days ago) Jun 28
to Chromium Extensions, Long
Only Firefox implements this functionality properly.
In Chrome there are limited workarounds:
  1. The declarativeNetworkRequest API can redirect to data:text/html,foo
  2. A script in the MAIN world can spoof XMLHttpRequest.prototype, Response.prototype, window.fetch. It won't work for requests made inside a worker or service worker of a site or when the site intentionally exploits https://crbug.com/40202434, preventing which requires spoofing dozens of methods, which might cause problems by itself.
  3. The chrome.debugger API can be used to implement what devtools does: send Fetch.enable, listen to Fetch.requestPaused event, send Fetch.takeResponseBodyAsStream, see https://chromedevtools.github.io/devtools-protocol/tot/Fetch/ and inspect example extensions. Using this API displays a warning above all tabs that annoyingly flickers on every navigation in the tab.
Reply all
Reply to author
Forward
0 new messages