Suppose HTML files coming from a server have these headers:
Content-Type: application/octet-stream
Content-Disposition: attachment;
I would like to alter the headers in a web extension (so that the HTML file is displayed as a regular web page):
Content-Type: text/html; charset=utf-8
(Removed Content-Disposition)
In Google Chrome, the web extension successfully changes the headers and the HTML file is displayed as a normal web page. In Firefox (48), although the headers are changed, I am prompted to download the HTML file. Am I doing something wrong or is this a known bug?
Example of request: localhost:8000/test.html
Code below.
Web extension - manifest.json:
{
"description": "Change content type",
"manifest_version": 2,
"name": "change-content-type",
"version": "1.0",
"permissions": [
"webRequest", "webRequestBlocking", "http://localhost/*"
],
"background": {
"scripts": ["background.js"]
}
}
Web extension - Background.js looks as follows:
function setHeader(headers, name, value) {
for (var header of headers) {
if (header.name.toLowerCase() == name.toLowerCase()) {
header.value = value;
return;
}
}
headers.push({ name : name, value : value });
}
function removeHeader(headers, name) {
for (var i = 0; i < headers.length; i++) {
if (headers[i].name.toLowerCase() == name.toLowerCase()) {
headers.splice(i, 1);
return;
}
}
}
function changeResponseHeaders(details) {
removeHeader(details.responseHeaders, "Content-Disposition");
setHeader(details.responseHeaders, "Content-Type", "text/html; charset=utf-8");
return {
responseHeaders: details.responseHeaders
};
}
chrome.webRequest.onHeadersReceived.addListener(
changeResponseHeaders, {
urls: ["http://localhost/*"],
types: ["main_frame", "sub_frame"]
}, ["blocking", "responseHeaders"]
)
A tiny web server (as an example):
import SimpleHTTPServer
class MyHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def guess_type(self, path):
return 'application/octet-stream';
def end_headers(self):
self.send_my_headers()
SimpleHTTPServer.SimpleHTTPRequestHandler.end_headers(self)
def send_my_headers(self):
self.send_header("Content-Disposition", "attachment;")
if __name__ == '__main__':
SimpleHTTPServer.test(HandlerClass=MyHTTPRequestHandler)
And a sample HTML file:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Test</title>
</head>
<body>
<div>Test</div>
</body>
</html>
Firefox will add them in this situation: right-click the download on the Downloads list and choose "Always Open Similar Files". They then should appear in the box with "Use [relevant application" as the action. You can change that to Always Ask if you prefer.
This was a bug and it has been fixed. The fix seems to be planned for Mozilla Firefox 52.
I do not have knowledge on the implementation, thus I cannot provide further details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With