Skip to content

Instantly share code, notes, and snippets.

@chhoumann
Last active February 1, 2021 08:55
Show Gist options
  • Save chhoumann/4f8a6c56838b04789060042d11e651de to your computer and use it in GitHub Desktop.
Save chhoumann/4f8a6c56838b04789060042d11e651de to your computer and use it in GitHub Desktop.
Makes the Moodle schedule bearable.
// ==UserScript==
// @name MoodleSchedule++
// @namespace https://gist.github.com/chhoumann/4f8a6c56838b04789060042d11e651de
// @version 0.1
// @description Automatically goes to semester page and removes past weeks.
// @author Christian B. B. Houmann
// @match www.moodle.aau.dk/local/planning/calendar.php?fid=*
// @grant none
// ==/UserScript==
window.addEventListener('load', function() {
ClickSemesterButton();
TogglePastWeeks(Toggle.hide);
AddShowPastWeeksButton();
}, false);
let pastWeeks;
let pastWeeksShown;
const Toggle = {
show: true,
hide: false
}
function ClickSemesterButton() {
document.querySelector(".fc-dayGridSemester-button").click();
}
function TogglePastWeeks(bToggle) {
const WEEK_CLASS = ".fc-week";
let allWeeks = document.querySelectorAll(WEEK_CLASS);
pastWeeks = FindPastWeeks(allWeeks);
pastWeeks.forEach(week => bToggle ? week.classList.remove("hidden") : week.classList.add("hidden"));
pastWeeksShown = bToggle;
}
function FindPastWeeks(allWeeks) {
const DAYS_IN_WEEK = 5;
const PAST_CLASS_NAME = "fc-past";
let pastWeeks = [];
allWeeks.forEach(week => {
// Traversing from week div -> div -> table -> tbody -> tr to get array of td
let days = week.childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes;
let pastDayCounter = 0;
days.forEach(day => {
if (day.classList.contains(PAST_CLASS_NAME)) {
pastDayCounter++;
}
});
if (pastDayCounter >= DAYS_IN_WEEK) {
pastWeeks.push(week);
}
});
return pastWeeks;
}
function AddShowPastWeeksButton() {
const fcRight = document.querySelector(".fc-right");
const MOODLE_BUTTON_CLASSES = "fc-today-button fc-button fc-button-primary";
const showPastWeeksButton = document.createElement("button");
showPastWeeksButton.classList = MOODLE_BUTTON_CLASSES;
showPastWeeksButton.innerText = "Toggle past weeks";
showPastWeeksButton.addEventListener("click", () => {
TogglePastWeeks(pastWeeksShown ? Toggle.hide : Toggle.show);
})
fcRight.appendChild(showPastWeeksButton);
}
@chhoumann
Copy link
Author

Added a toggle button to show or hide past weeks.

@chhoumann
Copy link
Author

Updated code for 1. refactoring and 2. more persistence in hiding columns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment