Focus Input Keybind

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

当前为 2022-06-21 提交的版本,查看 最新版本

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