9

I'm dealing with some mp3 link on the internet.

When using Chrome developer tool, I see some have response header with Content-Type:application/octet-stream (links like these force Chrome to download), some links have reponse header with Content-Type:audio/mpeg (links like these allow Chrome to play them streamingly).

Are there any Chrome extensions that allow changing response header? Because I want to change Content-Type

onmyway133
  • 45,645
  • 31
  • 257
  • 263

1 Answers1

19

See the Chrome developer page.

Here's a simple example that modifies Content-Type of https://www.google.com/ to text/plain.

chrome.webRequest.onHeadersReceived.addListener(details => {
    let header = details.responseHeaders.find(e => e.name.toLowerCase() === 'content-type') ;
    header.value = 'text/plain';
    return {responseHeaders: details.responseHeaders};
}, {urls: ['https://www.google.com/']}, ['blocking', 'responseHeaders']);

Note that you have to declare both webRequest and webRequestBlocking permissions in manifest.json.

Ivan Castellanos
  • 8,041
  • 1
  • 47
  • 42
方 觉
  • 4,042
  • 1
  • 24
  • 28
  • @方 觉 where should I place this code? Do I need to write a new extension? – onmyway133 Dec 04 '12 at 10:51
  • @Yamamoto Place the code in your background script. If you're using an event page instead of the background page, you should use chrome.declarativeWebRequest (which will be supported in the next Chrome stable release) instead. – 方 觉 Dec 08 '12 at 02:57
  • 3
    I've tried to do the same in background page. Strangely, according to logs the event handler is executed, and headers are changed, but this has no effect on page being loaded - its http headers remain unchanged (according to developer view). There is no errors. Any ideas? – Stan Dec 08 '12 at 18:49
  • @Stan, I found that the example in this answer worked fine for me. However, changing the headers from `application/octet-stream` to something else failed to prevent Chrome from trying to save the file. Perhaps @entropy didn't test it. – Sam Apr 10 '13 at 11:39
  • 1
    @Stan, the solution to this in my case was to also look for and remove any Content-Disposition headers. However, I recommend changing the header's `attachment` value to `inline` so the file name information is not lost if present. – Sam Apr 10 '13 at 12:15
  • @Sam, thanks for the feedback. It does realy work under some circumstances and does not under the others, which meet my use case, so I'll need to find a workaround. – Stan Apr 10 '13 at 18:52