Lazyfoo Highlight Code

Add code highlight to LazyFoo's code snippets in the tutorials section, updated to HLJS 11.6, no jQuery, switchable theme.

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        Lazyfoo Highlight Code
// @namespace   codesthings.com
// @match       *://*.lazyfoo.net/tutorials/*
// @version     1.5
// @license MIT
// @require     https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/highlight.min.js
// @require     https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/languages/cpp.min.js
// @resource    THEME_DEFAULT https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/styles/default.min.css
// @resource    THEME_DARK https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/styles/a11y-dark.min.css
// @grant       GM_getResourceText
// @grant       GM_addStyle
// @description Add code highlight to LazyFoo's code snippets in the tutorials section, updated to HLJS 11.6, no jQuery, switchable theme.
// ==/UserScript==


// Highlight JS
var hljs = hljs || null;

(function() {
    'use strict';
    const blocks = document.querySelectorAll('div.tutCode');

    if (blocks.length) {
        if (typeof GM_getResourceText === 'function') {
            // Change THEME_DARK to THEME_DEFAULT here for light theme.
            const themeCss = GM_getResourceText("THEME_DARK");
            GM_addStyle(themeCss);
        } else {
            document.head.innerHTML += `<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/styles/a11y-dark.min.css' />`;
        }

        blocks.forEach((block) => {
            block.innerHTML = `<pre><code class="cpp">${block.innerHTML}</code></pre>`;
        });

        if (hljs && typeof hljs.highlightAll === 'function') {
            hljs.highlightAll();
        }
    }
})();