QuickSearch

Use '/' to quickly focus on the search input

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         QuickSearch
// @name:zh-CN   QuickSearch
// @namespace    https://github.com/zitup
// @version      1.0.3
// @description  Use '/' to quickly focus on the search input
// @description:zh-cn 按 `'/'` 键快速聚焦并滚动到搜索输入框处。
// @author       zitup
// @homepage     https://github.com/zitup/quick-search
// @include      *
// @exclude      https://www.google.com/*
// @exclude      https://www.google.com.hk/*
// @exclude      https://github.com/*
// @exclude      https://developer.mozilla.org/*
// @icon         https://www.google.com/s2/favicons?domain=w3.org
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
  'use strict';

  // 监听键盘输入 '/' 搜索
  document.addEventListener('keydown', (e) => {
      if (
          e.key === '/' &&
          !e.metaKey &&
          !e.ctrlKey &&
          !["TEXTAREA", "INPUT"].includes(e.target.tagName)
      ) {
          main()
      }
  })

  function main() {
      // 选择第一个 type 为 search 或存在 autofocus 且在页面显示的
      const autofocusOrSearch = document.querySelector('input[autofocus],input[type=search]')
      if (autofocusOrSearch && isVisible(autofocusOrSearch)) {
          focusAndScrollIntoView(autofocusOrSearch)
          return
      }

      // 选择第一个 id/class 中包含[search]关键词且在页面显示的
      const idOrClassContainSearch = document.querySelectorAll('input[id*=search],input[class*=search]')
      if (idOrClassContainSearch.length) {
          const element = findInNodeList(idOrClassContainSearch)
          if (element) {
              focusAndScrollIntoView(element)
              return
          }
      }

      // 选择第一个 placeholder 中包含[search/搜索]关键词且在页面显示的
      const placeholderContainSearch = document.querySelectorAll('input[placeholder*=search],input[placeholder*=搜索]')
      if (placeholderContainSearch.length) {
          const element = findInNodeList(placeholderContainSearch)
          if (element) {
              focusAndScrollIntoView(element)
              return
          }
      }

      // 选择第一个在页面显示的
      const textInputTypes = ['hidden', 'button', 'checkbox', 'color', 'file', 'image', 'radio', 'range', 'reset', 'submit']
      const selector = textInputTypes.map(t => `[type=${t}]`).join(',')
      const firstInput = document.querySelector(`input:not(${selector})`)
      if (firstInput && isVisible(firstInput)) {
          focusAndScrollIntoView(firstInput)
          return
      }
  }

  // 判断 NodeList 中是否有可用的
  function findInNodeList(list) {
      return [].find.call(list, (item) => isVisible(item))
  }

  // 判断元素是否可见
  function isVisible(element) {
      const style = getComputedStyle(element)
      return (
          !!element.getClientRects().length &&
          style.visibility !== 'hidden' &&
          style.width !== 0 &&
          style.height !== 0 &&
          style.opacity !== 0
      )
  }

  // 聚焦元素并滚动到视野
  function focusAndScrollIntoView(element) {
      event.preventDefault();
      const pos = element.value.length;
      element.setSelectionRange(pos, pos)
      element.focus()
      window.scrollTo({top: element.getBoundingClientRect().top + window.pageYOffset - 100, behavior: 'smooth'});
  }
})();