Suddenly, you need to download a batch of files using chrome extensions, but the file name must be the ID name in the URL.
Want to modify the downloaded file name? This demand is amazing, but as a developer, we must find a way to fulfill this need.
Step 1: Check the chrome plug-in development process. A manifest file specifies an HTML file to complete the production of Hello world.
Generally, the plug-in puts the script on the host page and interacts with the script in the plug-in through the host script, in this way, DOM nodes and related information on the host page can be transmitted to the plug-in script through messages.
The demand side gave me a plug-in for downloading the master. Its functions are complete, but the downloaded file name is the file with the content-Disposition specified by the server.
I tried four methods to modify the file name.
1. if the downloaded file is a static file, slice, HTML, CSS, and other resource files on the webpage, you can generate a tag and assign href to the resource URL, at the same time, assign a value to the downloaded file name for the download attribute of tag a, and use a mouse to simulate the clicking method to automatically download the above resources, the following script is placed in the host script, triggered by message transmission.
function downloadResource(url){var link = document.createElement('a');a.href= url;a.download = "xxxxx.xxxx";var event = document.createEvent("MouseEvents"); event.initMouseEvent("click", true, false); link.dispatchEvent(event);}
2. use Chrome plug-in. downloads. download method, this method is to use the chrome development version, so immediately install the development version, and open the experiment extension interface in the flag, the above method is used to see that there is a filename item in the API description for modification. It turns out that this filename item has the same effect as the first method, it does not work for files whose content-Disposition Header attribute is specified on the server. The method that does not work is useless.
chrome.downloads.download({url: torrentsLinks[i],filename:'1.torrent'},function(id) {});
3. through the above attempts, I already know that the content-Disposition specified by the server makes me unable to change the file name, and I want to find a way to change this attribute in responseheader, finally, the chrome plug-in API found a way to modify chrome. webrequest. onheadersreceived. This object can be triggered by receiving the header of the server. The breakthrough has been found. How can I write this method? First, let's look at the source code.
chrome.webRequest.onHeadersReceived.addListener(function(details){var headers = details.responseHeaders,blockingResponse = {};for( var i = 0, l = headers.length; i < l; ++i ) { if( (headers[i].name.toLowerCase() == 'content-disposition') ) {headers[i].value = 'attachment; filename=\"xxxx.xxxx\"';break; }}blockingResponse.responseHeaders = headers;return {responseHeaders: headers}},{urls: ["http://jpopsuki.eu/*","https://what.cd/*"]},["responseHeaders","blocking"]);
Addlistener has three parameters: the first is the callback function, and the second is the filtered URLs. Only these URLs are listened to. The third parameter must have a blocking string, if this string does not exist, you only need to call the callback function. The responseheader has been downloaded after the callback function returns, while the third parameter responseheaders only listens to responseheaders, this completes the modification of a download file name.
In the above development process, find a chromehidden. internalapis. downloads. ondeterminingfilename supports file name modification in API description. However, an error is thrown during the actual call and the internalapis object cannot be found. A bug is recorded on chrome, I don't know if it will attract Chrome's attention.