Works with the CSS to add interactive features to the PX Help Centre, including Dark mode with saved preferences, automatic theme detection, topic printing, customised footer information and preservation of original text colours.

This JavaScript file controls the interactive features of the PX Help Centre. It:

- Adds version information, copyright details and a clickable link to the latest documentation.

- Adds a Dark mode switch and saves the user's preference.

- Automatically detects the browser or Windows light/dark setting.

- Changes the PurpleXtra logo to match the selected theme.

- Preserves original text and link colours in Dark mode.

- Adds a Print topic button.

- Automatically switches to Light mode for printing and restores Dark mode afterwards.

- Maintains custom footer information and text colours when navigating between topics.


Click on Home > Generate help > Build html documentation menu icons, then copy and paste the following JavaScript code into the appropriate location called Custom JavaScript field. Refer to the screenshot below for an example:



// =======================================

// Change Log for Version 1.0.1

//

// 12/10/26 - Changed footer URL to blue text to match doc convention, underlined and clickable

//

//

//========================================



// ========================================

// PX Help Centre - Footer Information

// ========================================


function insertCopyright() {


    const footerContent =

        document.querySelector("#topic_footer_content");


    // Ensure footer exists and hasn't already been added

    if (

        footerContent &&

        !footerContent.querySelector(".copyright")

    ) {


        // Version information

        const versionElement =

            document.createElement("p");


        versionElement.className =

            "version-info";


        versionElement.textContent =

            "PX Help Centre, Version: 0.144";



        // Copyright

        const copyrightElement =

            document.createElement("p");


        copyrightElement.className =

            "copyright";


        copyrightElement.textContent =

            "Copyright © PurpleXtra Limited 2026. All Rights Reserved.";



        // Update notice

        const updateNoticeElement =

            document.createElement("p");


        updateNoticeElement.className =

            "update-notice";


        updateNoticeElement.innerHTML =

            'This document is subject to ongoing updates. ' +

            'For the latest version, visit the ' +

            '<a href="https://onlinedata.purplextra.com/PX_Help_Centre" ' +

            'target="_blank" style="color: rgb(113, 182, 251); text-decoration: underline;">' +

            'https://onlinedata.purplextra.com/PX_Help_Centre' +

            '</a>';



        // Add footer elements

        footerContent.appendChild(

            versionElement

        );


        footerContent.appendChild(

            copyrightElement

        );


        footerContent.appendChild(

            updateNoticeElement

        );

    }

}



// ========================================

// PX Help Centre - Light / Dark Theme

// ========================================


const PX_THEME_KEY =

    "px-help-theme";



/*

 * Standard HelpNDoc logo.

 */

const PX_LIGHT_LOGO =

    "_logo.png";



/*

 * Dark-mode logo.

 *

 * This must exist in the generated HTML

 * folder alongside index.html.

 */

const PX_DARK_LOGO =

    "_logo_dark.png";



/*

 * Find HelpNDoc's theme stylesheet.

 */

function getHelpNDocThemeLink() {


    return document.querySelector(

        'link[href*="theme-light-blue.min.css"], ' +

        'link[href*="theme-dark-blue.min.css"]'

    );

}



/*

 * Switch the PurpleXtra logo.

 */

function setPXLogo(darkMode) {


    const logo =

        document.querySelector(

            "header img.logo"

        );


    if (!logo) {

        return;

    }


    logo.src =

        darkMode

            ? PX_DARK_LOGO

            : PX_LIGHT_LOGO;

}





/*

 * Preserve coloured text used in HelpNDoc topics.

 *

 * HelpNDoc stores topic formatting in generated rvts classes.

 * Our dark-mode CSS treats black, grey and slightly tinted default

 * HelpNDoc text as normal text and displays it as white. Only clearly

 * authored colours are preserved.

 */

