Gmail pagination Key Shortcut

paginate Gmail by arrow keys

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Gmail pagination Key Shortcut
// @name:ja      Gmailページネーション キーショートカット
// @namespace    https://greasyfork.org/ja/users/570127
// @version      2025.12.25.3
// @description  paginate Gmail by arrow keys
// @description:ja  Gmailで左右のキーでメールを移動します。
// @author       universato
// @license      MIT
// @match        https://mail.google.com/mail/u/0/*
// @supportURL   https://twitter.com/universato
// ==/UserScript==

console.log("【UserScript】Gmail pagination Key Shortcut");

(function () {
  document.addEventListener('keydown', function (event) {
    if (event.ctrlKey || event.metaKey || event.altKey) return;
    if (event.isComposing) return;
    if (event.repeat) return;
    if (event.key !== 'ArrowLeft' && event.key !== 'ArrowRight') return;
    const activeElement = document.activeElement;
    if (activeElement.isContentEditable) return;
    if (/^(INPUT|TEXTAREA)$/.test(activeElement.tagName)) return;

    /* =====================
       Inbox(一覧画面)
       ===================== */
    if (location.href === 'https://mail.google.com/mail/u/0/#inbox') {
      let element = null;

      if (event.key === 'ArrowLeft') {
        element = document.getElementById(':m9');
      } else if (event.key === 'ArrowRight') {
        element = document.getElementById(':ma');
      }

      if (!element) return;

      // Inbox は Gmail の keydown を殺さない
      // (capture も preventDefault もしない)

      element.focus();
      ['mousedown', 'mouseup', 'click'].forEach(type => {
        element.dispatchEvent(
          new MouseEvent(type, {
            bubbles: true,
            cancelable: true
          })
        );
      });

      return;
    }

    /* =====================
       個別メール表示
       ===================== */
    let element = null;

    if (event.key === 'ArrowLeft') {
      element = document.querySelector('div.T-I.J-J5-Ji.adg.T-I-awG.T-I-ax7.T-I-Js-IF.L3');
    } else if (event.key === 'ArrowRight') {
      element = document.querySelector('div.T-I.J-J5-Ji.adg.T-I-awG.T-I-ax7.T-I-Js-Gs.L3');
    }

    element?.click();
  }, false);
})();