RARBG Direct Download, Magnet and Magnet HASH Buttons

Adds direct download, magnet and reduced to HASH magnet buttons to RARBG.to

// ==UserScript==
// @name         RARBG Direct Download, Magnet and Magnet HASH Buttons
// @namespace    http://rarbg.to/
// @version      0.2
// @description  Adds direct download, magnet and reduced to HASH magnet buttons to RARBG.to 
// @author       BearNecessities
// @match        https://rarbg.to/*
// @match        http://rarbg.to/*
// @grant        none
// ==/UserScript==
var ddicon = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAMCAYAAABWdVznAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA3ZpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNS1jMDIxIDc5LjE1NTc3MiwgMjAxNC8wMS8xMy0xOTo0NDowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3VyY2VSZWYjIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtcE1NOk9yaWdpbmFsRG9jdW1lbnRJRD0ieG1wLmRpZDoyMDNjOGQwOC0wMTA2LWM2NDEtOTFmOC1mYzlhYjVkNTgxNjciIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6QTFB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AAMAAwAAARB8MnnqpuzroZYzQvSNMroUeFIjornbK1mVkRzUgQSyPfbFi/dBRdzCAyJoTFhcBQOiYHyAABUDsiCxAFNWj6UbwQAOw=='
var selectedLinks = $('a[href^="/torrent/"]').not("a[href*=#]");
$.each(selectedLinks, function() {
    id = $(this).attr('href').split('/')[2];
    if (window.location.href.indexOf("/torrent/") > -1) {
        target = $(this).html();
    } else {
        target = $(this).attr('title');
    }

    ddhref = "http://rarbg.to/download.php?id=" + id + "&f=" + target + "-[rarbg.com].torrent";
    ddlink = "<a onmouseover='return overlib(\"Direct download.\")' onmouseout='return nd();'  id='dd-" + id + "' href='" + ddhref + "'><img src='" + ddicon + "'/></a>";
    $(this).after(ddlink);
    if ($('#dd' + id).prev().children().is("img")) {
        $('#dd' + id).css({
            'margin-left': '-12px',
            'margin-top': '-12px'
        });
    } else {
        $('#dd' + id).css({
            'margin-left': '3px'
        });
    }
    $.ajax({
        url: $(this).attr('href'),
        storedData: id,
        success: function(data) {
            mghref = data.match(/href\=\"(magnet[\:\_\-\+\%\?\=\&\;\.0-9a-zA-Z]*)\"/)[1];
            mhhref = mghref.replace(/&.*$/, '');
            mglink = "<a onmouseover='return overlib(\"Magnet link.\")' onmouseout='return nd();'  id='mg-" + this.storedData + "' href='" + mghref + "'><img src='" + mgicon + "'/></a>";
            mhlink = "<a onmouseover='return overlib(\"Magnet hash.\")' onmouseout='return nd();'  id='mh-" + this.storedData + "' href='" + mhhref + "'><img src='" + mhicon + "'/></a>";
            $.event.trigger({
                type: "requestDone",
                id: this.storedData,
                linkData: [mglink,mhlink]
            });
        }
    });
});
$(document).on("requestDone", function(event) {
    $('#dd-' + event.id).after(event.linkData[0]);
    $('#mg-' + event.id).after(event.linkData[1]);
    if ($('#dd-' + event.id).prev().children().is("img")) {
        $('#dd-' + event.id).css({
            'margin-left': '-42px',
            'margin-top': '-12px'
        });
        
        $('#mg-' + event.id).css({
            'margin-left': '3px',
            'margin-top': '-12px'
        });
        
        $('#mh-' + event.id).css({
            'margin-left': '3px',
            'margin-top': '-12px'
        });
    } else {
        $('#dd-' + event.id).css({
            'margin-left': '3px'
        });
        $('#mg-' + event.id).css({
            'margin-left': '3px'
        });
        
        $('#mh-' + event.id).css({
            'margin-left': '3px'
        });
    }        
});