Focus Input Keybind

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

当前为 2021-01-01 提交的版本,查看 最新版本

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