vinny / Improve Mediasite + Download

// ==UserScript==
// @namespace    https://openuserjs.org/users/vinny
// @name         Improve Mediasite + Download
// @version      1.0
// @description  Remove distractions when watching mediasite recordings and download html5 video
// @copyright    2020, vinny (https://openuserjs.org/users/vinny)
// @author       Oliver Hinds, Vincent
// @include      *mediasite.bris.ac.uk/Mediasite/Play/*
// @license      MIT
// @grant        GM_download
// ==/UserScript==


(function () {
    let downloadLink;
    let downloadName;
    let downloaded = false;

    // Feature options:
    var options = {
        keypress_shows_controlbar: false, // Pressing any key on the keyboard will reveal the controlbar just like moving your mouse - true/false
        controlbar_hover: false, // The controlbar will not hide if your mouse is hovering over it - true/false
        use_html5: true, // Use html5 player, which allows for control over the speed of playback - true/false
        show_download: true, // display the download button
        use_legacy_download: false, // opens the video in current tab, if the download doesn't work.
    };

    if (options.use_html5) {
        if (window.location.href.includes("usehtml5=true")) { // Check for html5
            wait(options); // Everything relies on the distracting elements existing, which only happens after the recording has initialised. Thus, we have to wait for them.
        } else if (!window.location.href.includes("?")) {
            window.location.replace(window.location.href + "?usehtml5=true");
        } else {
            window.location.replace(window.location.href + "&?usehtml5=true");
        }
    } else {
        wait(options);
    }

    function download() {
        if (downloadLink && !downloaded) {
            if (!options.use_legacy_download) {
                GM_download(downloadLink, downloadName);
                let downloadBtn = document.getElementsByClassName('download')[0];
                downloadBtn.setAttribute('style', 'display:none');
                let s = document.createElement('span');
                s.setAttribute('class', 'ui-button');
                s.append(document.createTextNode('download in background...'));
                let x = document.getElementsByClassName('alwaysControls')[0];
                x.append(s);
            } else {
                window.location.replace(downloadLink);
            }
        }
    }

    function setDownloadLink() {
        let media = document.getElementById("MediaElement");
        // check if media is loaded and video link exists
        if (media && !!media.firstChild.src) {
            downloadLink = media.firstChild.src;
            downloadName = document.getElementsByTagName('h1').length > 0 ? document.getElementsByTagName('h1')[0].innerText : 'bb-replay';
        } else {
            setTimeout(setDownloadLink, 400);
        }
    }

    function wait(options) {
        if (document.getElementsByClassName("controlBar").length) {
            // If the controlbar element exists, hide distracting elements and create event listeners for Youtube-esque functionality
            var controlBar = document.getElementsByClassName("controlBar")[0];
            controlBar.style.WebkitTransition = "bottom 0.5s";
            controlBar.style.MozTransition = "bottom 0.5s";

            // When mouse is moved, show controlbar. If mouse not moved for certain length of time (e.g. 2000ms), hide controlbar again
            var hide_delay;
            var listenMouse = function () {
                showControls(true);
                clearTimeout(hide_delay);
                hide_delay = setTimeout(function () {
                    showControls(false);
                }, 2000);
            };

            document.addEventListener("mousemove", listenMouse, false); // Listen for mouse movement

            if (options.keypress_shows_controlbar) {
                document.addEventListener("keydown", listenMouse, false); // When any key is pressed, show controlbar
            }

            if (options.controlbar_hover) {
                // When mouse is over controlbar, don't hide controlbar even if mouse is idle
                controlBar.addEventListener("mouseover", function () {
                    document.removeEventListener("mousemove", listenMouse, false);
                    clearTimeout(hide_delay);
                }, false);

                // When mouse leaves the controlbar, resume normal behaviour. Avoids duplicate event listeners
                controlBar.addEventListener("mouseout", function () {
                    document.removeEventListener("mousemove", listenMouse, false);
                    document.addEventListener("mousemove", listenMouse, false);
                }, false);
            }

            if (options.show_download) {
                // create Download button to video
                let downloadBtn = document.createElement("button");
                downloadBtn.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="white" width="18px" height="18px"><path d="M0 0h24v24H0z" fill="none"/><path d="M19 9h-4V3H9v6H5l7 7 7-7zM5 18v2h14v-2H5z"/></svg>';
                downloadBtn.classList.add("download", "ui-button", "ui-corner-all", "ui-widget", "ui-blur-background");
                downloadBtn.addEventListener("click", download);
                controlBar.getElementsByClassName("alwaysControls")[0].appendChild(downloadBtn);
                setDownloadLink();
            }

            hideDistractions();
        } else {
            setTimeout(wait, 200, options); // If elements are non-existent, wait another 200ms before checking again
        }
    }

    function showControls(state) {
        var controlBar = document.getElementsByClassName("controlBar")[0];
        var stream = document.getElementsByClassName("stream")[0];
        if (state) {
            controlBar.style.bottom = "0"; // Show controlbar
            document.body.style.cursor = "Default"; // Show cursor
            stream.style.cursor = "Default";
        } else {
            controlBar.style.bottom = -(controlBar.scrollHeight + 15) + "px"; // Hide controlbar
            document.body.style.cursor = "None"; // Hide cursor
            stream.style.cursor = "None";

        }
    }

    function hideDistractions() {
        // Hide all the distracting and annoying elements
        var stream = document.getElementsByClassName("stream")[0];
        stream.style.userSelect = "None";
        stream.style.webkitUserSelect = "None";
        stream.style.MozUserSelect = "None";
        stream.style.backgroundColor = "Black";

        document.getElementsByClassName("framer")[0].style.visibility = "Hidden";
        document.getElementsByClassName("banner")[0].style.visibility = "Hidden";
        document.getElementsByClassName("stage-lightbox-overlay")[0].style.visibility = "Hidden";
        document.getElementsByClassName("video-stream-overlay")[0].style.visibility = "Hidden";
        document.getElementsByClassName("stage-cycle-primary-button")[0].style.visibility = "Hidden";
        document.getElementsByClassName("toggle-stage-lightbox-button")[0].style.visibility = "Hidden";
    }

})();