按 / 回到搜索框

想要输入新的搜索内容时,按下 ` / ` 键,光标焦点就会自动跳转到搜索框中,接下来就可以愉快的输入新的搜索内容啦!

  1. // ==UserScript==
  2. // @name 按 / 回到搜索框
  3. // @version 0.2.4
  4. // @description 想要输入新的搜索内容时,按下 ` / ` 键,光标焦点就会自动跳转到搜索框中,接下来就可以愉快的输入新的搜索内容啦!
  5. // @author missiont522
  6. // @license MIT
  7. // @icon https://www.bing.com/sa/simg/favicon-trans-bg-blue-mg.ico
  8. // @match *://*.baidu.com/*
  9. // @match *://*.sogou.com/*
  10. // @match *://*.taobao.com/*
  11. // @match *://*.tmall.com/*
  12. // @match *://*.bilibili.com/*
  13. // @match *://*.greasyfork.org/*
  14. // @match *://*.douban.com/*
  15. // @match *://fanyi.sogou.com/*
  16. // @match *://share.dmhy.org/*
  17. // @match *://*.weibo.com/*
  18. // @match *://*.shanbay.com/*
  19. // @match *://*.jd.com/*
  20. // @match *://*.cnki.net/*
  21. // @match *://developers.weixin.qq.com/*
  22. // @match *://*.ixigua.com/*
  23. // @match *://kimi.moonshot.cn/*
  24. // @match *://*.toutiao.com/*
  25. // @match *://*.xiaohongshu.com/*
  26. // @namespace http://tampermonkey.net/
  27. // ==/UserScript==
  28.  
  29. const siteList = [
  30. { id: 0, name: "baidu", query: "#kw" },
  31. // { id: 1, name: "google", query: "没有" },
  32. { id: 3, name: "sogou", query: `#upquery.query` },
  33. {
  34. id: 4,
  35. name: ["taobao", "tmall", "jd", "bilibili"],
  36. query: `[accesskey="s"]`,
  37. },
  38. {
  39. id: 5,
  40. name: "search.bilibili",
  41. query: `.search-input-el`,
  42. },
  43. {
  44. id: 7,
  45. name: ["greasyfork", "toutiao", "ixigua", "developers.weixin.qq.com"],
  46. query: '[type="search"]',
  47. },
  48. { id: 8, name: "douban", query: "#inp-query" },
  49. { id: 10, name: "fanyi.sogou", query: "#trans-input" },
  50. { id: 11, name: "dmhy", query: "input#keyword" },
  51. { id: 12, name: "weibo", query: "input.woo-input-main" },
  52. { id: 13, name: "shanbay", query: "#search .input" },
  53. { id: 14, name: ["cnki", "xiaohongshu"], query: ".search-input" },
  54. { id: 15, name: "baike.baidu", query: ".searchInput" },
  55. { id: 16, name: "tieba.baidu", query: "#wd1" },
  56. { id: 17, name: "kimi.moonshot", query: `div[contenteditable="true"]` },
  57. // { id: 99, name: '', query: ``,},
  58. ];
  59.  
  60. siteList.forEach((item) => {
  61. const names = Array.isArray(item.name) ? item.name : [item.name];
  62. const isMatched = names.some((name) => location.host.includes(name));
  63. if (!isMatched) {
  64. return;
  65. }
  66.  
  67. /** @type {HTMLInputElement} */
  68. let form = document.querySelector(item.query);
  69. // console.log("|", item.name, form);
  70. document.documentElement.addEventListener("keydown", (event) => {
  71. if (event.key !== "/") return;
  72. if (!form) {
  73. form = document.querySelector(item.query);
  74. }
  75.  
  76. if (document.activeElement === form) {
  77. return;
  78. }
  79.  
  80. form.focus();
  81. event.preventDefault();
  82.  
  83. if (form.value) {
  84. const contentLen = form.value.length;
  85. form.setSelectionRange(contentLen, contentLen);
  86. }
  87. });
  88. });