Scroll to empty text box

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

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

})();