Skip to content

Instantly share code, notes, and snippets.

@yenson-lau
Last active May 26, 2023 03:08
Show Gist options
  • Save yenson-lau/3903603c2e534237b94e348d2c7c92d1 to your computer and use it in GitHub Desktop.
Save yenson-lau/3903603c2e534237b94e348d2c7c92d1 to your computer and use it in GitHub Desktop.
Send all mails in a ProtonMail search query to any folder of your choosing
function waitForElm(selector) {
return new Promise(resolve => {
if (document.querySelector(selector)) {
return resolve(document.querySelector(selector));
}
const observer = new MutationObserver(mutations => {
if ((document.querySelector(selector) !== undefined) &&
(document.querySelector(selector) !== null)) {
resolve(document.querySelector(selector));
observer.disconnect();
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
});
}
const folderPage = async function(folder) {
(await waitForElm(".item-container:not(.item-is-loading)")); // wait for messages to show up
(await waitForElm('#idSelectAll')).click();
(await waitForElm('[data-testid="toolbar:moveto"]')).click();
(await waitForElm('[data-testid="folder-dropdown:folder-'+folder+'"]')).click();
}
const folderEmails = async function (folder) {
await folderPage(folder);
var nextpage = await waitForElm('[data-testid="pagination-row:go-to-next-page"]');
while (!nextpage.disabled) {
nextpage.click();
await folderPage(folder);
nextpage = await waitForElm('[data-testid="pagination-row:go-to-next-page"]')
}
};
await folderEmails("Trash");
@yenson-lau
Copy link
Author

yenson-lau commented Aug 24, 2022

To use the script do the following:

  1. Enter desired search query or open folder in ProtonMail.
  2. Open Developer Tools in your browser (F12).
  3. Navigate to the Console tab.
  4. Paste the code below, and choose the folder you want to, e.g. "Trash" or "Inbox".
    • Inspect the "Move to" menu for a field of data-test-id="folder-dropdown:folder-<foldername>" to get the <foldername>
  5. Hit Enter and wait for the emails to be moved to the folder.
  6. Reload the page to make sure the script doesn't continue running!

To delete everything in Trash: go to Trash and click Delete All. (It is hidden under More Options on the control panel.)

Update 2022-08-06: Made the code robust to loading times and can move to any folder.

@yeled
Copy link

yeled commented Aug 25, 2022

This is great. Can you figure out how to delete all my labels, too? :)

@yenson-lau
Copy link
Author

yenson-lau commented Aug 26, 2022

This is great. Can you figure out how to delete all my labels, too? :)

How many labels do you have? I don't have that many so I would probably just do it by hand.
But the key to this script is using waitForElm. If you want to delete all your labels you can just go the folders folder, keep calling waitForElm to get the next label, and delete it until all your labels are gone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment