Skip to content

Instantly share code, notes, and snippets.

@vict0rsch
Last active February 22, 2022 15:21
Show Gist options
  • Select an option

  • Save vict0rsch/365b3e2b1aee8b634579d4001aaf103c to your computer and use it in GitHub Desktop.

Select an option

Save vict0rsch/365b3e2b1aee8b634579d4001aaf103c to your computer and use it in GitHub Desktop.

Revisions

  1. vict0rsch revised this gist Feb 22, 2022. 1 changed file with 29 additions and 28 deletions.
    57 changes: 29 additions & 28 deletions protonmail-read.js
    Original file line number Diff line number Diff line change
    @@ -9,9 +9,9 @@
    * @returns HTML element
    */
    const getFromTestId = (testId) => {
    return Array.from(document.getElementsByTagName("button")).filter(
    (element) => element.getAttribute("data-testid") === testId
    )[0];
    return Array.from(document.getElementsByTagName("button")).filter(
    (element) => element.getAttribute("data-testid") === testId
    )[0];
    };

    /**
    @@ -38,42 +38,43 @@ const next = () => getFromTestId("pagination-row:go-to-next-page");
    /**
    * Perform 1 iteration of selectAll-markAsRead-goToNextPage.
    * @param {number} clickTime The time to wait between button clicks.
    * @param {number} interval The interval id to clear if there's no next page.
    * @param {string} action The action to perform on each page: "markAsRead" or "moveToTrash".
    */
    const actionOnAllAndNext =
    (clickTime, action = "markAsRead") =>
    () => {
    // select all emails
    all().click();
    setTimeout(() => {
    // choose element depending on the action
    const element = action === "markAsRead" ? markRead() : moveToTrash();
    // click element
    element.click();
    setTimeout(() => {
    // Find button to next page
    const nextElement = next();
    if (nextElement && !nextElement.disabled) {
    // Next page is available: click it
    nextElement.click();
    } else {
    // No next page: stop
    console.info("No more pages. Stopping.");
    clearInterval(interval);
    }
    }, clickTime);
    }, clickTime);
    };
    (clickTime, action = "markAsRead") =>
    () => {
    // select all emails
    all().click();
    setTimeout(() => {
    // choose element depending on the action
    const element = action === "markAsRead" ? markRead() : moveToTrash();
    // click element
    element.click();
    setTimeout(() => {
    // Find button to next page
    const nextElement = next();
    if (nextElement && !nextElement.disabled) {
    // Next page is available: click it
    nextElement.click();
    } else {
    // No next page: stop
    console.info("No more pages. Stopping.");
    clearInterval(interval);
    }
    }, clickTime);
    }, clickTime);
    };

    /**
    *
    * @param {number} clickTime The time to wait between button clicks.
    * @param {number} loadingTime The time to wait for a single page to load, i.e.
    * the time between calls to actionOnAllAndNext()
    * @param {string} action The action to perform on each page: "markAsRead" or "moveToTrash".
    * @returns interval id
    */
    const start = (clickTime, loadingTime, action = "markAsRead") => {
    interval = setInterval(actionOnAllAndNext(clickTime, action), loadingTime);
    interval = setInterval(actionOnAllAndNext(clickTime, action), loadingTime);
    };

    // -----------------------
  2. vict0rsch revised this gist Feb 22, 2022. 1 changed file with 46 additions and 17 deletions.
    63 changes: 46 additions & 17 deletions protonmail-read.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    // -----------------------
    // ----- Functions -----
    // -----------------------

    /**
    *
    * @param {string} testId The string id for the element to find,
    @@ -19,7 +23,12 @@ const all = () => document.getElementById("idSelectAll");
    * Get the element to mark all the emails on this page as "read".
    * @returns HTML element
    */
    const read = () => getFromTestId("toolbar:read");
    const markRead = () => getFromTestId("toolbar:read");
    /**
    * Get the element to move all the emails on this page to the trash.
    * @returns HTML element
    */
    const moveToTrash = () => getFromTestId("toolbar:movetotrash");
    /**
    * Get the element to go to the next page.
    * @returns HTML element
    @@ -31,34 +40,54 @@ const next = () => getFromTestId("pagination-row:go-to-next-page");
    * @param {number} clickTime The time to wait between button clicks.
    * @param {number} interval The interval id to clear if there's no next page.
    */
    const allAsReadAndNext = (clickTime, interval) => () => {
    all().click();
    setTimeout(() => {
    read().click();
    const actionOnAllAndNext =
    (clickTime, action = "markAsRead") =>
    () => {
    // select all emails
    all().click();
    setTimeout(() => {
    const nextElement = next();
    nextElement ? nextElement.click() : clearInterval(interval);
    // choose element depending on the action
    const element = action === "markAsRead" ? markRead() : moveToTrash();
    // click element
    element.click();
    setTimeout(() => {
    // Find button to next page
    const nextElement = next();
    if (nextElement && !nextElement.disabled) {
    // Next page is available: click it
    nextElement.click();
    } else {
    // No next page: stop
    console.info("No more pages. Stopping.");
    clearInterval(interval);
    }
    }, clickTime);
    }, clickTime);
    }, clickTime);
    };
    };

    /**
    *
    * @param {number} clickTime The time to wait between button clicks.
    * @param {number} loadingTime The time to wait for a single page to load, i.e.
    * the time between calls to allAsReadAndNext()
    * the time between calls to actionOnAllAndNext()
    * @returns interval id
    */
    const start = (clickTime, loadingTime) => {
    var interval = null;
    interval = setInterval(allAsReadAndNext(clickTime, interval), loadingTime);
    return interval;
    const start = (clickTime, loadingTime, action = "markAsRead") => {
    interval = setInterval(actionOnAllAndNext(clickTime, action), loadingTime);
    };

    // -----------------------
    // ----- Execution -----
    // -----------------------

    // state variable to stop the interval loop
    var interval = null;
    // The time to wait between button clicks.
    const clickTime = 200;
    // The time to wait for a single page to load, i.e. the time between calls to allAsReadAndNext()
    // The time to wait for a single page to load, i.e. the time between calls to actionOnAllAndNext()
    const loadingTime = 4000;
    // The action to perform: markAsRead or moveToTrash
    const action = "moveToTrash";
    // Start the bulk mark as read process.
    const intervalId = start(clickTime, loadingTime);
    // clearInterval(intervalId);
    start(clickTime, loadingTime, action);
    // clearInterval(interval);
  3. vict0rsch created this gist Feb 22, 2022.
    64 changes: 64 additions & 0 deletions protonmail-read.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    /**
    *
    * @param {string} testId The string id for the element to find,
    * matchec against elements' `data-testid` attribute.
    * @returns HTML element
    */
    const getFromTestId = (testId) => {
    return Array.from(document.getElementsByTagName("button")).filter(
    (element) => element.getAttribute("data-testid") === testId
    )[0];
    };

    /**
    * Get the element to select all the emails on this page.
    * @returns HTML element
    */
    const all = () => document.getElementById("idSelectAll");
    /**
    * Get the element to mark all the emails on this page as "read".
    * @returns HTML element
    */
    const read = () => getFromTestId("toolbar:read");
    /**
    * Get the element to go to the next page.
    * @returns HTML element
    */
    const next = () => getFromTestId("pagination-row:go-to-next-page");

    /**
    * Perform 1 iteration of selectAll-markAsRead-goToNextPage.
    * @param {number} clickTime The time to wait between button clicks.
    * @param {number} interval The interval id to clear if there's no next page.
    */
    const allAsReadAndNext = (clickTime, interval) => () => {
    all().click();
    setTimeout(() => {
    read().click();
    setTimeout(() => {
    const nextElement = next();
    nextElement ? nextElement.click() : clearInterval(interval);
    }, clickTime);
    }, clickTime);
    };

    /**
    *
    * @param {number} clickTime The time to wait between button clicks.
    * @param {number} loadingTime The time to wait for a single page to load, i.e.
    * the time between calls to allAsReadAndNext()
    * @returns interval id
    */
    const start = (clickTime, loadingTime) => {
    var interval = null;
    interval = setInterval(allAsReadAndNext(clickTime, interval), loadingTime);
    return interval;
    };

    // The time to wait between button clicks.
    const clickTime = 200;
    // The time to wait for a single page to load, i.e. the time between calls to allAsReadAndNext()
    const loadingTime = 4000;
    // Start the bulk mark as read process.
    const intervalId = start(clickTime, loadingTime);
    // clearInterval(intervalId);