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).

Stan na 28-02-2018. 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         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();
    }
  }
});