Skip to content

Instantly share code, notes, and snippets.

@zevaverbach
Last active November 20, 2023 06:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zevaverbach/2f8639ccb2c5c58ce707027cc3657b0a to your computer and use it in GitHub Desktop.
Save zevaverbach/2f8639ccb2c5c58ce707027cc3657b0a to your computer and use it in GitHub Desktop.
Get remaining time in a Teachable course
// I created this for (and tested it on) a learn.cantrill.io course
// to quickly determine how much more video I have left to finish the course,
// so I could plan my time in advance of an AWS exam.
function getTotalMinutesLeftInCourse() {
let totalMinutes = 0;
for (const chapter of getChapters()) {
if (!chapterIsComplete(chapter)) {
const [minutes, seconds] = getMinutesAndSeconds(getLengthString(chapter.innerText))
totalMinutes += (minutes + (seconds / 60))
}
}
return totalMinutes
}
function getChapters() {
return document.querySelectorAll(".title-container");
}
function chapterIsComplete(chapter) {
return chapter.previousElementSibling.getAttribute("aria-label") === "Completed item"
}
function getLengthString(string) {
const lengthString = string.match(/\d:\d\d/)
if (lengthString == null) {
// this only happens with quizzes, which is accurate
console.log(string, 'no match for length');
return "0:00"
}
return lengthString[0];
}
function getMinutesAndSeconds(lengthString) {
let [minutes, seconds] = lengthString.split(":")
return [parseInt(minutes), parseInt(seconds)]
}
function getChapterThisManyHoursFromWhereIAmCurrently(hours) {
let totalMinutes = 0;
for (const chapter of getChapters()) {
if (!chapterIsComplete(chapter)) {
const [minutes, seconds] = getMinutesAndSeconds(getLengthString(chapter.innerText))
totalMinutes += (minutes + (seconds / 60))
if (totalMinutes >= (hours * 60)) {
return [totalMinutes, chapter.innerText]
}
}
}
}
function howManyHoursUntilChapter(chapterNameSubstring) {
let totalMinutes = 0;
for (const chapter of getChapters()) {
if (!chapterIsComplete(chapter)) {
const chapterText = chapter.innerText;
if (chapterText.includes(chapterNameSubstring)) {
return totalMinutes / 60;
}
const [minutes, seconds] = getMinutesAndSeconds(getLengthString(chapterText));
totalMinutes += (minutes + (seconds / 60));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment