Recurbate download

Adds a download button for each video on recurbate.com

As of 2021-01-06. See the latest version.

// ==UserScript==
// @name         Recurbate download
// @version      1.0
// @description  Adds a download button for each video on recurbate.com
// @author       razorwax
// @match        https://recurbate.com/play.php*
// @namespace https://greasyfork.org/users/724632
// ==/UserScript==

function AddDownloadButton(appendToElem)
{
    var downloadBtn = $("<button style=\"float: right;font-size: 10px;\" class=\"btn btn-warning\"><b style=\"color:#212528\">Download</b></button>").appendTo(appendToElem);
    downloadBtn.on("click", function()
    {
        var success = 0;
        var videoBtns = $("#play_button");
        for (var i = 0; i < videoBtns.length; ++i)
        {
            success = Math.max(success, 1); // Step 1
            var videoBtn = $(videoBtns[i]);
            var token = videoBtn.attr("data-token");
            var id = videoBtn.attr("data-video-id");
            if (token && id)
            {
                success = Math.max(success, 2); // Step 2
                var url = "/api/get.php?video="+id+"&token="+token;
                $.get(url, function(data)
                {
                    if (data.includes("<video") && data.includes("src="))
                    {
                        var src = data.match(/src=".*"/gm);
                        if (src.length >= 1)
                        {
                            src = src[0];
                            src = src.substring(5, src.length - 1);
                            window.open(src, '_blank');
                        }
                    }
                    else
                    {
                        window.alert("Server blocked request, reason = " + data);
                    }
                });
            }
        }
        if (success < 2)
        {
            window.alert("Couldn't find video to download... [errornum = " + success + "]");
        }
    });
}

$(function ()
{
    var addTo = $("a.bookmark");
    if (addTo.length == 1)
    {
        addTo = addTo.parent();
    }
    else
    {
        addTo = $("div.video-info").children("div");
    }

    if (addTo.length > 0)
    {
        AddDownloadButton($(addTo[0]));
    }
    else
    {
         window.alert("Failed to add download button...");
    }
});