Focus Input Keybind

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

当前为 2020-04-27 提交的版本,查看 最新版本

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