Skip to content

Instantly share code, notes, and snippets.

@lyndsysimon
Forked from apazzy/protonmail_archiver.js
Last active October 25, 2023 21:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lyndsysimon/b1a0b782e731abb53724a0b817648a2b to your computer and use it in GitHub Desktop.
Save lyndsysimon/b1a0b782e731abb53724a0b817648a2b to your computer and use it in GitHub Desktop.
Protonmail inbox cleaner
/* Protonmail cleanup scripts
* Works with ProtonMail v4.0.14 as of 2022-01-23
*
* Based on Lyndsy's Protonmail mass archiver:
* https://gist.github.com/lyndsysimon/e6a4a1becaddf09fe02c503793b545d6
*
* Usage:
* - Navigate to your inbox (or another folder/label, presumably)
* - Open your browser's developer tools' Javascript console
* - Paste in this script
* - Paste one of the below commented 'setInterval' commands
* based on need to start the script
* - Wait until the script finishes, it will run forever until
* the page is closed/refreshed
*
*
* setInterval(removeLabelMarkReadAndArchive, 4400);
* Remove the label selected in the navigation pane from all emails,
* mark the emails as read, and archive them.
*
* setInterval(markAsReadAndArchive, 2500);
* Mark all emails in a folder/label as read and then archive them.
*
* setInterval(markAllAsRead, 2500);
* Mark all Unread emails as read
*
*/
let viewAll = function() { document.querySelector('[data-testid="filter-dropdown:show-all"]').click() };
let viewUnread = function() { document.querySelector('[data-testid="filter-dropdown:show-unread"]').click() };
let viewRead = function() { document.querySelector('[data-testid="filter-dropdown:show-read"]').click() };
let markAsRead = function() {
let button = document.querySelector('[data-testid="toolbar:read"]')
// button is only present if there are unread messages in the current selection
if ( button ) { button.click() }
};
// Not currently used, but opens the dropdown to the right of the "select all checkbox"
let toggleDropdown = function() { document.querySelector('[data-testid="toolbar:select-all-dropdown"]').click() };
let selectAll = function() { document.querySelector('[data-testid="toolbar:select-all-checkbox"]').click() };
let buttonArchive = function() { document.querySelector('[data-testid="toolbar:movetoarchive"]').click() };
let viewLabelsMenu = function() {document.querySelector('button[aria-describedby="tooltip-19"]').click() };
let labelMenuArchive = function() { document.querySelector('input[data-testid="label-dropdown:also-archive"]').click() };
let applyLabel = function() { document.querySelector("#dropdown-18 > div > form > div.m1:not(div.mb0) > button").click() };
let removeActiveLabel = function() {
var activeLabel = document.querySelector(".navigation-item > .active").title
var labelCheckbox = `input[data-testid="label-dropdown:label-checkbox-${activeLabel}"]`
if(document.querySelector(labelCheckbox).checked){ // error check in case lable checkbox is not checked.
document.querySelector(labelCheckbox).click()
}
};
// Only use the below function if removing multiple labels, otherwise use removeActiveLabel
// Replace "_LABEL_" with full label name, copy for as many labels as neede
/*
let removeLabels_LABEL_ = function() {
if(document.querySelector('input[data-testid="label-dropdown:label-checkbox-_LABEL_"]').checked) {
document.querySelector('input[data-testid="label-dropdown:label-checkbox-_LABEL_:checked').click()
} else {
// If not checked (from filtering in a different view than the active label) check and uncheck to force removal
document.querySelector('input[data-testid="label-dropdown:label-checkbox-_LABEL_"]:checked').click()
document.querySelector('input[data-testid="label-dropdown:label-checkbox-_LABEL_"]:checked').click()
}
};
*/
function removeLabelMarkReadAndArchive() {
setTimeout(selectAll,400);
setTimeout(markAsRead,800);
setTimeout(viewLabelsMenu,900);
setTimeout(removeActiveLabel, 1700);
setTimeout(labelMenuArchive,1950);
setTimeout(applyLabel, 2400);
}
function markAsReadAndArchive() {
setTimeout(selectAll,400);
setTimeout(markAsRead, 1000);
setTimeout(buttonArchive, 1200);
}
function markAllAsRead() {
setTimeout(viewUnread,400);
setTimeout(selectAll,1000);
setTimeout(markAsRead, 1200);
setTimeout(viewRead,1700);
setTimeout(viewUnread,1900);
}
@lyndsysimon
Copy link
Author

Forked from revisions made by @apazzy here.

I've fixed the broken references used by the markAsReadAndArchive function, and it worked as of today against my inbox, handling >125 pages of messages.

@lyndsysimon
Copy link
Author

I ran into issues with the $ being unavailable on ProtonMail after the first call in the setInterval call. I'm not sure if that's because I'm using Arc or due to a change in the underlying JS context in the ProtonMail inbox.

Either way, I've replaced calls to $ with document.querySelector, which solves the problem. Previous version are available as revisions to this gist.

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