Press Slash to Search

After pressing slash, you can enter slash to search.

  1. // ==UserScript==
  2. // @name Press Slash to Search
  3. // @namespace impossible98/press-slash-to-search
  4. // @version 0.0.4
  5. // @author impossible98
  6. // @description After pressing slash, you can enter slash to search.
  7. // @license MIT
  8. // @icon https://vitejs.dev/logo.svg
  9. // @homepageURL https://github.com/impossible98/press-slash-to-search-extension
  10. // @match https://search.bilibili.com/*
  11. // @match https://www.bilibili.com/*
  12. // @match https://psnine.com/psngame
  13. // @match https://www.douyin.com/*
  14. // ==/UserScript==
  15.  
  16. (function () {
  17. 'use strict';
  18.  
  19. function printError(text) {
  20. console.log(
  21. `%c ${text}`,
  22. "color: #fff; background-color: #F44336; padding: 10px; border-radius: 5px;"
  23. );
  24. }
  25. function printSuccess(text) {
  26. console.log(
  27. `%c ${text}`,
  28. "color: #fff; background-color: #4CAF50; padding: 10px; border-radius: 5px;"
  29. );
  30. }
  31. function handleKeydown(query) {
  32. if (!query || typeof query !== "string" || query.trim() === "") {
  33. printError("输入的查询字符串无效。");
  34. return;
  35. }
  36. const cleanedQuery = query.trim();
  37. if (document.querySelectorAll(query).length > 1) {
  38. printError(`指定的输入框: ${query} 不是唯一的输入框`);
  39. return;
  40. }
  41. let form = document.querySelector(
  42. cleanedQuery
  43. );
  44. if (!form || form.tagName !== "INPUT") {
  45. printError(`无法找到指定的输入框: ${cleanedQuery}`);
  46. return;
  47. }
  48. document.documentElement.removeEventListener("keydown", handleKeyDownEvent);
  49. document.documentElement.addEventListener("keydown", handleKeyDownEvent);
  50. function handleKeyDownEvent(event) {
  51. if (event.key === "/") {
  52. if (form) {
  53. form.focus();
  54. printSuccess(`已聚焦到输入框${cleanedQuery}`);
  55. const tempv = form.value;
  56. form.value = "";
  57. form.value = tempv;
  58. }
  59. event.preventDefault();
  60. }
  61. }
  62. }
  63. const eventBound = /* @__PURE__ */ new WeakMap();
  64. function handleEsc() {
  65. if (!eventBound.has(document.documentElement)) {
  66. document.documentElement.addEventListener("keydown", (event) => {
  67. if (event.key !== "Escape") {
  68. return;
  69. }
  70. try {
  71. if (document.activeElement instanceof HTMLInputElement) {
  72. document.activeElement.blur();
  73. }
  74. } catch (error) {
  75. printError("Error while blurring the active element.");
  76. }
  77. });
  78. eventBound.set(document.documentElement, true);
  79. }
  80. }
  81. function handleSlash() {
  82. if (location.href.includes("search.bilibili.com/")) {
  83. handleKeydown("input.search-input-el");
  84. } else if (location.href.includes("www.bilibili.com")) {
  85. handleKeydown("input.nav-search-input");
  86. } else if (location.href.includes("https://psnine.com/psngame")) {
  87. handleKeydown("input.btn-large");
  88. } else if (location.href.includes("https://www.douyin.com/search/")) {
  89. handleKeydown("input.igFQqPKs");
  90. } else if (location.href.includes("https://www.douyin.com")) {
  91. handleKeydown("input.st2xnJtZ.YIde9aUh");
  92. }
  93. }
  94. function main() {
  95. handleSlash();
  96. handleEsc();
  97. }
  98. main();
  99.  
  100. })();