PapaAndreas / Show Target Age

// ==UserScript==
// @name         Show Target Age
// @namespace    PapaAndreas [2169463]
// @version      1.0
// @description  Shows the age of possible targets on elimination page
// @author       PapaAndreas [2169463]
// @match        https://www.torn.com/competition.php
// @license MIT
// @run-at document-end
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==

$(document).ajaxComplete((event, jqXHR, ajaxObj) => {
  if (jqXHR.responseText) {
    handle(jqXHR.responseText);
  }
});

function handle(responseText) {
  if (responseText.trim().endsWith('<div class="clear"></div>')) {
    if ($("#apikey").length == 0) {
      var API_KEY = GM_getValue("API_KEY", "insert API Key");
      $('<input>').attr({
        type: 'text',
        id: 'apikey',
        value: API_KEY
      }).appendTo('#mainContainer');
    }
    API_KEY = $("#apikey").first().val();
    $(".attack a").each(function () {
      var extractID = /.*user2ID=(\d+)/;
      var attackHref = $(this).attr("href");
      var id = attackHref.replace(extractID, '$1');
      var attackLink = $(this);

      $.get("https://api.torn.com/user/" + id + "?selections=profile&key=" + API_KEY, function (data) {
        if (data.age) {
          attackLink.text(data.age);
          GM_setValue("API_KEY", API_KEY);
        }
      });
    });
  }
}