generickyle123 / Empornium Thanks & Credits

// ==UserScript==
// @license GPL-3.0
// @name        Empornium Thanks & Credits
// @namespace   http://localhost
// @include     /^https?://www\.empornium\.(me|sx)/torrents\.php\?id.*/
// @version     0.4
// @grant       none
// ==/UserScript==
// Tested on Firefox Quantum 57.0.4 (64-bit) and Chrome 63.0.3239.132 (Official Build) (64-bit) with Tampermonkey 4.5.5660

(function($) {
//Set what goes in the message sent to uploader
var message = "Thanks for sharing " + window.location.href + "! [br][br] Credits sent with https://www.empornium.me/forums.php?action=viewthread&threadid=77858 by Doppleganger. [br][br] :emplove: ";

//Get user name, ID & authkey
var user, authkey, k, user_authkey;
var username = $('a.username').text().replace('nav_userinfo', '');
var rss = $('link[title="Empornium - News"]').attr('href').split('&');
for (k = 0; k < rss.length; k++) {
  user_authkey = rss[k].split('=');
  if (user_authkey[0] == 'user') user = user_authkey[1];
  if (user_authkey[0] == 'authkey') authkey = user_authkey[1];
}

//Get uploader name & ID
var uploader = $('em > a').text();
var uploaderID = $('em > a').attr('href').split('=')[1];

if (username !== uploader) {
  //Create the submit form, dropdown and insert the button
  var form = $("<form/>", {
    id: "cForm",
    action: "bonus.php",
    method: "POST"
  }).prependTo('.box.pad.center');

  form.append(
    $("<input>", { name: "action", value: "buy", type: "hidden" }),
    $("<input>", { name: "othername", value: uploader, /*Uploader name*/ type: "hidden" }),
    $("<input>", { id: "message", name: "message", value: message, /*User message*/ type: "hidden" }),
    $("<input>", { name: "userid", value: user, /*Donor user ID*/ type: "hidden" }),
    $("<input>", { name: "auth", value: authkey, /*Donor user authkey*/ type: "hidden" }),
    $("<input>", { id: "itemid", name: "itemid", value: "1", /*How many credits*/ type: "hidden" }),
    $("<input>", { name: "retu", value: uploaderID, /*Uploader ID*/ type: "hidden" }),
    $("<input>", { id: "submit_", name: "submit_", value: "Thank and gift 600 credits!", type: "submit", style: "font-weight:bold;font-size:larger;" })
  );

  var cSelect = $("<select/>", {
    id: "cSelect"
  }).prependTo(form);

  cSelect.append(
    $("<option>", { text: "600", value: 1 }),
    $("<option>", { text: "3000", value: 2 }),
    $("<option>", { text: "6000", value: 3 }),
    $("<option>", { text: "60000", value: 85 })
  );

  $('<div/>').attr({ 'id':'bonusdiv', 'class':'pad' }).insertAfter('#cForm');
}

//Update the form with the dropdown selection
$(function() {
  $('#cSelect').change(function() {
    $('#itemid').val($(this).val());
    $('#submit_').val("Thank and gift " + $(this).find("option:selected").text() + " credits!");
  });
});

//Say thanks and gift credits
var n = 0;

$('#cForm').submit(function() {
  if ($("#thankstext:contains('" + username + "')").length == 0) Say_Thanks();
  $.ajax({
    data: $(this).serialize(),
    type: $(this).attr('method'),
    url: $(this).attr('action'),
    success: function(response) {
      var result = $(response).find('.center.body').text();
      var credits = $(response).find('#stats_credits').text();
      if (result) {
        n = n + parseInt($('#cSelect option:selected').text(), 10);
        $('#bonusdiv').html("<h3>You gave a gift of " + n + " credits to " + uploader + "!</h3>");
        $('#stats_credits').text(credits);
      } else {
        $('#bonusdiv').html("<h3>Your request failed!</h3>");
      }
    }
  });
  return false;
});
})(jQuery);