AutoFocusSearchBox

Automatically focuses the search box on page load

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

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