Amazon Search Bar Highlight Fix

Keep Amazon's search bar active and allow proper text highlighting/editing, using a lightweight self-healing observer only when needed.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Amazon Search Bar Highlight Fix
// @namespace    fixThisAmazonSeriously
// @version      1.3
// @description  Keep Amazon's search bar active and allow proper text highlighting/editing, using a lightweight self-healing observer only when needed.
// @author       Max Lemkin
// @match        https://*.amazon.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    function applyFix(searchInput) {
        if (!searchInput || searchInput.dataset.fixApplied) return;
        searchInput.dataset.fixApplied = "true";

        let mouseDownInside = false;

        searchInput.addEventListener('mousedown', () => {
            mouseDownInside = true;
        });

        document.addEventListener('mouseup', () => {
            if (mouseDownInside) {
                setTimeout(() => {
                    searchInput.focus();
                    mouseDownInside = false;
                }, 0);
            }
        });

        searchInput.addEventListener('blur', (e) => {
            if (mouseDownInside) {
                e.stopImmediatePropagation();
                setTimeout(() => searchInput.focus(), 0);
            }
        }, true);

        console.log('Fix applied to search bar');
    }

    function init() {
        const searchInput = document.getElementById('twotabsearchtextbox');
        if (searchInput) {
            applyFix(searchInput);
        } else {
            // Start temporary observer only if the box is missing
            const observer = new MutationObserver(() => {
                const found = document.getElementById('twotabsearchtextbox');
                if (found) {
                    applyFix(found);
                    observer.disconnect(); // Stop watching once fixed
                    console.log("Observer disconnected (search bar found)");
                }
            });
            observer.observe(document.body, { childList: true, subtree: true });
            console.log("Observer enabled (waiting for search bar)");
        }
    }

    // Initial run
    if (document.readyState === 'complete' || document.readyState === 'interactive') {
        init();
    } else {
        window.addEventListener('DOMContentLoaded', init);
    }
})();