Focus Input Keybind

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

当前为 2019-06-16 提交的版本,查看 最新版本

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

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址