Link & Form Field Navigation Shortcuts

Adds keyboard shortcuts for focusing: previous form field (ALT+1), next form field (ALT+2), previous link (ALT+3), and next link (ALT+4).

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Link & Form Field Navigation Shortcuts
// @namespace    http://tampermonkey.net/
// @version      1.0.1
// @license      AGPL v3
// @description  Adds keyboard shortcuts for focusing: previous form field (ALT+1), next form field (ALT+2), previous link (ALT+3), and next link (ALT+4).
// @author       jcunews
// @match        *://*/*
// @grant        none
// ==/UserScript==

function isElementFocusable(ele, css) {
  while (ele && (ele !== document)) {
    if (!ele.offsetWidth || !ele.offsetHeight || ((css = getComputedStyle(ele)) && (css.display === "none"))) return false;
    ele = ele.parentNode;
  }
  return true;
}

addEventListener("keypress", function(ev, eles, i) {
  if ((["1", "2"].indexOf(ev.key) >= 0) && !ev.location && ev.altKey && !ev.ctrlKey) {
    eles = Array.prototype.slice.call(document.querySelectorAll("input, select, textarea"));
    if (document.activeElement && (["INPUT", "SELECT", "TEXTAREA"].indexOf(document.activeElement.tagName) >= 0)) {
      eles.some(function(ele, i) {
        if (ele === document.activeElement) {
          ele = ev.key === "1" ? eles[i - 1] || eles[eles.length - 1] : eles[i + 1] || eles[0];
          while (ele) {
            if (isElementFocusable(ele)) {
              ele.focus();
              ele.scrollIntoView(false);
              return true;
            }
            i += ev.key === "1" ? -1 : 1;
            ele = eles[i];
          }
        }
      });
    } else if (ele = eles[ev.key === "1" ? eles.length - 1 : 0]) {
      ele.scrollIntoView();
      ele.focus();
    }
  } else if ((["3", "4"].indexOf(ev.key) >= 0) && !ev.location && ev.altKey && !ev.ctrlKey) {
    eles = Array.prototype.slice.call(document.querySelectorAll("a"));
    if (document.activeElement && (document.activeElement.tagName === "A")) {
      eles.some(function(ele, i) {
        if (ele === document.activeElement) {
          ele = ev.key === "3" ? eles[i - 1] || eles[eles.length - 1] : eles[i + 1] || eles[0];
          while (ele) {
            if (ele.href && isElementFocusable(ele)) {
              ele.focus();
              ele.scrollIntoView(false);
              return true;
            }
            i += ev.key === "1" ? -1 : 1;
            ele = eles[i];
          }
        }
      });
    } else if (ele = eles[ev.key === "3" ? eles.length - 1 : 0]) {
      ele.scrollIntoView();
      ele.focus();
    }
  }
});