Filter Gmail shortcut

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

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램을 설치해야 합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 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);
})();