changed extension stuff to Manifest v3; coded the file data extraction
This commit is contained in:
parent
959d201d63
commit
fbc13d6b55
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
*.code-workspace
|
*.code-workspace
|
||||||
tmp.md
|
tmp.md
|
||||||
|
utils/
|
||||||
@ -1,19 +1,33 @@
|
|||||||
console.log("background.js started!");
|
console.log("background.js starts now");
|
||||||
|
|
||||||
function handlePageactionClick(tab, onClickData) {
|
function handlePageactionClick(tab, onClickData) {
|
||||||
console.log("background.js: executing content script");
|
console.log("background.js: executing content script");
|
||||||
let executing = browser.tabs.executeScript({file: "/contentscript.js"})
|
let executing = browser.scripting.executeScript({
|
||||||
|
files: [ "/contentscript.js" ],
|
||||||
|
target: {
|
||||||
|
tabId: tab.id,
|
||||||
|
allFrames: false
|
||||||
|
}
|
||||||
|
});
|
||||||
executing.then(onExecuted, onExecutionError);
|
executing.then(onExecuted, onExecutionError);
|
||||||
}
|
}
|
||||||
|
|
||||||
function onExecuted(result) {
|
function onExecuted(result) {
|
||||||
console.log(`background.js: content script execution successful with result: ${result}`);
|
console.log(`background.js: content script execution successful with result: ${JSON.stringify(result)}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function onExecutionError(error) {
|
function onExecutionError(error) {
|
||||||
console.error(`background.js: The following error occured while executing a content script: ${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
|
// add listener for the extension button
|
||||||
browser.pageAction.onClicked.addListener(handlePageactionClick);
|
browser.pageAction.onClicked.addListener(handlePageactionClick);
|
||||||
|
|
||||||
|
|||||||
@ -9,34 +9,63 @@ function getCurrentDate() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function extractFileUrlsAndNames() {
|
function extractFileUrlsAndNames() {
|
||||||
return [];
|
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
|
||||||
|
divs.forEach(div => {
|
||||||
|
let url = div.getAttribute("attachment-ref");
|
||||||
|
let filename = div.getAttribute("file-name");
|
||||||
|
|
||||||
|
// if filename is null, derive it from url
|
||||||
|
if ( ! filename ) {
|
||||||
|
let splitUrl = url.split("/");
|
||||||
|
filename = splitUrl[splitUrl.length-1];
|
||||||
|
}
|
||||||
|
urlsAndFilenames.push({url: url, filename: filename});
|
||||||
|
})
|
||||||
|
|
||||||
|
//console.log(`contentscript.js: urlsAndFilenames is ${JSON.stringify(urlsAndFilenames)}`);
|
||||||
|
return urlsAndFilenames;
|
||||||
}
|
}
|
||||||
|
|
||||||
function onStartedDownload(id) {
|
function onStartedDownload(id) {
|
||||||
console.log(`Started downloading: ${id}`);
|
console.log(`contentscript.js: Started downloading: ${id}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
function onFailedToStartDownload(error) {
|
function onFailedToStartDownload(error) {
|
||||||
console.log(`Download failed: ${error}`);
|
console.log(`contentscript.js: Download failed: ${error}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get today's date
|
||||||
let currentDate = getCurrentDate();
|
let currentDate = getCurrentDate();
|
||||||
console.log(`contentscript.js: currentDate is ${currentDate}`);
|
console.log(`contentscript.js: currentDate is ${currentDate}`);
|
||||||
|
|
||||||
let files = extractFileUrlsAndNames();
|
let urlsAndFilenames = extractFileUrlsAndNames();
|
||||||
console.log(`contentscript.js: files is ${files}`);
|
|
||||||
|
|
||||||
for (let i = 0; i < files.length; i++) {
|
// loop over attachments
|
||||||
// prefix files[i].filename with currentDate
|
for (let i = 0; i < urlsAndFilenames.length; i++) {
|
||||||
let prefixedFilename = currentDate + " " + files[i].filename;
|
// prefix filename
|
||||||
|
let prefixedFilename = currentDate + " - " + urlsAndFilenames[i].filename;
|
||||||
|
console.log(`contentscript.js: prefixedFilename is ${prefixedFilename}`);
|
||||||
|
|
||||||
|
// start download
|
||||||
let downloading = browser.downloads.download({
|
let downloading = browser.downloads.download({
|
||||||
url: files[i].url,
|
url: urlsAndFilenames[i].url,
|
||||||
filename: prefixedFilename,
|
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
|
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);
|
downloading.then(onStartedDownload, onFailedToStartDownload);
|
||||||
}
|
}
|
||||||
|
|
||||||
"contentscript.js has finished";
|
console.log("contentscript.js: has finished");
|
||||||
|
"contentscript.js: this is the final line";
|
||||||
Loading…
Reference in New Issue
Block a user