Press Slash to Search

After pressing slash, you can enter slash to search.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Press Slash to Search
// @namespace    impossible98/press-slash-to-search
// @version      0.0.4
// @author       impossible98
// @description  After pressing slash, you can enter slash to search.
// @license      MIT
// @icon         https://vitejs.dev/logo.svg
// @homepageURL  https://github.com/impossible98/press-slash-to-search-extension
// @match        https://search.bilibili.com/*
// @match        https://www.bilibili.com/*
// @match        https://psnine.com/psngame
// @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("search.bilibili.com/")) {
      handleKeydown("input.search-input-el");
    } else if (location.href.includes("www.bilibili.com")) {
      handleKeydown("input.nav-search-input");
    } else if (location.href.includes("https://psnine.com/psngame")) {
      handleKeydown("input.btn-large");
    } 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");
    }
  }
  function main() {
    handleSlash();
    handleEsc();
  }
  main();

})();