Autofocus input text field

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

Stan na 27-04-2015. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name          Autofocus input text field
// @description   Autofocus the first visible input text field when a page is loaded
// @version       1.0.1
// @author        wOxxOm
// @namespace     wOxxOm.scripts
// @license       MIT License
// @run-at        document-start
// ==/UserScript==

var TEXT_FIELD = ' text number search url ';

document.addEventListener('DOMContentLoaded', function() {
  if (TEXT_FIELD.indexOf(document.activeElement.type) >= 0)
    return;
  // find text inputs inside visible DOM containers
  var inputs = document.getElementsByTagName('input');
  var visible = [];
  for (var i=0, input, il=inputs.length; i<il && (input=inputs[i]); i++)
    if (TEXT_FIELD.indexOf(' '+input.type+' ') >= 0) {
      var n=input, style;
      while (n && n.style && (style=getComputedStyle(n)) && style.display!='none' && style.visibility!='hidden')
        n = n.parentNode;
      if (!n || !n.style)
        visible.push(input);
    }
  if (visible.length) {
    var toFocus = visible[0];
    // if empty, try to select an identically named input field with some text (happens on some sites)
    if (!toFocus.value)
      for (var i in visible)
        if (visible[i].value && visible[i].name == toFocus.name
            && (!visible[i].form && !toFocus.form || visible[i].form.action == toFocus.form.action)) {
          toFocus = visible[i];
          break;
        }
    toFocus.focus();
    toFocus.select();
  }
});