diff --git a/background.js b/background.js index 1c68273..e4a3615 100644 --- a/background.js +++ b/background.js @@ -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) { - console.log("background.js: executing content script"); - let executing = browser.scripting.executeScript({ - files: [ "/contentscript.js" ], - target: { - tabId: tab.id, - allFrames: false + console.log("background.js: handlePageactionClick(): executing content script"); + + // get base url + browser.tabs.query( + { + 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(); gettingAllPermissions.then((allPermissions) => { console.log(JSON.stringify(allPermissions)) }); - - +*/ // add listener for the extension button browser.pageAction.onClicked.addListener(handlePageactionClick); diff --git a/contentscript.js b/contentscript.js index 6eb3809..f9cf217 100644 --- a/contentscript.js +++ b/contentscript.js @@ -1,25 +1,15 @@ console.log("contentscript.js started!"); -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() { -console.log("contentscript.js: this is extractFileUrlsAndNames()"); +function extractUrlsAndFilenames() { + console.log("contentscript.js: this is extractFileUrlsAndNames()"); // get the correct frame let frame = document.querySelector("iframe.js-mail-content"); // get all divs containing attachments 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 + let urlsAndFilenames = [] divs.forEach(div => { let url = div.getAttribute("attachment-ref"); let filename = div.getAttribute("file-name"); @@ -31,41 +21,9 @@ console.log("contentscript.js: this is extractFileUrlsAndNames()"); } urlsAndFilenames.push({url: url, filename: filename}); }) - - //console.log(`contentscript.js: urlsAndFilenames is ${JSON.stringify(urlsAndFilenames)}`); return urlsAndFilenames; } -function onStartedDownload(id) { - console.log(`contentscript.js: Started downloading: ${id}`); -} - -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"; \ No newline at end of file +const urlsAndFilenames = extractUrlsAndFilenames(); +console.log(`contentscript.js: done; found ${JSON.stringify(urlsAndFilenames)}`); +urlsAndFilenames;