stuart.crouch / Pluralsite

// ==UserScript==
// @name         Pluralsite
// @version      2.15
// @description  Downloads a copy of the current pluralsight video. As each video starts playing it will download it
// @author       Stuart Crouch
// @match        *://*.pluralsight.com/player?course=*
// @require      https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js
// @grant GM_download
// ==/UserScript==

var vidEleID = '#vjs_video_3_html5_api';
var vidEleTitle = '#module-clip-title';

$(document).ready(function()
{
    // If the observer sees a video change, call the download method again
    var observer = new MutationObserver(function(mutations) {
        downloadVideo();
    });

    var downloadVideo = function() {
        // Download this video
        GM_download($(vidEleID).attr("src"), $(vidEleTitle).html());

        // start watching to see if the video changes
        observer.observe( $(vidEleID).get(0), {
            attributes: true,
            attributeFilter: ['src']
        });
    };

    // Keep looking until the element we are looking for appears, then call the callback (downloadVideo)
    var waitForEl = function(selector, callback) {
        if (jQuery(selector).attr("src")) {
            callback();
        } else {
            setTimeout(function() {
                waitForEl(selector, callback);
            }, 100);
        }
    };

    // Start watching for the element to appear
    waitForEl(vidEleID, downloadVideo);
});