Advertisement
piotrirving

Untitled

Mar 12th, 2025
107
0
6 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Gmail Label Toggle Shortcut
  3. // @namespace    http://tampermonkey.net/
  4. // @version      1.2
  5. // @description  Toggle Gmail label "[01] Follow Up" using the "1" key
  6. // @match        https://mail.google.com/*
  7. // @grant        none
  8. // ==/UserScript==
  9.  
  10. (function() {
  11.     'use strict';
  12.  
  13.     // Define the label to toggle
  14.     const LABEL_NAME = "[01] Follow Up";
  15.  
  16.     // Utility: Wait for an element to appear in the DOM
  17.     function waitForElement(selector, timeout = 5000) {
  18.         return new Promise((resolve, reject) => {
  19.             const intervalTime = 100;
  20.             let elapsed = 0;
  21.             const interval = setInterval(() => {
  22.                 const el = document.querySelector(selector);
  23.                 if (el) {
  24.                     clearInterval(interval);
  25.                     resolve(el);
  26.                 }
  27.                 elapsed += intervalTime;
  28.                 if (elapsed >= timeout) {
  29.                     clearInterval(interval);
  30.                     reject(new Error("Element not found: " + selector));
  31.                 }
  32.             }, intervalTime);
  33.         });
  34.     }
  35.  
  36.     // Listen for keydown events
  37.     document.addEventListener("keydown", function(event) {
  38.         // Avoid interfering with typing
  39.         if (event.target.tagName === "INPUT" || event.target.tagName === "TEXTAREA") return;
  40.         // Check if "1" key is pressed
  41.         if (event.key === "1") {
  42.             event.preventDefault();
  43.             toggleLabel(LABEL_NAME);
  44.         }
  45.     });
  46.  
  47.     // Toggle the label on/off for the selected email(s)
  48.     async function toggleLabel(labelName) {
  49.         try {
  50.             // Wait for the Labels button in the Gmail toolbar
  51.             const labelButton = await waitForElement('div[aria-label="Labels"]');
  52.             labelButton.click();
  53.             console.log("Labels button clicked.");
  54.  
  55.             // Wait for the label dialog/menu to appear (this selector may vary)
  56.             const labelDialog = await waitForElement('div[role="menu"]', 3000);
  57.             console.log("Label dialog appeared.");
  58.  
  59.             // Look for the label option in the dialog
  60.             let options = labelDialog.querySelectorAll('div[role="menuitemcheckbox"]');
  61.             let foundOption = null;
  62.             options.forEach(opt => {
  63.                 if (opt.textContent && opt.textContent.trim().indexOf(labelName) !== -1) {
  64.                     foundOption = opt;
  65.                 }
  66.             });
  67.  
  68.             if (foundOption) {
  69.                 // If found, click it to toggle the label (applies or removes)
  70.                 foundOption.click();
  71.                 console.log("Toggled label:", labelName);
  72.             } else {
  73.                 // If not found, try typing into the input field (if available)
  74.                 const input = await waitForElement('input[aria-label="Label as"]', 3000);
  75.                 input.value = labelName;
  76.                 input.dispatchEvent(new Event('input', { bubbles: true }));
  77.                 console.log("Typed label name:", labelName);
  78.  
  79.                 // Wait a bit for the options to update and then click the matching one
  80.                 setTimeout(() => {
  81.                     let updatedOptions = labelDialog.querySelectorAll('div[role="menuitemcheckbox"]');
  82.                     let matchingOption = null;
  83.                     updatedOptions.forEach(opt => {
  84.                         if (opt.textContent && opt.textContent.trim().indexOf(labelName) !== -1) {
  85.                             matchingOption = opt;
  86.                         }
  87.                     });
  88.                     if (matchingOption) {
  89.                         matchingOption.click();
  90.                         console.log("Clicked matching label option for:", labelName);
  91.                     } else {
  92.                         console.error("No matching label option found for:", labelName);
  93.                     }
  94.                 }, 500);
  95.             }
  96.         } catch (error) {
  97.             console.error("Error in toggleLabel:", error);
  98.         }
  99.     }
  100. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement