Search Box Keyboard Shortcut for General Websites

Adds a keyboard shortcut to jump to the search box on websites when '/' is pressed. Forked from Lak's script with the same name.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Search Box Keyboard Shortcut for General Websites
// @version      1.4
// @description  Adds a keyboard shortcut to jump to the search box on websites when '/' is pressed. Forked from Lak's script with the same name.
// @author       Arg Anon
// @originalAuthor Lak
// @icon         https://img.icons8.com/?size=32&id=132&format=png
// @match        https://*/*
// @exclude      https://www.google.*/*
// @grant        none
// @license      MIT
// @namespace https://greasyfork.org/users/768814
// ==/UserScript==
  
(function(){
  'use strict';
  document.addEventListener('keydown', function(e) {
    // target the specific elements for the search boxes on websites
    const searchBox = document.querySelector('input[type=text], textarea[aria-label~=Search], input[aria-label~=Search], .searchboxinput, input[id=query-input], input[id=searchform], input[type=Search], textarea[inputmode=search], .js-search-input.search__input--adv, .header-search-field, input[enterkeyhint=go], button.AppHeader-searchButton');
    // check if user is currently typing text somewhere on the site
    const isTyping = document.activeElement.matches("textarea, input[type=text], input[type=url], [contenteditable=true]");

    if (e.key === '/' && searchBox && !isTyping) {
      e.preventDefault();
      if (searchBox.matches("button"))
        searchBox.click();
      else {
        searchBox.focus();
        searchBox.setSelectionRange(0, searchBox.value.length);
      }
    }
  });
}());