47 lines
1.1 KiB
JavaScript
47 lines
1.1 KiB
JavaScript
|
|
|
|
|
|
function getCurrentDate() {
|
|
let today = new Date();
|
|
let yyyy = today.getFullYear();
|
|
let mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
|
|
let dd = String(today.getDate()).padStart(2, '0');
|
|
return yyyy + "-" + mm + "-" + dd;
|
|
}
|
|
|
|
function extractFileUrlsAndNames() {
|
|
return [];
|
|
}
|
|
|
|
function onStartedDownload(id) {
|
|
console.log(`Started downloading: ${id}`);
|
|
}
|
|
|
|
function onFailed(error) {
|
|
console.log(`Download failed: ${error}`);
|
|
}
|
|
|
|
|
|
|
|
function handleButtonClick(event) {
|
|
let currentDate = getCurrentDate();
|
|
|
|
let files = extractFileUrlsAndNames();
|
|
|
|
for (let i = 0; i < files.length; i++) {
|
|
// prefix files[i].filename with currentDate
|
|
let prefixedFilename = currentDate + " " + files[i].filename;
|
|
|
|
let dl = browser.downloads.download({
|
|
url: files[i].url,
|
|
filename: prefixedFilename,
|
|
saveAs: (i == 0) // raise a path chooser dialog for the first file only; all later files will (hopefully!) be saved to the same folder
|
|
}).then(onStartedDownload, onFailed);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
// add listener for the extension button
|
|
browser.browserAction.onClicked.addListener(handleButtonClick); |