From 89f4b2f9a63765b3bc94efda4e9a5a879cf1fd20 Mon Sep 17 00:00:00 2001 From: eclipse Date: Mon, 3 Nov 2025 17:45:17 +0100 Subject: [PATCH] switched from executeScript() to message-based communication between scripts so the content script can be executed repeatedly --- background.js | 63 +++++++++++++++++++----------------------------- contentscript.js | 37 ++++++++++++++-------------- manifest.json | 9 ++++++- 3 files changed, 52 insertions(+), 57 deletions(-) diff --git a/background.js b/background.js index 1396325..16308eb 100644 --- a/background.js +++ b/background.js @@ -18,6 +18,7 @@ function onFailure(error) { console.log(`background.js: onFailure(): Promise rejected with error '${error}'`); } +// actually download an attached file async function downloadFile(url, filename, saveAs=false) { console.log(`background.js: downloadFile(): url is '${url}' and filename is '${filename}'`); return browser.downloads.download({ @@ -27,57 +28,43 @@ async function downloadFile(url, filename, saveAs=false) { }); } +// sequentially initiate download for each attached file async function downloadAllFiles(urlsAndFilenames) { - console.log(`background.js: downloadAllFiles(): started with ${JSON.stringify(urlsAndFilenames)}`); const currentDate = getCurrentDate(); - 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, true).then(onSuccess, onFailure); - } + await downloadFile(urlsAndFilenames[i].url, prefixedFilename, true).then(onSuccess, onFailure); } } -function handlePageactionClick(tab, onClickData) { - console.log("background.js: handlePageactionClick(): executing content script"); +// page action handler +function handlePageactionClick(tab) { + console.log("background.js: handlePageactionClick(): sending message"); // get base url - browser.tabs.query( - { - currentWindow: true, - active: true - } - ) - .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)); + browser.tabs.query({ + currentWindow: true, + active: true }) - .catch(error => - console.error(`background.js: handlePageactionClick(): The following error occured while executing a content script: ${error}`) + .then(tabs => { + console.log(`background.js: handlePageactionClick(): tab query returned ${JSON.stringify(tabs)}`); + baseUrl = tabs[0].url; + }) + + // send message to content script which will trigger said content script + browser.tabs.sendMessage( + tab.id, + "pageAction was clicked" + ) + .then(message => { + console.log(`background.js: received a response from contentscript.js with content ${JSON.stringify(message.urlsAndFilenames)}`); + downloadAllFiles(message.urlsAndFilenames); + }) + .catch(error => + console.error(`background.js: handlePageactionClick(): The following error occured while handling a page action: ${error}`) ) } diff --git a/contentscript.js b/contentscript.js index 894f79b..dc5567c 100644 --- a/contentscript.js +++ b/contentscript.js @@ -1,14 +1,27 @@ console.log("contentscript.js started!"); +// +function handleReceivedMessage(message) { + // log received message + console.log(`contentscript.js received a message with content ${message}`); + + // execute script regardless of message content + const urlsAndFilenames = extractUrlsAndFilenames(); + clickPrintButton(); + console.log(`contentscript.js: URLs and filenames extracted -> ${JSON.stringify(urlsAndFilenames)}`); + + // return data as the resolved Promise's payload + return Promise.resolve({ urlsAndFilenames: urlsAndFilenames }); +}; + +// extract URL and filename for each mail attachment 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]" - ); + let divs = frame.contentWindow.document.body.querySelectorAll("div.samoware-mail-message__attach__item[attachment-ref]"); // loop over divs and extract attachment information let urlsAndFilenames = []; @@ -26,22 +39,10 @@ function extractUrlsAndFilenames() { return urlsAndFilenames; } +// trigger a click event on samoware's print button function clickPrintButton() { - let printElement = document.querySelector( - 'li[ng-click="printMessage(selectedMessage)"]' - ); + let printElement = document.querySelector('li[ng-click="printMessage(selectedMessage)"]'); printElement.click(); } -const urlsAndFilenames = extractUrlsAndFilenames(); -console.log( - `contentscript.js: URLs and filenames extracted -> ${JSON.stringify( - urlsAndFilenames - )}` -); - -clickPrintButton(); -console.log(`contentscript.js: clicked the print button`); - -// this last statement is important – it's evaluation value is the return value for the background script -urlsAndFilenames; +browser.runtime.onMessage.addListener(handleReceivedMessage); \ No newline at end of file diff --git a/manifest.json b/manifest.json index 5d040fa..598cbae 100644 --- a/manifest.json +++ b/manifest.json @@ -3,7 +3,7 @@ "name": "samoware-multisave", "version": "1.0", - "description": "When using the groupware CommuniGate with the web application Samoware, this browser extension saves all attachments from the currently opened email to a directory of the user's choosing, prefixing each entry's filename with the current date. Yep, it's quite specific.", + "description": "When using the groupware CommuniGate in combination with the web application Samoware, this browser extension saves all attachments from the currently opened email to a directory of the user's choosing, prefixing each entry's filename with the current date, while also raising a print dialog for the mail body. Yep, it's quite specific.", "icons": { "48": "icons/samovar-48.png", @@ -34,5 +34,12 @@ "scripts": [ "background.js" ] + }, + + "content_scripts": [ + { + "matches": ["https://communigate.aip.de/*"], + "js": ["contentscript.js"] } +] } \ No newline at end of file