Greasy Fork is available in English.

Discussions » Development

Problems with GMail script not working when changing pages

§
Posted: 29-10-2023

To use this script, please ensure that the website is currently in focus. To toggle the selection of all emails, simply press the 'A' key. To search for messages from the domain of a hovered email, first highlight the email and then press the 'F' key.

However, there is an issue with this script when transitioning between page one and page two. In most cases, it stops working. Even though the event listeners remain active and trigger an alert upon key presses, pressing the 'A' key will run the code but it doesn't work, but then I can run the same code in the console, and it'll work.


// ==UserScript==
// @name         Find all from sender (and delete)
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://mail.google.com/mail/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=google.com
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    document.addEventListener("keydown", function(event) {
        if (event.key === "f" || event.key === "F") {
            var tbody = document.querySelector("div > div > div > div > div.Cp > div > table > tbody");
            var elem = tbody.querySelector("tr.aqw > td > div > span > span");
            var email = elem.getAttribute("email");
            var email_start = email.indexOf("@") + 1;
            var email_domain = email.substring(email_start);
            var url = "https://mail.google.com/mail/u/0/#advanced-search/from=";
            document.location.href = url + email_domain;
        } else if (event.key === "a" || event.key === "A") {
            document.querySelector("div.J-J5-Ji.J-JN-M-I-Jm > span").click();
        }
    });
})();
§
Posted: 03-11-2023

there is an issue with this script when transitioning between page one and page two. In most cases, it stops working

That's because when you're going to next page gmail creates a new second one layout and your code is just keeps looking for the first one. For the 'A' key this seems to be fixed by replacing document.querySelector("div.J-J5-Ji.J-JN-M-I-Jm > span").click() to [...document.querySelectorAll("div.J-J5-Ji.J-JN-M-I-Jm > span")].pop().click(), and for the 'F' you need to investigate by yourself

Post reply

Sign in to post a reply.