Gmail pagination Key Shortcut

paginate Gmail by arrow keys

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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);
})();