function captureOriginalTopicColours() {


    const topic =

        document.querySelector(

            "#topic-content .main-content"

        );


    if (!topic) {

        return;

    }



    /*

     * Read colours from the normal light appearance.

     */

    const wasDark =

        document.documentElement.classList.contains(

            "dark-mode"

        );


    if (wasDark) {

        document.documentElement.classList.remove(

            "dark-mode"

        );

    }



    /*

     * Remove any previous classification first.

     * This prevents an element that was incorrectly marked on an

     * earlier pass from remaining permanently "coloured".

     */

    topic.querySelectorAll(

        ".px-preserve-colour"

    ).forEach(

        function (element) {


            element.classList.remove(

                "px-preserve-colour"

            );


            element.style.removeProperty(

                "--px-original-colour"

            );

        }

    );



    const topicColour =

        window.getComputedStyle(

            topic

        ).color;



    function rgbFromColour(colour) {


        const match =

            colour.match(

                /rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/i

            );


        if (!match) {

            return null;

        }


        return {

            red: parseInt(match[1], 10),

            green: parseInt(match[2], 10),

            blue: parseInt(match[3], 10)

        };

    }



    const topicRGB =

        rgbFromColour(

            topicColour

        );



    const elements =

        topic.querySelectorAll(

            "a, span, p, li, td, th, h1, h2, h3, h4, h5, h6"

        );



    elements.forEach(

        function (element) {


            const colour =

                window.getComputedStyle(

                    element

                ).color;


            const rgb =

                rgbFromColour(

                    colour

                );


            if (!rgb) {

                return;

            }



            const maximum =

                Math.max(

                    rgb.red,

                    rgb.green,

                    rgb.blue

                );


            const minimum =

                Math.min(

                    rgb.red,

                    rgb.green,

                    rgb.blue

                );


            const chroma =

                maximum - minimum;


            const saturation =

                maximum === 0

                    ? 0

                    : chroma / maximum;



            /*

             * Compare the element colour with the topic's normal

             * light-mode text colour.

             */

            let distanceFromDefault =

                0;


            if (topicRGB) {


                distanceFromDefault =

                    Math.sqrt(

                        Math.pow(

                            rgb.red - topicRGB.red,

                            2

                        ) +

                        Math.pow(

                            rgb.green - topicRGB.green,

                            2

                        ) +

                        Math.pow(

                            rgb.blue - topicRGB.blue,

                            2

                        )

                    );

            }



            const isLink =

                element.tagName.toLowerCase() ===

                "a";



            /*

             * Important:

             *

             * HelpNDoc's ordinary text may be black, dark grey or

             * slightly blue-grey. We treat all of that as NORMAL

             * text, so it becomes white in dark mode.

             *

             * Only clearly chromatic text that is significantly

             * different from the topic's normal text colour is

             * preserved.

             */

            const isClearlyColoured =

                chroma >= 35 &&

                saturation >= 0.25 &&

                distanceFromDefault >= 35;



            /*

             * Links retain their light-mode link colour.

             * Other text is preserved only when it is clearly

             * an authored colour.

             */

            const isColoured =

                isLink ||

                isClearlyColoured;



            if (isColoured) {


                element.style.setProperty(

                    "--px-original-colour",

                    colour

                );


                element.classList.add(

                    "px-preserve-colour"

                );

            }

        }

    );



    if (wasDark) {

        document.documentElement.classList.add(

            "dark-mode"

        );

    }

}



/*

 * Apply Light or Dark mode.

 */

function setPXTheme(darkMode) {


    // Preserve authored topic colours before dark mode is applied

    if (darkMode) {

        captureOriginalTopicColours();

    }



    // ====================================

    // Right-hand Topic Area

    // ====================================


    document.documentElement.classList.toggle(

        "dark-mode",

        darkMode

    );



    // ====================================

    // PurpleXtra Logo

    // ====================================


    setPXLogo(

        darkMode

    );



    // ====================================

    // HelpNDoc Theme

    // ====================================


    /*

     * HelpNDoc's own theme controls:

     *

     * - Top header

     * - Contents tree

     * - Index

     * - Search

     * - Left navigation panel

     */


    const themeLink =

        getHelpNDocThemeLink();



    if (themeLink) {


        let href =

            themeLink.getAttribute(

                "href"

            );



        if (darkMode) {


            href = href.replace(

                "theme-light-blue.min.css",

                "theme-dark-blue.min.css"

            );


        } else {


            href = href.replace(

                "theme-dark-blue.min.css",

                "theme-light-blue.min.css"

            );

        }



        themeLink.setAttribute(

            "href",

            href

        );

    }



    // ====================================

    // Update Dark Mode Switch

    // ====================================


    const darkModeSwitch =

        document.getElementById(

            "px-dark-mode"

        );



    if (darkModeSwitch) {


        darkModeSwitch.checked =

            darkMode;


        darkModeSwitch.setAttribute(

            "aria-checked",

            darkMode ? "true" : "false"

        );


        darkModeSwitch.title =

            darkMode

                ? "Switch dark mode off"

                : "Switch dark mode on";

    }


}



/*

 * Get the user's saved preference.

 */

function getInitialDarkMode() {


    let savedTheme =

        null;



    try {


        savedTheme =

            localStorage.getItem(

                PX_THEME_KEY

            );


    } catch (error) {


        /*

         * Continue if localStorage

         * isn't available.

         */

    }



    // Previously selected Dark mode

    if (savedTheme === "dark") {

        return true;

    }



    // Previously selected Light mode

    if (savedTheme === "light") {

        return false;

    }



    /*

     * No saved preference.

     *

     * Follow Windows/browser setting.

     */

    return (

        window.matchMedia &&

        window.matchMedia(

            "(prefers-color-scheme: dark)"

        ).matches

    );

}



/*

 * Create a Windows 11-style Dark mode switch

 * immediately to the left of the logo.

 */

function insertDarkModeButton() {


    // Prevent duplicate controls

    if (

        document.getElementById(

            "px-dark-mode-control"

        )

    ) {

        return;

    }



    const header =

        document.querySelector(

            "header"

        );


    const logo =

        document.querySelector(

            "header img.logo"

        );



    if (!header || !logo) {

        return;

    }



    // ====================================

    // Right-hand Header Wrapper

    // ====================================


    const headerRight =

        document.createElement(

            "div"

        );


    headerRight.id =

        "px-header-right";



    /*

     * Insert the wrapper at the existing

     * logo position, then move the logo

     * inside it.

     */

    logo.parentNode.insertBefore(

        headerRight,

        logo

    );



    // ====================================

    // Dark Mode Control

    // ====================================


    const control =

        document.createElement(

            "div"

        );


    control.id =

        "px-dark-mode-control";



    // Text next to the switch

    const textLabel =

        document.createElement(

            "span"

        );


    textLabel.className =

        "px-theme-label";


    textLabel.textContent =

        "Dark mode";



    // Switch container

    const switchLabel =

        document.createElement(

            "label"

        );


    switchLabel.className =

        "px-switch";


    switchLabel.setAttribute(

        "for",

        "px-dark-mode"

    );



    // Accessible checkbox

    const darkModeSwitch =

        document.createElement(

            "input"

        );


    darkModeSwitch.id =

        "px-dark-mode";


    darkModeSwitch.type =

        "checkbox";


    darkModeSwitch.setAttribute(

        "role",

        "switch"

    );


    darkModeSwitch.setAttribute(

        "aria-label",

        "Dark mode"

    );



    // Visible switch slider

    const slider =

        document.createElement(

            "span"

        );


    slider.className =

        "px-switch-slider";


    slider.setAttribute(

        "aria-hidden",

        "true"

    );



    switchLabel.appendChild(

        darkModeSwitch

    );


    switchLabel.appendChild(

        slider

    );



    control.appendChild(

        textLabel

    );


    control.appendChild(

        switchLabel

    );



    /*

     * Header order:

     * Dark mode [switch]   [logo]

     */

    headerRight.appendChild(

        control

    );


    headerRight.appendChild(

        logo

    );



    // Apply initial theme

    setPXTheme(

        getInitialDarkMode()

    );



    // ====================================

    // Theme Switch

    // ====================================


    darkModeSwitch.addEventListener(

        "change",

        function () {


            const darkMode =

                darkModeSwitch.checked;



            // Save the user's preference

            try {


                localStorage.setItem(

                    PX_THEME_KEY,

                    darkMode

                        ? "dark"

                        : "light"

                );


            } catch (error) {


                /*

                 * Continue if localStorage

                 * isn't available.

                 */

            }



            // Apply selected theme

            setPXTheme(

                darkMode

            );

        }

    );

}





/*

 * Create a Print topic control immediately

 * before the Dark mode control.

 */

function insertPrintButton() {


    // Prevent duplicate print controls

    if (

        document.getElementById(

            "px-print-button"

        )

    ) {

        return;

    }



    const headerRight =

        document.getElementById(

            "px-header-right"

        );


    const darkModeControl =

        document.getElementById(

            "px-dark-mode-control"

        );



    if (!headerRight || !darkModeControl) {

        return;

    }



    const printButton =

        document.createElement(

            "button"

        );



    printButton.id =

        "px-print-button";


    printButton.type =

        "button";


    printButton.title =

        "Print topic";


    printButton.setAttribute(

        "aria-label",

        "Print topic"

    );



    // ------------------------------------

    // Print topic text

    // ------------------------------------


    const printLabel =

        document.createElement(

            "span"

        );


    printLabel.className =

        "px-print-label";


    printLabel.textContent =

        "Print topic";



    // ------------------------------------

    // Printer icon

    // ------------------------------------


    const printIcon =

        document.createElement(

            "span"

        );


    printIcon.className =

        "bi bi-printer";


    printIcon.setAttribute(

        "aria-hidden",

        "true"

    );



    printButton.appendChild(

        printLabel

    );


    printButton.appendChild(

        printIcon

    );



    /*

     * Put Print topic before Dark mode:

     *

     * Print topic [printer]   Dark mode [switch]   [logo]

     */

    headerRight.insertBefore(

        printButton,

        darkModeControl

    );



    // Open the browser's standard print dialogue

    printButton.addEventListener(

        "click",

        function (event) {


            event.preventDefault();

            event.stopPropagation();


            window.print();

        }

    );

}



// ========================================

// Initialise PX Help Centre

// ========================================


function initialisePXHelpCentre() {


    // Add custom footer

    insertCopyright();



    // Record any original coloured topic text before applying dark mode

    captureOriginalTopicColours();



    // Add Light / Dark switch

    insertDarkModeButton();



    // Add print icon next to the title

    insertPrintButton();



    /*

     * HelpNDoc dynamically replaces topic

     * content when navigating.

     *

     * This observer restores your custom footer and records

     * any authored text colours in newly loaded topics.

     *

     * It deliberately does NOT reapply or switch the theme.

     */

    const observer =

        new MutationObserver(

            function () {


                insertCopyright();

                captureOriginalTopicColours();


            }

        );



    observer.observe(

        document.body,

        {

            childList: true,

            subtree: true

        }

    );

}




// ========================================

// Printing - Always Use Light Mode

// ========================================


let pxThemeWasDarkBeforePrint = false;


window.addEventListener(

    "beforeprint",

    function () {


        pxThemeWasDarkBeforePrint =

            document.documentElement

                .classList

                .contains(

                    "dark-mode"

                );


        if (pxThemeWasDarkBeforePrint) {

            setPXTheme(false);

        }

    }

);


window.addEventListener(

    "afterprint",

    function () {


        if (pxThemeWasDarkBeforePrint) {

            setPXTheme(true);

        }


        pxThemeWasDarkBeforePrint = false;

    }

);



// ========================================

// Start

// ========================================


if (

    document.readyState === "loading"

) {


    document.addEventListener(

        "DOMContentLoaded",

        initialisePXHelpCentre

    );


} else {


    initialisePXHelpCentre();

}