Focus Input Keybind

Focus to search or a certain text input with forward slash (/) key similar to YouTube.

当前为 2022-04-11 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Focus Input Keybind
  3. // @namespace https://github.com/kittenparry/
  4. // @version 1.6
  5. // @description Focus to search or a certain text input with forward slash (/) key similar to YouTube.
  6. // @author kittenparry
  7. // @match *://*/*
  8. // @grant none
  9. // @license GPL-3.0-or-later
  10. // ==/UserScript==
  11.  
  12. /* LIST:
  13. * *.booru.org
  14. * camwhores.tv
  15. * instagram.com/direct/
  16. * jisho.org
  17. * metal-tracker.com
  18. * nyaa.si
  19. * rarbg.to || rarbg2020.org
  20. * reddit.com
  21. * twitch.tv
  22. * wiktionary.org
  23. */
  24.  
  25. /* CHANGELOG:
  26. * 1.6: +camwhores.tv
  27. * 1.5: +jisho.org
  28. * 1.4.1: +rarbg2020.org (rarbg.to alternative)
  29. * 1.4: +nyaa.si +instagram.com/direct/
  30. * 1.3: +metal-tracker.com
  31. * 1.2: +*.booru.org
  32. * 1.1: +wiktionary.org
  33. * 1.0: initial
  34. */
  35.  
  36. check_focus_input_keybind = (e, val, special) => {
  37. var type = e.target.getAttribute('type');
  38. var tag = e.target.tagName.toLowerCase();
  39. if (type != 'text' && tag != 'textarea') {
  40. if (e.keyCode == 191) { // /
  41. if (special == 'reddit') {
  42. document.getElementById(val).firstChild.focus();
  43. } else if (special == 'instagram') {
  44. document.querySelector(val).firstChild.focus()
  45. } else if (special == 'selector') {
  46. document.querySelector(val).focus();
  47. } else {
  48. document.getElementById(val).focus();
  49. }
  50. }
  51. }
  52. };
  53.  
  54. /* probably need a better way than simply .includes()
  55. * inid: id or other value of the input element
  56. * inspcl: when inid isn't an id (eg. a class) to specify it
  57. */
  58.  
  59. var current_url = window.location.href;
  60.  
  61. if (current_url.includes('.booru.org')) {
  62. var inid = 'tags';
  63. } else if (current_url.includes('camwhores.tv')) {
  64. var inid = 'input[name="q"]';
  65. var inspcl = 'selector';
  66. } else if (current_url.includes('instagram.com/direct/')) {
  67. var inid = 'div[class=" Igw0E IwRSH eGOV_ vwCYk ItkAi "]';
  68. var inspcl = 'instagram';
  69. } else if (current_url.includes('jisho.org')) {
  70. var inid = 'keyword';
  71. } else if (current_url.includes('metal-tracker.com')) {
  72. var inid = 'searchBox';
  73. } else if (current_url.includes('nyaa.si')) {
  74. var inid = 'input[class="form-control search-bar"]';
  75. var inspcl = 'selector';
  76. } else if (current_url.includes('rarbg.to') || current_url.includes('rarbg2020.org')) {
  77. var inid = 'searchinput';
  78. } else if (current_url.includes('reddit.com')) {
  79. var inid = 'search';
  80. var inspcl = 'reddit';
  81. } else if (current_url.includes('twitch.tv')) {
  82. var inid = 'textarea[class="tw-block tw-border-radius-medium tw-font-size-6 tw-full-width tw-textarea tw-textarea--no-resize"]';
  83. var inspcl = 'selector';
  84. } else if (current_url.includes('wiktionary.org')) {
  85. var inid = 'searchInput';
  86. }
  87.  
  88. if (inid != undefined) {
  89. try {
  90. // pass an empty string for input special so to not repeat the event listener code similar to other script
  91. if (!inspcl) {
  92. var inspcl = '';
  93. }
  94. // keyup instead of keydown to prevent the initial entry of a forward slash to input
  95. window.addEventListener('keyup', (e) => check_focus_input_keybind(e, inid, inspcl), false);
  96. } catch (e) {}
  97. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址