Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Gmail Label Toggle Shortcut
- // @namespace http://tampermonkey.net/
- // @version 1.2
- // @description Toggle Gmail label "[01] Follow Up" using the "1" key
- // @match https://mail.google.com/*
- // @grant none
- // ==/UserScript==
- (function() {
- 'use strict';
- // Define the label to toggle
- const LABEL_NAME = "[01] Follow Up";
- // Utility: Wait for an element to appear in the DOM
- function waitForElement(selector, timeout = 5000) {
- return new Promise((resolve, reject) => {
- const intervalTime = 100;
- let elapsed = 0;
- const interval = setInterval(() => {
- const el = document.querySelector(selector);
- if (el) {
- clearInterval(interval);
- resolve(el);
- }
- elapsed += intervalTime;
- if (elapsed >= timeout) {
- clearInterval(interval);
- reject(new Error("Element not found: " + selector));
- }
- }, intervalTime);
- });
- }
- // Listen for keydown events
- document.addEventListener("keydown", function(event) {
- // Avoid interfering with typing
- if (event.target.tagName === "INPUT" || event.target.tagName === "TEXTAREA") return;
- // Check if "1" key is pressed
- if (event.key === "1") {
- event.preventDefault();
- toggleLabel(LABEL_NAME);
- }
- });
- // Toggle the label on/off for the selected email(s)
- async function toggleLabel(labelName) {
- try {
- // Wait for the Labels button in the Gmail toolbar
- const labelButton = await waitForElement('div[aria-label="Labels"]');
- labelButton.click();
- console.log("Labels button clicked.");
- // Wait for the label dialog/menu to appear (this selector may vary)
- const labelDialog = await waitForElement('div[role="menu"]', 3000);
- console.log("Label dialog appeared.");
- // Look for the label option in the dialog
- let options = labelDialog.querySelectorAll('div[role="menuitemcheckbox"]');
- let foundOption = null;
- options.forEach(opt => {
- if (opt.textContent && opt.textContent.trim().indexOf(labelName) !== -1) {
- foundOption = opt;
- }
- });
- if (foundOption) {
- // If found, click it to toggle the label (applies or removes)
- foundOption.click();
- console.log("Toggled label:", labelName);
- } else {
- // If not found, try typing into the input field (if available)
- const input = await waitForElement('input[aria-label="Label as"]', 3000);
- input.value = labelName;
- input.dispatchEvent(new Event('input', { bubbles: true }));
- console.log("Typed label name:", labelName);
- // Wait a bit for the options to update and then click the matching one
- setTimeout(() => {
- let updatedOptions = labelDialog.querySelectorAll('div[role="menuitemcheckbox"]');
- let matchingOption = null;
- updatedOptions.forEach(opt => {
- if (opt.textContent && opt.textContent.trim().indexOf(labelName) !== -1) {
- matchingOption = opt;
- }
- });
- if (matchingOption) {
- matchingOption.click();
- console.log("Clicked matching label option for:", labelName);
- } else {
- console.error("No matching label option found for:", labelName);
- }
- }, 500);
- }
- } catch (error) {
- console.error("Error in toggleLabel:", error);
- }
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement