Focus input text field on Esc

Focus the first visible input text field when you press Esc key, or restore the previously focused element on second press

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

  1. // ==UserScript==
  2. // @name Focus input text field on Esc
  3. // @description Focus the first visible input text field when you press Esc key, or restore the previously focused element on second press
  4. // @version 1.0.9
  5. // @include *
  6. // @author wOxxOm
  7. // @namespace wOxxOm.scripts
  8. // @license MIT License
  9. // @run-at document-start
  10. // ==/UserScript==
  11.  
  12. var TEXT_FIELD = ' search text number url ';
  13. var previousElement;
  14. var scrollPos;
  15. var first;
  16.  
  17. window.addEventListener('keydown', function (e) {
  18. if (e.defaultPrevented || e.which !== 27 || e.altKey || e.ctrlKey || e.shiftKey || e.metaKey) {
  19. return;
  20. }
  21. if (window !== top) {
  22. rememberFocus();
  23. window.addEventListener('message', maybeRestoreFocus);
  24. top.postMessage(GM_info.script.name, '*');
  25. e.preventDefault();
  26. e.stopPropagation();
  27. return;
  28. }
  29. run();
  30. }, true);
  31.  
  32. if (window === top) {
  33. window.addEventListener('message', function (e) {
  34. if (e.data === GM_info.script.name) {
  35. run({relayedFromFrame: true});
  36. }
  37. });
  38. }
  39.  
  40. function run(params) {
  41. // find text inputs inside visible DOM containers
  42. var inputs = [];
  43. populateInputs(inputs);
  44. for (var i = 0, input, il = inputs.length; i < il && (input = inputs[i]); i++) {
  45. var priority = TEXT_FIELD.indexOf(' ' + input.type + ' ');
  46. if (priority < 0) continue;
  47. var n = input, style;
  48. while (n && n.style && (style = getComputedStyle(n)) && style.display !== 'none' && style.visibility !== 'hidden') {
  49. n = n.parentNode;
  50. }
  51. // visible if reached DOM root
  52. if (n && n.style) continue;
  53. // set the first OR if it's empty, try to select an identically named input field with some text (happens on some sites)
  54. if (!first || (
  55. input.value &&
  56. input.name === first.name && (
  57. !input.form && !first.form ||
  58. input.form && first.form && input.form.action === first.form.action
  59. )
  60. )) {
  61. first = input;
  62. if (first.value) break;
  63. }
  64. }
  65.  
  66. if (!first) return;
  67.  
  68. var invoke = params && params.relayedFromFrame ? passthru : onkeyup;
  69.  
  70. if (first !== getActiveElement()) {
  71. rememberFocus();
  72. invoke(setFocus);
  73. } else if (previousElement) {
  74. invoke(restoreFocus);
  75. if (previousElement && previousElement.localName === 'iframe') {
  76. previousElement.contentWindow.postMessage(GM_info.script.name, '*');
  77. }
  78. }
  79. }
  80.  
  81. function populateInputs(inputs, root) {
  82. var walker = document.createTreeWalker(root || document, NodeFilter.SHOW_ELEMENT);
  83. var el;
  84. while ((el = walker.nextNode())) {
  85. if (el.shadowRoot)
  86. populateInputs(inputs, el.shadowRoot);
  87. if (el.localName === 'input')
  88. inputs.push(el);
  89. }
  90. }
  91.  
  92. function getActiveElement() {
  93. var el = document.activeElement;
  94. while (el) {
  95. if (!el.shadowRoot)
  96. return el;
  97. el = el.shadowRoot.activeElement;
  98. }
  99. }
  100.  
  101. function rememberFocus() {
  102. previousElement = document.activeElement;
  103. scrollPos = [scrollX, scrollY];
  104. }
  105.  
  106. function setFocus() {
  107. first.focus();
  108. first.select();
  109. }
  110.  
  111. function restoreFocus() {
  112. // in case document.body (page "background") was previously selected
  113. document.activeElement.blur();
  114. previousElement.focus();
  115. scrollTo(scrollPos[0], scrollPos[1]);
  116. }
  117.  
  118. function maybeRestoreFocus(e) {
  119. if (e.data === GM_info.script.name) {
  120. restoreFocus();
  121. }
  122. }
  123.  
  124. // focusing should be done at key-up to prevent the Esc-keydown being also chain-handled by the just focused element
  125. function onkeyup(cb) {
  126. window.addEventListener('keyup', function keyup(e) {
  127. if (e.which !== 27) return;
  128. window.removeEventListener('keyup', keyup);
  129. if (e.defaultPrevented) return;
  130. cb(e);
  131. });
  132. }
  133.  
  134. function passthru(fn) {
  135. return fn.apply(this, arguments);
  136. }

QingJ © 2025

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