Lazyfoo Highlight Code

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

Pada tanggal 21 Juli 2022. Lihat %(latest_version_link).

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

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.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        Lazyfoo Highlight Code
// @namespace   lazyfoo
// @match       *://*.lazyfoo.net/tutorials/*
// @version     1.3
// @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==

const blocks = document.querySelectorAll('div.tutCode');

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

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>`;
    });

    hljs.highlightAll();
}