聚焦第一个文本输入框

Press Alt+1 to focus the first text input on all pages

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         聚焦第一个文本输入框
// @version      0.6
// @description  Press Alt+1 to focus the first text input on all pages
// @author       hiisme
// @match        *://*/*
// @grant        none
// @namespace https://greasyfork.org/users/217852
// ==/UserScript==

(function() {
    'use strict';

    // 监听键盘按下事件
    document.addEventListener('keydown', function(event) {
        // 检查是否按下了Alt键和1键
        if (event.altKey && event.key === '1') {
            event.preventDefault();
            focusFirstTextInput();
        }
    }, false);

    // 尝试聚焦第一个文本输入框
    function focusFirstTextInput() {
        // 扩展的选择器,尝试匹配更多类型的输入框
        var inputSelector = [
            'input[type="text"]:not([disabled]):not([readonly])',
            'input[type="email"]:not([disabled]):not([readonly])',
            'input[type="password"]:not([disabled]):not([readonly])',
            'input[type="search"]:not([disabled]):not([readonly])',
            'input[type="tel"]:not([disabled]):not([readonly])',
            'input[type="url"]:not([disabled]):not([readonly])',
            'input:not([type]):not([disabled]):not([readonly])', // 无type属性的input也默认为text
            'textarea:not([disabled]):not([readonly])',
            '[contenteditable="true"]:not([disabled]):not([readonly])', // 可编辑区域
            '[role="textbox"]:not([disabled]):not([readonly])' // 带有特定角色的元素
        ].join(',');

        // 排除不可见的元素
        var allInputs = document.querySelectorAll(inputSelector);
        var firstVisibleInput = Array.from(allInputs).find(input => {
            var style = window.getComputedStyle(input);
            return style.display !== 'none' && style.visibility !== 'hidden';
        });

        if (firstVisibleInput) {
            firstVisibleInput.focus();
        }
    }
})();