Supress The Save As Dialog In Chrome
Solution 1:
In the extended BNF notation of [RFC 822], the Content-Disposition header field is defined as follows:
disposition := "Content-Disposition"":"
disposition-type
*(";" disposition-parm)
disposition-type := "inline"
/ "attachment"
/ extension-token
; values are not case-sensitive
disposition-parm := filename-parm / parameter
filename-parm := "filename""=" value;
If ignoring disposition parameters it simply does the following.
"content-disposition","attachment; filename=fname.jpeg"
downloads jpeg file when ever it is served.
"content-disposition","inline; filename=fname.jpeg"
displays jpeg file rather downloading jpeg file when ever it is served.
This behavior depends on the browser and the file you are trying to serve.
For example, if you have a JPEG file an inline disposition-type
will open the Image within browser, whereas attachment will force it to download.
If you're using a .ZIP file, browsers won't be able to display it inline, so for inline and attachment disposition-type
, the file will be downloaded.
You have to use WebRequest API
, to modify your headers
Sample Code
chrome.webRequest.onBeforeSendHeaders.addListener(
function (details) {//Modify Headers
details.requestHeaders.push({
"name": "content-disposition",
"value": "inline; filename=`_some_filename.some_extension`"
});
return {//Update HeadersrequestHeaders: details.requestHeaders
};
}, {
urls: ["<all_urls>"]
}, ["blocking", "requestHeaders"]);//Block the requests
Make sure you declare
"permissions":["webRequest","webRequestBlocking"]
in your manifest file
References
EDIT 1
Add your URL for this code and check if it still throws a save as
dialog.
chrome.webRequest.onHeadersReceived.addListener(
function (details) {
var _content_to_append = {
"name": "content-disposition",
"value": "inline"
};
details.responseHeaders.push(_content_to_append);
return {
responseHeaders: details.responseHeaders
};
}, {
urls: ["<all_urls>"]
}, ["blocking", "responseHeaders"]);
Solution 2:
@Sudarshan gave the right direction.
But it's appeared on another site, that even Content-Disposition
isn't enough.
So my current working code is:
chrome.webRequest.onHeadersReceived.addListener(
function (details) {
for (var i in details.responseHeaders) {
if (details.responseHeaders[i].name == "Content-Disposition")
details.responseHeaders[i].value = "inline; filename=\"\"";
if (details.responseHeaders[i].name == "Content-Type")
details.responseHeaders[i].value = "image/jpeg";
};
return { responseHeaders: details.responseHeaders };
}, {
urls: [
"http://qwe.rty.net/*",
"http://*.qwerty.com/*",
]
}, ["blocking", "responseHeaders"]
);
Post a Comment for "Supress The Save As Dialog In Chrome"