AutoFocusSearchBox

Automatically focuses the search box on page load

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @license MIT
// @name         AutoFocusSearchBox
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Automatically focuses the search box on page load
// @author       aceitw
// @match        *://*/*
// @grant        GM_registerMenuCommand
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

(function () {
    'use strict';

    // Utility: Determine if the current hostname is in the user-defined list
    const hostname = window.location.hostname;
    const siteList = GM_getValue('siteList', []);

    // Try to focus the search box
    function focusSearchBox() {
        const inputTags = Array.from(document.querySelectorAll('input[type="search"], input[type="text"], input:not([type])'))
            .filter(el => el.offsetParent !== null && el.offsetHeight > 0 && el.offsetWidth > 0);

        // Try the first element that looks like a search bar
        const searchInput = inputTags.find(input =>
            input.placeholder?.toLowerCase().includes('search') ||
            input.name?.toLowerCase().includes('search') ||
            input.id?.toLowerCase().includes('search') ||
            input.ariaLabel?.toLowerCase().includes('search')
        );

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

    // Focus only if the site is allowed
    if (siteList.includes(hostname)) {
        window.addEventListener('load', () => {
            setTimeout(focusSearchBox, 200); // Delay to wait for page load
        });
    }

    // === SETTINGS UI ===
    GM_registerMenuCommand('🔧 Manage Focus Sites', () => {
        const currentList = GM_getValue('siteList', []);
        const input = prompt(
            `Enter a list of hostnames (one per line).\nExample:\nwww.google.com\ndocs.google.com`,
            currentList.join('\n')
        );
        if (input !== null) {
            const newList = input
                .split('\n')
                .map(s => s.trim())
                .filter(s => s.length > 0);
            GM_setValue('siteList', newList);
            alert('✅ Site list updated!\n\nReload the page to apply changes.');
        }
    });
})();