slash search bar focus

Focus search bar on SLASH press

目前為 2024-03-03 提交的版本,檢視 最新版本

// ==UserScript==
// @name         slash search bar focus
// @namespace    http://tampermonkey.net/
// @author       limex
// @match        https://www.bing.com/search*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=bing.com
// @grant        none
// @description  Focus search bar on SLASH press
// @version      0.1
// ==/UserScript==

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

(function() {
    window.addEventListener("keypress", e => {
        if (['TEXTAREA', 'INPUT'].includes(e.target.nodeName)) return;
        if (e.key !== "/") return;
        const searchField = document.querySelector('textarea[type="search"], input[type="search"], input[name="search"], input[placeholder*="Search"]');
        if (!searchField) return;
        searchField.focus();
        searchField.setSelectionRange(searchField.value.length, searchField.value.length);
        e.preventDefault();
    });
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址