Autofocus input text field

Autofocus the first visible input text field when a page is loaded

  1. // ==UserScript==
  2. // @name Autofocus input text field
  3. // @description Autofocus the first visible input text field when a page is loaded
  4. // @version 1.0.5
  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 = ' text number search url ';
  13.  
  14. document.addEventListener('DOMContentLoaded', function() {
  15. if (TEXT_FIELD.indexOf(document.activeElement.type) >= 0)
  16. return;
  17. // find text inputs inside visible DOM containers
  18. var inputs = document.getElementsByTagName('input');
  19. var first;
  20. for (var i=0, input, il=inputs.length; i<il && (input=inputs[i]); i++)
  21. if (TEXT_FIELD.indexOf(' '+input.type+' ') >= 0) {
  22. var n=input, style;
  23. while (n && n.style && (style=getComputedStyle(n)) && style.display!='none' && style.visibility!='hidden')
  24. n = n.parentNode;
  25. if (!n || !n.style) {
  26. if (!first // set the first OR if it's empty, try to select an identically named input field with some text (happens on some sites)
  27. || (input.value && input.name == first.name && (!input.form && !first.form || input.form.action == first.form.action))) {
  28. first = input;
  29. if (first.value)
  30. break;
  31. }
  32. }
  33. }
  34. if (first) first.focus();
  35. });

QingJ © 2025

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