1

I according to the chrome.webRequest I want to change data-urls responseheaders.But I can't capture data request in chrome.webRequest.onHeadersReceived.

Am I wrong ?

chrome.webRequest.onBeforeRequest.addListener(
  function (details) {
    const url = details.url

    if(url == 'http://www.example.com/api/getUsers') {
      return {
        redirectUrl: 'data:application/json; charset=utf-8,' + 
JSON.stringify({"a":1, "b": 2})
      }
    }

    return {cancel: false}

  },
  {urls: ["<all_urls>"]},
  ["blocking"]
)

chrome.webRequest.onHeadersReceived.addListener(
  function (details) {
    console.log(details)  // can't capture data-urls

    return {responseHeaders:details.responseHeaders};
  },
  {urls: ["<all_urls>"]},
  ["responseHeaders","blocking"]
)
riskers
  • 178
  • 13

1 Answers1

1

You can't capture data: URLs with webRequest API, since data: is not a supported scheme for host permissions.

As such, even with <all_urls> permission/filter you won't get events for data:

Documentation quote:

The webRequest API only exposes requests that the extension has permission to see, given its host permissions. Moreover, only the following schemes are accessible: http://, https://, ftp://, file://, ws:// (since Chrome 58), wss:// (since Chrome 58), or chrome-extension://.

I'm pretty sure there's already a feature request for this, but I can't currently find it.

Xan
  • 74,770
  • 16
  • 179
  • 206
  • so it's impossible to capture? – riskers Aug 18 '17 at 12:23
  • No. I guess it's not that easy for them to implement the feature: judging by https://crbug.com/130065#c3 data urls are handled in a different code path. – wOxxOm Aug 18 '17 at 12:24
  • @riskers Added documentation quote with the help of wOxxOm's link; yes, it's currently impossible. – Xan Aug 18 '17 at 12:26