// ==UserScript==
// @name Udemy - show total section time
// @namespace http://tampermonkey.net/
// @version 0.3
// @description For Udemy, adds total time to each section
// @copyright 2017, Pedro Mass (https://github.com/pedro-mass)
// @author pedro-mass
// @match *.udemy.com/*
// @grant none
// @require http://code.jquery.com/jquery-3.2.0.min.js
// @require https://greasyfork.org/scripts/5392-waitforkeyelements/code/WaitForKeyElements.js?version=115012
// @run-at document-idle
// @downloadURL none
// ==/UserScript==
(function() {
// ----- todo
// - split up for hours
var selectors = {
sectionCard: 'curriculum-navigation-section',
lectureTime: '.lecture__item__link__time',
lectureName: '.curriculum-navigation__section__title',
lectureStatus: '.cur-status'
};
// waits for the cards to be loaded
waitForKeyElements(selectors.sectionCard, run);
function run() {
var sections = $(selectors.sectionCard);
$.each(sections, function(index, section) {
// get the section title
var title = $(section).find(selectors.lectureName).text();
// get the times
var timeSpans = $(section).find(selectors.lectureTime);
var timeTexts = convertTimeSpansToTexts(timeSpans);
// sum the times
var totalTime = sumTextTimes(timeTexts);
// log it
console.log(title + ': ' + totalTime);
// prepend to lecture status
var totalTimeSpan = $(section).find(selectors.lectureStatus).find('.section-totalTime');
// check to see if we've already added the time to the DOM
if (totalTimeSpan.length > 0) {
$(totalTimeSpan[0]).text(totalTime);
} else {
// we haven't, so create the element and add it
$(section).find(selectors.lectureStatus).prepend(''+ totalTime + '');
}
});
}
function convertTimeSpansToTexts(timeSpans) {
var timeTexts = [];
for (var i=0; i 0) {
result += hours + ':';
result += timePad(minutes);
} else {
result += minutes;
}
if (minutes > 0) {
result += ':' + timePad(seconds);
} else {
result += '0:' + timePad(seconds);
}
return result;
}
function timePad(timeSegment) {
var result = timeSegment + '';
while (result.length < 2) {
result = '0' + result;
}
return result;
}
})();