Focus Input Keybind

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

当前为 2019-08-22 提交的版本,查看 最新版本

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