Moodle: Jump to Current Section

Userscript for Moodle sites that lets you quickly jump to the current week/section. Made for the Boost and Fordson themes.

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 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         Moodle: Jump to Current Section
// @version      0.1
// @description  Userscript for Moodle sites that lets you quickly jump to the current week/section. Made for the Boost and Fordson themes.
// @author       dotcomboom
// @match        *://*/course/view.php?id=*
// @grant        none
// @namespace https://greasyfork.org/en/users/691054-dotcomboom
// ==/UserScript==

(function() {
    'use strict';

    // config
    const jump_on_page_load = true; // whether to jump when page first loads
    const navbar = true; // whether to add button to top navbar
    //-----------

    const c = document.getElementsByClassName("current")[1];
    // index 0 is your user avatar, index 1 is the highlighted/current section if the course has one

    if (c) { // check if there is one
        if (jump_on_page_load) {
            c.scrollIntoView();
        }

        if (navbar) {
            const btn = document.createElement("li");
            const link = document.createElement("a");
            const ltext = document.createTextNode("Current Section");

            btn.classList.add("nav-item");
            link.classList.add("nav-link");
            link.href = "javascript:void(0)";
            link.addEventListener("click", () => { c.scrollIntoView() });

            link.appendChild(ltext);
            btn.appendChild(link);
            document.querySelector(".navbar-nav").appendChild(btn);
        }
    }
})();