w3schools - Navigate to Next/Previous section using arrow keys!

Use the arrow keys to quickly navigate to the next or previous sections of w3schools.com tutorials ;)

Versione datata 05/03/2025. Vedi la nuova versione l'ultima versione.

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

You will need to install an extension such as Tampermonkey to install this 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        w3schools - Navigate to Next/Previous section using arrow keys!
// @namespace   Violentmonkey Scripts
// @match       https://www.w3schools.com/*/*
// @grant       none
// @version     1.2
// @author      AvinashReddy3108 (assisted by ChatGPT)
// @license     WTFPL
// @description Use the arrow keys to quickly navigate to the next or previous sections of w3schools.com tutorials ;)
// ==/UserScript==

(function () {
    'use strict';

    document.addEventListener('keydown', function (event) {
        // We don't want to bother you when you're filling a form or typing a large text
        if (event.target.tagName.toLowerCase() === 'input' || event.target.tagName.toLowerCase() === 'textarea') {
            return;
        }

        let container = document.querySelector("div.w3-clear.nextprev");
        if (!container) {
            return;
        }

        let prevButton = container.querySelector("a.w3-left.w3-btn");
        let nextButton = container.querySelector("a.w3-right.w3-btn");

        if (event.key === 'ArrowLeft' && prevButton) {
            // Prevent going away to the home page when pressing the left key at the first section.
            if (prevButton.textContent.trim() !== "❮ Home") {
                window.location.href = prevButton.href;
            }
        } else if (event.key === 'ArrowRight' && nextButton) {
            // We don't want to accidentally go into any external sites using the sneaky "Next" button.
            // ex: Certification exams, etc..
            let currentHost = window.location.hostname;
            let nextHost = new URL(nextButton.href, window.location.href).hostname;

            if (currentHost === nextHost) {
                window.location.href = nextButton.href;
            }
        }
    });
})();