0

With a chrome extension you can block a request as follows

chrome.webRequest.onBeforeRequest.addListener(
    function(details) {
        return { cancel: true }; 
    }, {
        urls: ["*://localhost/*"]
    }, [
        "blocking"
    ]
);

In my case I want to respond to the request within the chrome extension itself without going to the backend. I actually want 2 to do 2 things

1) respond with some data within my extension
2) respond with a 4xx or 5xx

So, in my code example above, canceling doesn't seem to do it, the request is cancelled and nothing is possible anymore. So my question is, how can I respond within my extension with whatever statusCode I want?

Jeanluca Scaljeri
  • 26,343
  • 56
  • 205
  • 333
  • Chrome extension can do it via `chrome.debugger` API. You'll use [CDP](https://chromedevtools.github.io/devtools-protocol/tot/) events/methods to intercept requests and provide custom response. Look for examples for various automation tools like puppeteer and then convert them to use chrome.debugger. – wOxxOm Jan 23 '21 at 18:32
  • Looks very complicated, no examples. Do you know if there is a site with examples using chrome.debugger or anything else that can get me started? – Jeanluca Scaljeri Jan 23 '21 at 20:09
  • There are many examples of other things done via chrome.debugger: [Chrome Extension - How to get HTTP Response Body?](https://stackoverflow.com/a/29845219) – wOxxOm Jan 24 '21 at 05:11

0 Answers0