Steam Workshop Downloader

Downloads anything from the steam workshop (steamworkshopdownloader.io api)

// ==UserScript==
// @name           Steam Workshop Downloader
// @version        0.7
// @description    Downloads anything from the steam workshop (steamworkshopdownloader.io api)
// @author         J.H
// @include        *steamcommunity.com/sharedfiles/filedetails/?*id=*
// @include        *steamcommunity.com/workshop/filedetails/?*id=*
// @grant          GM_xmlhttpRequest
// @namespace https://greasyfork.org/users/448067
// ==/UserScript==

(function() {
    'use strict';

    addButton();
})();

function httpPost(url, data, func) {
    GM_xmlhttpRequest({
        method: "POST",
        url: url,
        data: data,
        headers: {
            "Accept": "application/json, text/plain, */*",
            "Accept-Language": "en-US,en;q=0.5",
            "Accept-Encoding": "gzip, deflate",
            "Content-Type": "application/x-www-form-urlencoded",
            "Referer": "https://steamworkshopdownloader.io/"
        },
        onload: function (response) {
            func(response)
        }
    })

}

function setDownloadButtonStyle(text) {
    return '<div class="subscribeIcon"></div><span class="subscribeText"><div class="subscribeOption subscribe selected">' + text + '</div></span>';
}

function addButton() {
    var a = document.createElement("a");
    a.classList.add("btn_darkblue_white_innerfade");
    a.classList.add("btn_border_2px");
    a.classList.add("btn_medium");
    a.innerHTML = setDownloadButtonStyle("Download   ");
    a.onclick = function() {
        a.innerHTML = setDownloadButtonStyle("Downloading ");
        startDownload(getFileID(),a);
    }
    var element = document.getElementById("SubscribeItemBtn");
    element.style.right = 3 + 'px';
    element.style.top = 3 + 'px';
    a.style.right = 3 + 'px';
    a.style.top = 40 + 'px';
    element.parentNode.insertBefore(a, element.nextSibling);
}

function getFileID() {
    var url_string = window.location.href;
    var url = new URL(url_string);
    var id = url.searchParams.get("id");
    return id;
}

var str;
function statusPost(uuid) {
    httpPost("https://api.steamworkshopdownloader.io/api/download/status", JSON.stringify({uuids: [new String(uuid)]}), function(res){
        str = "retrieving";
        JSON.parse(res.responseText, (key, value) => {
            if (key == "status" && value == "prepared") {
                str = value;
            }
        });
    })
    return str;
}

function startDownload(itemID,dButton) {
    httpPost("https://api.steamworkshopdownloader.io/api/download/request",  JSON.stringify({publishedFileId: new Number(itemID),collectionId: null,hidden: true,direct: false}), function(res){
        var uuid = res.responseText.split("\"")[3];
        var request = "https://api.steamworkshopdownloader.io/api/download/transmit?uuid=" + uuid;
        function timerStart() {
            console.log(statusPost(uuid))
            if (statusPost(uuid) == "prepared") {
                httpPost("https://api.steamworkshopdownloader.io/api/download/request", null, function(res){
                    window.location = request;
                    dButton.innerHTML = setDownloadButtonStyle("Download   ");
                    console.log("download");
                })
            } else {
               setTimeout(timerStart, 1000);
            }
        }
        timerStart()
    })
}