Scroll to empty text box

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

Från och med 2018-10-14. Se den senaste versionen.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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();
      }
    }
  });

})();