Skip to content

Instantly share code, notes, and snippets.

@motionharvest
Created January 2, 2026 21:59
Show Gist options
  • Select an option

  • Save motionharvest/7b8a9123cc0e4e875aa981ba064dec2c to your computer and use it in GitHub Desktop.

Select an option

Save motionharvest/7b8a9123cc0e4e875aa981ba064dec2c to your computer and use it in GitHub Desktop.
If a user is on a course/view page and is viewing it for the first time, arrange the UI to the ideal state
if (window.location.href.includes('course/view.php')) {
const courseKey = `${window.location.pathname}${window.location.search}`;
const lsKey = `courseViewAutoClicks::${courseKey}`;
if (!localStorage.getItem(lsKey)) {
const clickIfExists = (selector) => {
const el = document.querySelector(selector);
if (el) el.click();
return !!el;
};
// collapse all course index links
clickIfExists('[data-action="collapseallcourseindexsections"]');
// collapse all sections
const hasCollapseSections = clickIfExists('#collapsesections');
// only open the course index sidebar if it isn't already open
const courseIndexDrawer = document.querySelector('#theme_boost-drawers-courseindex');
if (!courseIndexDrawer || !courseIndexDrawer.classList.contains('show')) {
clickIfExists('.drawer-toggler button');
}
// open first section after 500ms ONLY if #collapsesections existed
if (hasCollapseSections) {
setTimeout(() => {
clickIfExists('#section-0>div a');
}, 500);
}
// mark as done for this specific course URL
localStorage.setItem(lsKey, '1');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment