Press Slash to Search

After pressing slash, you can enter the search input

// ==UserScript==
// @name         Press Slash to Search
// @namespace    impossible98/press-slash-to-search
// @version      0.12.1
// @author       impossible98
// @description  After pressing slash, you can enter the search input
// @license      MIT
// @icon         https://raw.githubusercontent.com/impossible98/press-slash-to-search-extension/master/src/favicon.svg
// @homepageURL  https://github.com/impossible98/press-slash-to-search-extension
// @match        https://search.bilibili.com/*
// @match        https://www.bilibili.com/*
// @match        https://www.douyin.com/*
// ==/UserScript==

(function () {
  'use strict';

  function printError(text) {
    console.log(
      `%c ${text}`,
      "color: #fff; background-color: #F44336; padding: 10px; border-radius: 5px;"
    );
  }
  function printSuccess(text) {
    console.log(
      `%c ${text}`,
      "color: #fff; background-color: #4CAF50; padding: 10px; border-radius: 5px;"
    );
  }
  function handleKeydown(query) {
    if (!query || typeof query !== "string" || query.trim() === "") {
      printError("输入的查询字符串无效。");
      return;
    }
    const cleanedQuery = query.trim();
    if (document.querySelectorAll(query).length > 1) {
      printError(`指定的输入框: ${query} 不是唯一的输入框`);
      return;
    }
    let form = document.querySelector(
      cleanedQuery
    );
    if (!form || form.tagName !== "INPUT") {
      printError(`无法找到指定的输入框: ${cleanedQuery}`);
      return;
    }
    document.documentElement.removeEventListener("keydown", handleKeyDownEvent);
    document.documentElement.addEventListener("keydown", handleKeyDownEvent);
    function handleKeyDownEvent(event) {
      if (event.key === "/") {
        if (form) {
          form.focus();
          printSuccess(`已聚焦到输入框${cleanedQuery}`);
          const tempv = form.value;
          form.value = "";
          form.value = tempv;
        }
        event.preventDefault();
      }
    }
  }
  const eventBound = /* @__PURE__ */ new WeakMap();
  function handleEsc() {
    if (!eventBound.has(document.documentElement)) {
      document.documentElement.addEventListener("keydown", (event) => {
        if (event.key !== "Escape") {
          return;
        }
        try {
          if (document.activeElement instanceof HTMLInputElement) {
            document.activeElement.blur();
          }
        } catch (error) {
          printError("Error while blurring the active element.");
        }
      });
      eventBound.set(document.documentElement, true);
    }
  }
  function handleSlash() {
    if (location.href.includes("https://search.bilibili.com/")) {
      handleKeydown("input.search-input-el");
    } else if (location.href.includes("https://www.douyin.com/search/")) {
      handleKeydown("input.igFQqPKs");
    } else if (location.href.includes("https://www.douyin.com")) {
      handleKeydown("input.st2xnJtZ.YIde9aUh");
    } else if (location.href === "https://www.bilibili.com/account/history") {
      handleKeydown("input.b-head-search_input");
    } else if (location.href.includes("https://www.bilibili.com")) {
      handleKeydown("input.nav-search-input");
    } else if (location.href.includes("https://greasyfork.org/")) {
      handleKeydown('[type="search"]');
    } else if (location.href.includes("https://www.douyu.com")) {
      handleKeydown("#header-search-input.Search-text");
    } else if (location.href.includes("https://www.huya.com")) {
      handleKeydown("#search-bar-input");
    } else if (location.href.includes("https://kb.synology.cn/zh-cn")) {
      handleKeydown('[name="query"]');
    } else if (location.href.includes("https://www.baidu.com")) {
      handleKeydown("#kw.s_ipt");
    } else if (location.href.includes("https://scoop.sh")) {
      handleKeydown(".form-control.form-control-lg");
    } else if (location.href === "https://tv.doutoutiao.cc/") {
      handleKeydown("#scbar_txt.xg1");
    } else if (location.href.includes("https://tv.doutoutiao.cc")) {
      handleKeydown("#scform_srchtxt.xg1");
    } else if (location.href.includes("https://live.bilibili.com")) {
      handleKeydown(".v-middle.nav-search-content");
    } else if (location.href.includes("https://s.taobao.com/search")) {
      handleKeydown("input#q");
    }
  }
  function main() {
    handleSlash();
    handleEsc();
  }
  main();

})();