code now works as intended

This commit is contained in:
eclipse 2025-09-04 17:36:04 +02:00
parent fbc13d6b55
commit e330d7b02c
2 changed files with 91 additions and 69 deletions

View File

@ -1,33 +1,97 @@
console.log("background.js starts now"); console.log("background.js started");
var baseUrl = "";
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 onSuccess(result) {
console.log(`background.js: onSuccess(): Promise resolved with result '${JSON.stringify(result)}'`);
}
function onFailure(error) {
console.log(`background.js: onFailure(): Promise rejected with error '${error}'`);
}
async function downloadFile(url, filename, saveAs=false) {
console.log(`background.js: downloadFile(): url is '${url}' and filename is '${filename}'`);
return browser.downloads.download({
url: new URL(url, baseUrl).toString(),
filename: filename,
saveAs: saveAs // raise a path chooser dialog if true
});
}
async function downloadAllFiles(urlsAndFilenames) {
console.log(`background.js: downloadAllFiles(): started with ${JSON.stringify(urlsAndFilenames)}`);
const currentDate = getCurrentDate();
for (const urlAndFilename of urlsAndFilenames) {
let prefixedFilename = currentDate + " " + urlAndFilename.filename;
await downloadFile(urlAndFilename.url, prefixedFilename, true).then(onSuccess, onFailure);
}
/*
for (let i = 0; i < urlsAndFilenames.length; i++) {
// prefix filename
let prefixedFilename = currentDate + " " + urlsAndFilenames[i].filename;
console.log(`background.js: downloadAllFiles(): i is ${i}`);
// if ( i == 0 ) {
await downloadFile(urlsAndFilenames[i].url, prefixedFilename, true).then(onSuccess, onFailure);
// } else {
// downloadFile(urlsAndFilenames[i].url, prefixedFilename).then(onSuccess, onFailure);
// }
}
*/
}
function handlePageactionClick(tab, onClickData) { function handlePageactionClick(tab, onClickData) {
console.log("background.js: executing content script"); console.log("background.js: handlePageactionClick(): executing content script");
let executing = browser.scripting.executeScript({
files: [ "/contentscript.js" ], // get base url
target: { browser.tabs.query(
tabId: tab.id, {
allFrames: false currentWindow: true,
active: true
} }
}); )
executing.then(onExecuted, onExecutionError); .then(
tabs => {
console.log(`background.js: handlePageactionClick(): tab query returned ${JSON.stringify(tabs)}`);
baseUrl = tabs[0].url;
}
)
// execute content script
browser.scripting.executeScript(
{
files: [ "/contentscript.js" ],
target:
{
tabId: tab.id,
allFrames: false
}
}
)
.then(results => {
console.log(`background.js: handlePageactionClick(): content script execution successful with results '${JSON.stringify(results)}'`);
results.forEach(r => downloadAllFiles(r.result));
})
.catch(error =>
console.error(`background.js: handlePageactionClick(): The following error occured while executing a content script: ${error}`)
)
} }
function onExecuted(result) { /*
console.log(`background.js: content script execution successful with result: ${JSON.stringify(result)}`);
}
function onExecutionError(error) {
console.error(`background.js: The following error occured while executing a content script: ${error}`);
}
let gettingAllPermissions = browser.permissions.getAll(); let gettingAllPermissions = browser.permissions.getAll();
gettingAllPermissions.then((allPermissions) => { gettingAllPermissions.then((allPermissions) => {
console.log(JSON.stringify(allPermissions)) console.log(JSON.stringify(allPermissions))
}); });
*/
// add listener for the extension button // add listener for the extension button
browser.pageAction.onClicked.addListener(handlePageactionClick); browser.pageAction.onClicked.addListener(handlePageactionClick);

View File

@ -1,25 +1,15 @@
console.log("contentscript.js started!"); console.log("contentscript.js started!");
function getCurrentDate() { function extractUrlsAndFilenames() {
let today = new Date(); console.log("contentscript.js: this is extractFileUrlsAndNames()");
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() {
console.log("contentscript.js: this is extractFileUrlsAndNames()");
// get the correct frame // get the correct frame
let frame = document.querySelector("iframe.js-mail-content"); let frame = document.querySelector("iframe.js-mail-content");
// get all divs containing attachments // get all divs containing attachments
let divs = frame.contentWindow.document.body.querySelectorAll("div.samoware-mail-message__attach__item[attachment-ref]"); let divs = frame.contentWindow.document.body.querySelectorAll("div.samoware-mail-message__attach__item[attachment-ref]");
console.log(`contentscript.js: found ${divs.length} div containing attachments`);
let urlsAndFilenames = []
// loop over divs and extract attachment information // loop over divs and extract attachment information
let urlsAndFilenames = []
divs.forEach(div => { divs.forEach(div => {
let url = div.getAttribute("attachment-ref"); let url = div.getAttribute("attachment-ref");
let filename = div.getAttribute("file-name"); let filename = div.getAttribute("file-name");
@ -31,41 +21,9 @@ console.log("contentscript.js: this is extractFileUrlsAndNames()");
} }
urlsAndFilenames.push({url: url, filename: filename}); urlsAndFilenames.push({url: url, filename: filename});
}) })
//console.log(`contentscript.js: urlsAndFilenames is ${JSON.stringify(urlsAndFilenames)}`);
return urlsAndFilenames; return urlsAndFilenames;
} }
function onStartedDownload(id) { const urlsAndFilenames = extractUrlsAndFilenames();
console.log(`contentscript.js: Started downloading: ${id}`); console.log(`contentscript.js: done; found ${JSON.stringify(urlsAndFilenames)}`);
} urlsAndFilenames;
function onFailedToStartDownload(error) {
console.log(`contentscript.js: Download failed: ${error}`);
}
// get today's date
let currentDate = getCurrentDate();
console.log(`contentscript.js: currentDate is ${currentDate}`);
let urlsAndFilenames = extractFileUrlsAndNames();
// loop over attachments
for (let i = 0; i < urlsAndFilenames.length; i++) {
// prefix filename
let prefixedFilename = currentDate + " - " + urlsAndFilenames[i].filename;
console.log(`contentscript.js: prefixedFilename is ${prefixedFilename}`);
// start download
let downloading = browser.downloads.download({
url: urlsAndFilenames[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
});
console.log(`contentscript.js: downloading is ${downloading}`);
// resolve Promise
downloading.then(onStartedDownload, onFailedToStartDownload);
}
console.log("contentscript.js: has finished");
"contentscript.js: this is the final line";