Greasy Fork

Greasy Fork is available in English.

Udemy - show total section time

For Udemy, adds total time to each section

当前为 2017-03-20 提交的版本,查看 最新版本

// ==UserScript==
// @name         Udemy - show total section time
// @namespace    http://tampermonkey.net/
// @version      0.2
// @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      http://greasyfork.icu/scripts/5392-waitforkeyelements/code/WaitForKeyElements.js?version=115012
// @run-at       document-idle
// @locale       English
// ==/UserScript==



(function() {
  // ----- Steps
  // 1. get all the times in a card
  // 2. sum up those times
  // 3. display the sum
  // 4. Do this for all cards
  // 5. put it my local TamperMonkey
  // 6. search to see if anyone else has already solved this
  //    if they have -> compare solutions
  //    if NOT -> post solution

  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('<span class="section-totalTime" style="margin-right:1em">'+ totalTime + '</span>');
      }
    });
  }

  function convertTimeSpansToTexts(timeSpans) {
    var timeTexts = [];

    for (var i=0; i<timeSpans.length; i++) {
      timeTexts.push($(timeSpans[i]).text());
    }

    return timeTexts;
  }

  function convertTextTime(textTime) {
    var timeParts = textTime.split(':');

    var minutes = parseInt(timeParts[1]);

    minutes += parseInt(timeParts[0]) * 60;

    return minutes;
  }

  function sumTextTimes(textTimes) {
    var totalTime = 0;

    // get total minutes
    $.each(textTimes, function(index, textTime) {
      totalTime += convertTextTime(textTime);
    });

    // convert back to hh:mm
    var hour = Math.floor(totalTime / 60);
    var minutes = totalTime % 60;

    return hour + ':' + minutes;
  }
})();