User:Joris Darlington Quarshie/Userscript/Chapter Progress Tracker
Note: After saving, you have to Bypass your cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
Documentation
Documentation for this user script can be found at User:Joris Darlington Quarshie/Userscript/Chapter Progress Tracker/Documentation
// ==UserScript==
// @name Chapter Progress Tracker
// @namespace
// @version 1.0
// @description Tracks user progress through chapters on Wikibooks.
// @author Joris Darlington Quarshie
// @match https://en.wikibooks.org/wiki/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Add a "Mark Section Complete" button next to each section heading.
const sectionHeadings = document.querySelectorAll('.mw-headline');
for (const heading of sectionHeadings) {
const button = document.createElement('button');
button.textContent = 'Mark Section Complete';
button.addEventListener('click', function() {
markSectionCompleted(heading);
});
heading.parentNode.insertBefore(button, heading.nextSibling);
}
// Function to mark a section as completed and update progress.
function markSectionCompleted(heading) {
heading.classList.add('completed');
const chapterContent = document.querySelector('.mw-parser-output');
const totalSections = chapterContent.querySelectorAll('.mw-headline').length;
const completedSections = chapterContent.querySelectorAll('.mw-headline.completed').length;
const progressElement = document.createElement('p');
progressElement.textContent = `Chapter Progress: ${completedSections}/${totalSections}`;
chapterContent.appendChild(progressElement);
}
// Load previously completed sections from local storage.
const completedSections = JSON.parse(localStorage.getItem('wikibooks-completed-sections'));
if (completedSections) {
for (const sectionId of completedSections) {
const sectionElement = document.getElementById(sectionId);
if (sectionElement) {
markSectionCompleted(sectionElement);
}
}
}
// Save completed sections to local storage on page unload.
window.addEventListener('unload', function() {
const completedSections = [];
for (const heading of document.querySelectorAll('.mw-headline.completed')) {
completedSections.push(heading.id);
}
localStorage.setItem('wikibooks-completed-sections', JSON.stringify(completedSections));
});
})();