Focus Input Keybind

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

当前为 2020-07-08 提交的版本,查看 最新版本

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