Genius Tab to Select

You can use this script to select input result with tab button.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Genius Tab to Select
// @namespace    https://genius.com/
// @version      1.0.1
// @description  You can use this script to select input result with tab button.
// @author       SHOOK1sT and Google Gemini :)
// @match        *://genius.com/*-lyrics
// @match        *://genius.com/*-lyrics?*
// @icon         https://imgur.com/w3KeanO.png
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    console.log('Genius Tab to Select extension loaded');

    // Helper to find the focused option in a react-select or similar dropdown
    function findFocusedOption() {
        // 1. Look for options with "focused" classes (react-select standard)
        const focusedByClass = document.querySelector(
            '[class*="-option--is-focused"], ' +
            '[class*="-Option--is-focused"], ' +
            '[class*="option--is-focused"], ' +
            '[class*="-focused"], ' +
            '.is-focused'
        );
        if (focusedByClass) return focusedByClass;

        // 2. Fallback: Search all visible options and check for highlight background color
        const options = document.querySelectorAll('[class*="option"], [id*="option"], [role="option"]');
        for (const opt of options) {
            if (!(opt.offsetWidth > 0 || opt.offsetHeight > 0)) continue;
            const style = window.getComputedStyle(opt);
            const bg = style.backgroundColor;
            // Color Check
            if (bg !== 'rgba(0, 0, 0, 0)' && bg !== 'rgb(255, 255, 255)' && bg !== 'transparent') {
                return opt;
            }
        }
        return null;
    }

    document.addEventListener('keydown', (event) => {
        // Only intercept Tab key
        if (event.key !== 'Tab') return;

        const target = event.target;
        if (target.tagName !== 'INPUT' && target.tagName !== 'TEXTAREA') return;

        const focusedOption = findFocusedOption();

        if (focusedOption) {
            console.log('Tab pressed while a suggestion is focused. Selecting:', focusedOption.textContent);

            // Prevent default tab behavior (switching fields)
            event.preventDefault();

            // Execute selection with a tiny delay
            setTimeout(() => {
                focusedOption.click();

                // Re-focus the original input
                setTimeout(() => {
                    target.focus();
                }, 50);
            }, 10);
        }
    }, true); // Use capture phase to intercept early
})();