xolve / Helper

// ==UserScript==
// @name        Helper
// @namespace   nopes
// @description none
// @grant       none
// @include     https://online.becker.com/playlist/*
// @version     2
// @run-at      document-end
// ==/UserScript==


String.prototype.unescapeHtml = function () {
    var temp = document.createElement("div");
    temp.innerHTML = this;
    var result = temp.childNodes[0].nodeValue;
    temp.removeChild(temp.firstChild);
    return result;
}

var file_names = []
var mp4s = [];
var subs = [];
var subs_lang = [];

function get_toc() {
    var lec_items = document.getElementById("items").children;
    for (var i = 0; i < lec_items.length; i++) {
        var lec = lec_items[i];
        var file_name = lec.textContent.trim();
        file_name = file_name.replace(/\s+/g, " ").replace(/:/g, ".");
      
        file_names.push(file_name);
        mp4s.push(plItems[i].mp4Url);
        this_subs = [];
        this_subs_lang = [];
        for (var j = 0; j < plItems[i].captions.length; j++) {
            console.log(plItems[i].captions[j].src);
            this_subs.push(plItems[i].captions[j].src);
            this_subs_lang.push(plItems[i].captions[j].srclang);
        }
        subs.push(this_subs);
        subs_lang.push(this_subs_lang);
    }
    
    console.log("DONE");
    
//     for (var i = 0; i < file_names.length; i++) {
//         console.log("File name: " + file_names[i]);
//         console.log("link: " + mp4s[i]);
//         for (var j = 0; j < subs[i].length; j++) {
//             console.log("\t" + subs[i][j]);
//             console.log("\t" + subs_lang[i][j]);
//         }
//     }
}

function list_download_links() {
    var top_div = document.createElement("div");
    var top_p = document.createElement("p");
    top_div.appendChild(top_p);
    
    for (var i = 0; i < mp4s.length; i++) {
        var mp4_link = document.createElement("a");
        mp4_link.setAttribute("href", mp4s[i]);
        mp4_link.setAttribute("download", file_names[i] + ".mp4");
        var mp4_text = document.createTextNode(file_names[i] + ".mp4");
        mp4_link.appendChild(mp4_text);
        top_p.appendChild(mp4_link)
        
        var nbsp = document.createTextNode("    ");
        top_p.appendChild(nbsp);
        
        var this_subs = subs[i];
        var this_subs_lang = subs_lang[i];
        for (var j = 0; j < this_subs.length; j++) {
            var sub_link = document.createElement("a");
            sub_link.setAttribute("href", this_subs[j]);
            sub_link.setAttribute("download", file_names[i] + "_" + this_subs_lang[j] + ".srt");
            var sub_text = document.createTextNode(file_names[i] + "_" + this_subs_lang[j] + ".srt");
            sub_link.appendChild(sub_text);
            top_p.appendChild(sub_link);
            
            var nbsp = document.createTextNode("    ");
            top_p.appendChild(nbsp);
            
        }
        
        var new_line = document.createElement("br");
        top_p.appendChild(new_line);
    }
    
    top_p.style.cssText = "top: 200px; left: 200px; width: 80%; background-color: silver; position: absolute; padding: 20px; line-height: 25px;";
    
    document.getElementsByTagName("body")[0].appendChild(top_div);
}


get_toc();
list_download_links();