namkazt / RomHustler Direct download

// ==UserScript==
// @name RomHustler Direct download
// @description generate direct download rom from romhustler. also display download button for quick download.
// @author Namkazt ( nam.kazt.91@gmail.com )
// @include http://romhustler.net/roms/*
// ==/UserScript==
//------------------------------
// Prepare
//------------------------------
//$('head').append('<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">');
//$('head').append('<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">');
//$('body').append('<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>');
//------------------------------
// Funcs
//------------------------------
function Download(url) {document.getElementById('downloadHelper').src = url;}
$("#roms_table").before('<iframe id="downloadHelper" style="display:none;"></iframe>');
var romsContainer = $("#roms_table .row");
romsContainer.each(function(i,v){
    var romObj = $(v);
    var tempLink = romObj.find("a")[0];
    var romURL = $(tempLink).attr("href");
    var buttonDownload = $('<button type="button" class="btn btn-primary btn-lg" id="leakButton">Download</button>');
    $(tempLink).after(buttonDownload);
    buttonDownload.click(function(e){
        $.get(romURL,function( data ) {
            var firstIndicator = data.indexOf("download/");
            var body = data.substr(firstIndicator+9,30);
            var secondIndicator = body.indexOf("/");
            var romID = body.substr(0, secondIndicator);
            var downloadURL = "http://romhustler.net/link/" + romID + "?_=" + Math.floor(Date.now() / 1000);
            $.get(downloadURL, function(data) {
                var downloadResult = JSON.parse(data);
                Download(downloadResult.direct);
            });
        });
    });
});