D
D
demon18062019-01-29 12:50:01
JavaScript
demon1806, 2019-01-29 12:50:01

Intercepting requests with "JS" in "Chrome"?

Good afternoon. Can anyone tell me how to intercept requests using JS in "Chrome"?
For example, so that for any request to some or all sites, the string "this is a response constructed by VPN extension" is returned to me.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Somewhere Intech, 2019-01-31
@john36allTa

If I understand correctly that you have your own extension, then:
Edit the manifest by adding "webRequest" to the permissions

"permissions": [
          "webRequest"
          // other permissions

Before sending a request, you can cancel it:
chrome.webRequest.onBeforeRequest.addListener(
        function(details) {
          if (details.url.indexOf("://white.listed.site")) console.log("this is a response constructed by VPN extension");
          return {cancel: details.url.indexOf("://www.evil.com/") != -1}; // если нужно отменить запрос
        },
        {urls: ["<all_urls>"]},
        ["blocking"]);

You can also hang onComplete
chrome.webRequest.onCompleted.addListener(function(details){
    console.log(`Catch ${details.method} response from ${details.url} ${details.fromCache ? ' [ from cache ]' : ''} `)
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question