AutoFocusSearchBox

Automatically focuses the search box on page load

Du musst eine Erweiterung wie Tampermonkey, Greasemonkey oder Violentmonkey installieren, um dieses Skript zu installieren.

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.

Sie müssten eine Skript Manager Erweiterung installieren damit sie dieses Skript installieren können

(Ich habe schon ein Skript Manager, Lass mich es installieren!)

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.

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

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

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

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

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

// ==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.');
        }
    });
})();