Scroll to empty text box

Press F2 to scroll to an empty text box and focus it

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         Scroll to empty text box
// @namespace    http://tampermonkey.net/
// @version      0.2.0
// @description  Press F2 to scroll to an empty text box and focus it
// @author       Valacar
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
  "use strict";

  // custom key. note: anything besides function keys may not work.
  // key code list: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code#Code_values
  const config = {
    key: "F2"
  };

  function findEmptyInput() {
    const inputs =
          document.querySelectorAll('textarea:not([readonly]),input[type="text"]:not([readonly])');
    for (let input of inputs) {
      if (input.value === "") {
        return input;
      }
    }
  }

  addEventListener("keydown", e => {
    if (e.defaultPrevented) {
      return;
    }
    if (e.code === config.key) {
      const emptyInput = findEmptyInput();
      if (emptyInput) {
        //console.debug(emptyInput);
        emptyInput.scrollIntoView({
          behavior: "smooth",
          block: "center",
          inline: "nearest"
        });
        emptyInput.focus();
      }
    }
  });

})();