Sphinx RTD template: toggle sidebar and highlighting

Toggle sidebar (Esc-H) and search keyword highlighting (Esc-U) on Sphinx HTML pages using the Read the Docs template.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         Sphinx RTD template: toggle sidebar and highlighting
// @namespace    club.porcupine.gm_scripts.sphinx_rtd_template_toggle_sidebar_and_highlighting
// @version      3
// @description  Toggle sidebar (Esc-H) and search keyword highlighting (Esc-U) on Sphinx HTML pages using the Read the Docs template.
// @author       Sam Birch
// @license      MIT
// @icon         https://icons.duckduckgo.com/ip2/readthedocs.io.ico
// @match        *://*/*
// @grant        none
// @noframes
// ==/UserScript==
(function() {
    'use strict'

    function initialize() {
        function gen_style_toggler(source) {
            const stylesheet = document.createElement('style')
            stylesheet.innerHTML = source
            function toggle() {
                if (stylesheet.parentElement) {
                    stylesheet.parentElement.removeChild(stylesheet)
                } else {
                    document.head.appendChild(stylesheet)
                }
            }
            return toggle
        }

        const toggle_search_highlighting = gen_style_toggler(`
            .highlighted { background-color: unset !important; }
        `)
        const toggle_sidebar_visibility = gen_style_toggler(`
            .wy-nav-side, .rst-versions { display: none }
            .wy-nav-content-wrap { margin-left: unset }
        `)

        const esc_timeout_ms = 1000
        let esc_pressed = 0
        window.addEventListener("keydown", ev => {
            if (ev.defaultPrevented) {
                return
            }
            switch (ev.key) {
                case "Escape":
                    esc_pressed = Date.now()
                    ev.preventDefault()
                    break
                case "h":
                    if (esc_pressed && Date.now() - esc_pressed < esc_timeout_ms) {
                        esc_pressed = 0
                        toggle_sidebar_visibility()
                        ev.preventDefault()
                    }
                    break
                case "u":
                    if (esc_pressed && Date.now() - esc_pressed < esc_timeout_ms) {
                        esc_pressed = 0
                        toggle_search_highlighting()
                        ev.preventDefault()
                    }
                    break
            }
        })
    }

    if (document.body.classList.contains('wy-body-for-nav')) {
        initialize()
    }
}())