slash search bar focus

Focus search bar on pressing SLASH (/) on amazon.de, bing.com, userscript.zone

ของเมื่อวันที่ 04-03-2024 ดู เวอร์ชันล่าสุด

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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.

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

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

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==
// @name         slash search bar focus
// @namespace    guebosch
// @author       guebosch
// @match        https://www.bing.com/search*
// @match        https://www.userscript.zone/search*
// @match        https://www.amazon.de/*
// @match        https://piratehaven.xyz/search.php?*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=bing.com
// @grant        none
// @description  Focus search bar on pressing SLASH (/) on amazon.de, bing.com, userscript.zone
// @version      0.3
// ==/UserScript==

//debugger;

// Comment: google seems to work out of the box.



(function() {
    const hostname = window.location.hostname;
    var searchField;
    window.addEventListener("keypress", e => {
        if (['TEXTAREA', 'INPUT'].includes(e.target.nodeName)) return;
        if (e.key !== "/") return;

        if (['www.bing.com', 'foo.bing.com'].includes(hostname)) {
            searchField = document.querySelector('textarea[type="search"], input[type="search"], input[name="search"], input[placeholder*="Search"]');
        }
        else if (['www.userscript.zone','piratehaven.xyz'].includes(hostname)) {
            searchField = document.querySelector('#search');
        }
        else if (['www.amazon.de'].includes(hostname)) {
            searchField = document.querySelector('#twotabsearchtextbox');
        }

        if (!searchField) return;
        searchField.focus();
        // move cursor to the right
        searchField.setSelectionRange(searchField.value.length, searchField.value.length);
        e.preventDefault();
    });
})();