Filter Gmail shortcut

Filter Gmail messages by pressing \"alt+g\"

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Filter Gmail shortcut
// @namespace    https://stojanow.com/
// @version      0.2.0
// @description  Filter Gmail messages by pressing \"alt+g\"
// @author       Piotr Stojanow (https://github.com/psto/)
// @license      MIT
// @homepageURL  https://github.com/psto/userscript-clean-copy-url
// @supportURL   https://github.com/psto/userscript-clean-copy-url
// @match        *://mail.google.com/*
// @icon         data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>📩</text></svg>
// ==/UserScript==

(function() {
  'use strict'

  function handleKeyPress(event) {
    // Check if the alt+g keys are pressed
    if (event.altKey && event.code === 'KeyG') {
      const main = document.querySelector('div[role=main]');
      const emailElements = main.querySelectorAll('tr')

      // Filter out selected email rows
      let selectedEmailRows = Array.from(emailElements).filter((row) => {
        const tdElements = row.querySelectorAll('td');

        const hasAriaChecked = Array.from(tdElements).some((td) => {
          const isChecked = td.querySelector('div[aria-checked]')

          if (isChecked) {
            return isChecked.getAttribute('aria-checked') === 'true'
          }
        });

        const hasEmailSpan = row.querySelector('span[email]');

        return hasAriaChecked && hasEmailSpan;
      });

      // When filtering from an opened email, select the element containing the email
      if (selectedEmailRows.length === 0) {
        selectedEmailRows.push(emailElements[1])
      }

      let emails = []

      // Extract email addresses from the selected rows
      selectedEmailRows.filter((emailRow) => {
        const emailElement = emailRow.querySelector('td span[email]')
        const emailAddress = emailElement.getAttribute('email');
        emails.push(encodeURIComponent(emailAddress))
      })

      const searchUrl = `https://mail.google.com/mail/u/0/#search/from:(${emails.join(' OR ')})`;

      // Navigate to the Gmail search results page
      window.location.href = searchUrl;
    }
  }

  document.addEventListener('keydown', handleKeyPress);
})();