Modifying response headers in onHeadersReceived event of webRequest to initiate a “Save as” window
Modifying response headers in onHeadersReceived event of webRequest to initiate a “Save as” window
I'm experimenting with web extensions and trying to modify the response headers to make the results of the XMLHttpRequest() downloadable by setting the Content-Disposition to attachment.
Right now, I'm just attempting to replace the response headers received with the three most basic for intitiating a download as described in this MDN Web Doc. And I think the code is consistent with this very brief example provided in the webRequest web doc, which is modifying the request header.
I have the webRequest and webRequestBLocking permissions and host permission for all_urls in the manifest.json file.
After this code completes, when I write out the response headers received by the GET request it remains unchanged. I'm sure this code runs because I send messages back to the content script from within at each stage; and the onHeadersReceived event completes before the onreadystatechange event of the GET request.
There are numerous questions posted stating that the modified headers can't be viewed in dev tools, but I'm writing out what is received by the GET request. This question states that onHeadersReceived uses proxy headers and you can't really change those, and to do so in onResponseStarted; but that didn't work either.
I want the response header to initiate a "Save as" window to download the data returned from the GET response to the client disk.
I must be missing something very basic. What am I doing wrong or not doing? Thank you.
browser.webRequest.onHeadersReceived.addListener(
listener,
{ urls : ["<all_urls>"] },
["blocking", "responseHeaders"]
);
function listener( details) {
details.responseHeaders = [ { 'Content-Type' : 'text/html; charset=utf-8' }, { 'Content-Disposition' : 'attachment; filename="fileName.html"' }, { 'Content-Length' : '22' } ];
return { responseHeaders : details.responseHeaders };
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Comments
Post a Comment