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

当前为 2018-10-11 提交的版本,查看 最新版本

  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.7
  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.keyCode !== 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 = document.getElementsByTagName('input');
  43. for (var i = 0, input, il = inputs.length; i < il && (input = inputs[i]); i++) {
  44. var priority = TEXT_FIELD.indexOf(' ' + input.type + ' ');
  45. if (priority < 0) continue;
  46. var n = input, style;
  47. while (n && n.style && (style = getComputedStyle(n)) && style.display !== 'none' && style.visibility !== 'hidden') {
  48. n = n.parentNode;
  49. }
  50. // visible if reached DOM root
  51. if (n && n.style) continue;
  52. // set the first OR if it's empty, try to select an identically named input field with some text (happens on some sites)
  53. if (!first || (
  54. input.value &&
  55. input.name === first.name && (
  56. !input.form &&
  57. !first.form || input.form.action === first.form.action
  58. )
  59. )) {
  60. first = input;
  61. if (first.value) break;
  62. }
  63. }
  64.  
  65. if (!first) return;
  66.  
  67. var invoke = params && params.relayedFromFrame ? passthru : onkeyup;
  68.  
  69. if (first !== document.activeElement) {
  70. rememberFocus();
  71. invoke(setFocus);
  72. } else if (previousElement) {
  73. invoke(restoreFocus);
  74. if (previousElement instanceof HTMLIFrameElement) {
  75. previousElement.contentWindow.postMessage(GM_info.script.name, '*');
  76. }
  77. }
  78. }
  79.  
  80. function rememberFocus() {
  81. previousElement = document.activeElement;
  82. scrollPos = [scrollX, scrollY];
  83. }
  84.  
  85. function setFocus() {
  86. first.focus();
  87. first.select();
  88. }
  89.  
  90. function restoreFocus() {
  91. // in case document.body (page "background") was previously selected
  92. document.activeElement.blur();
  93. previousElement.focus();
  94. scrollTo(scrollPos[0], scrollPos[1]);
  95. }
  96.  
  97. function maybeRestoreFocus(e) {
  98. if (e.data === GM_info.script.name) {
  99. restoreFocus();
  100. }
  101. }
  102.  
  103. // focusing should be done at key-up to prevent the Esc-keydown being also chain-handled by the just focused element
  104. function onkeyup(cb) {
  105. window.addEventListener('keyup', function keyup(e) {
  106. if (e.keyCode !== 27) return;
  107. window.removeEventListener('keyup', keyup);
  108. cb(e);
  109. });
  110. }
  111.  
  112. function passthru(fn) {
  113. return fn.apply(this, arguments);
  114. }

QingJ © 2025

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