iThome Arrow Key Pager

Navigate iThome pages using Left (←) and Right (→) arrow keys.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

ستحتاج إلى تثبيت إضافة مثل Stylus لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتتمكن من تثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

(لدي بالفعل مثبت أنماط للمستخدم، دعني أقم بتثبيته!)

// ==UserScript==
// @name         iThome Arrow Key Pager
// @namespace    https://github.com/livinginpurple
// @version      2025.11.27.15
// @description  Navigate iThome pages using Left (←) and Right (→) arrow keys.
// @description:zh-TW 使用方向鍵前往上一頁(←)、下一頁(→)
// @license      WTFPL
// @author       livinginpurple (Refactored by Gemini)
// @match        **://ithelp.ithome.com.tw/*
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';

    const CONFIG = {
        SELECTORS: {
            PREV: '.fa-angle-left',
            NEXT: '.fa-angle-right'
        },
        IGNORED_TAGS: ['INPUT', 'TEXTAREA', 'SELECT']
    };

    /**
     * 執行頁面跳轉
     * @param {string} selector CSS 選擇器
     * @param {string} direction 除錯用的方向名稱
     */
    const navigate = (selector, direction) => {
        const targetIcon = document.querySelector(selector);
        const clickableLink = targetIcon?.closest('a') || targetIcon;
        if (clickableLink) {
            clickableLink.click();
        } else {
            alert(`No ${direction} page!`);
        }
    };

    /**
     * 處理鍵盤事件
     * @param {KeyboardEvent} event
     */
    const handleKeydown = (event) => {
        if (event.altKey || event.ctrlKey || event.shiftKey || event.metaKey) return;
        const activeElement = document.activeElement;
        if (activeElement && (
            CONFIG.IGNORED_TAGS.includes(activeElement.tagName) ||
            activeElement.isContentEditable
        )) {
            return;
        }

        switch (event.key) {
            case 'ArrowLeft':
                navigate(CONFIG.SELECTORS.PREV, 'Previous');
                break;
            case 'ArrowRight':
                navigate(CONFIG.SELECTORS.NEXT, 'Next');
                break;
        }
    };

    document.addEventListener('keydown', handleKeydown);
    console.log(`${GM_info.script.name} loaded.`);
})();