I am trying to use chrome.download methods to download a CSV file type and change the name of the file upon download.
Currently, I am using
document.querySelectorAll("[file$='foo']").forEach(a=>a.click());
to simulate a click on each element to download. However, each filename upon download is foo.csv, foo(1).csv etc. Utimately I would like to download the file, and change the filename. Currently I am doing this through a content script, and from what I have read I cannot access the chrome.download API and will have to use messaging from my background.js.
Other posts similar to this one, from my understanding, involve creating a new element/content for download versus downloading content already on the site.
Any help is greatly appreciated! Thank you!!
My background.js
function changeName(prevName){
chrome.downloads.onDeterminingFilename.addListener((prevName, suggest) => {
suggest({
filename: 'new-name.csv'
});
});
};
chrome.runtime.onMessage.addListener(function(msg, sender) {
if ((msg.action === 'renameSave')
&& (msg.params !== undefined)) {
changeName(msg.params);
};
});
and my contentscript.js
button.addEventListener('click', function() {
var elems = document.getElementsByClassName("Stmt");
var count = 1;
var interval = setInterval(function(){
elems[count].click();
chrome.runtime.sendMessage({
action: 'renameSave',
params: ["statement.csv"]
});
count = count + 2;
if(count > elems.length){
clearInterval(interval);
}
},800);
}, false);