Skip to content

Instantly share code, notes, and snippets.

@f0reachARR
Last active July 1, 2020 13:23
Show Gist options
  • Save f0reachARR/097805da16e532061015c1459a96231a to your computer and use it in GitHub Desktop.
Save f0reachARR/097805da16e532061015c1459a96231a to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name KIT Moodle utils
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://moodle.cis.kit.ac.jp/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
function toggleTopicList() {
const topics = document.querySelector('.topics');
if (!topics || topics.classList.contains('expand-topics')) return;
topics.classList.add('expand-topics');
const classId = location.search.match(/id=(\d+)/) ? Number(RegExp.$1) : -1;
const sections = topics.querySelectorAll('li.section.main');
const openedIndexes =
localStorage
.getItem(`toggle_topic_${classId}`)
?.split(',')
.filter((n) => n.length > 0)
.map((n) => Number(n)) || [];
sections.forEach((section, index) => {
section.style.paddingBottom = 0;
section.querySelector('div.left.side')?.remove();
section.querySelector('div.right.side')?.remove();
const content = section.querySelector('.content');
const sectionName = content.querySelector('.sectionname > span');
if (!sectionName) return;
const summary = document.createElement('summary');
const newSectionName = document.createElement('b');
newSectionName.append(sectionName.textContent);
summary.append(newSectionName);
sectionName.remove();
const details = document.createElement('details');
details.appendChild(summary);
details.appendChild(content.cloneNode(true));
if (
content.querySelector('.summary')?.childNodes.length === 0 &&
content.querySelector('.section')?.childNodes.length === 0
) {
summary.style.color = '#aaa';
}
if (openedIndexes.includes(index)) {
details.open = true;
}
details.addEventListener('toggle', () => {
const openedIndexes =
localStorage
.getItem(`toggle_topic_${classId}`)
?.split(',')
.filter((n) => n.length > 0)
.map((n) => Number(n)) || [];
if (openedIndexes.includes(index) && !details.open) {
localStorage.setItem(
`toggle_topic_${classId}`,
openedIndexes.filter((id) => id !== index).join(','),
);
} else if (!openedIndexes.includes(index) && details.open) {
localStorage.setItem(
`toggle_topic_${classId}`,
[...openedIndexes, index].join(','),
);
}
});
content.replaceWith(details);
});
}
if (location.href.match(/\/course\/view\.php/)) {
toggleTopicList();
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment