heninth / ArtifactFire Sell Price

// ==UserScript==
// @name    ArtifactFire Sell Price
// @version 0.1
// @match   https://www.artifactfire.com/artifact/cards/*
// @match   https://www.artifactfire.com/artifact/deck/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js
// @require https://cdnjs.cloudflare.com/ajax/libs/lscache/1.3.0/lscache.min.js
// @grant   GM_xmlhttpRequest
// @license MIT
// ==/UserScript==

(function () {
  'use strict';

  if (/\/artifact\/cards\/.*/.test(window.location.pathname)) {
    // card
    var name = $('.cards__details h1').text().replace(': Artifact Card', '').trim();
    getPrice(name, function (price) {
      var template = '<div class="cards__details__stats"><h2>Steam Market Price</h2><div><h3>Sale starting at ' + price + '</h3><div style="clear:both;"></div></div></div>';
      $('.cards__details').eq(0).append(template);
    });

  }
  else if (/\/artifact\/deck\/.*/.test(window.location.pathname)) {
    // deck

    // hero
    $('.deck__header__main__heroes > a').each(function (i) {
      var name = $(this).find('.hero__stats__name').text().trim();
      var self = this;

      getPrice(name, function (price) {
        var template = '<div class="hero__stats__color">' + price + '</div>';
        $(self).find('.hero__stats').eq(0).append(template);
      });
    });

    // other cards
    $('.card-table__body > a').each(function (i) {
      var name = $(this).find('span > em').eq(0).text().trim();
      var self = this;

      getPrice(name, function (price) {
        var template = price + ' | ';
        $(self).find('span > em').eq(0).prepend(template);
      });
    });

  }

  function getPrice(card_name, callback) {
    var cache = lscache.get('artifactfirecard_' + card_name);
    if (cache) {
      console.log('getPrice (cache) : ' + card_name);
      callback(cache);
      return;
    }

    GM_xmlhttpRequest({
      method: "GET",
      url: 'https://steamcommunity.com/market/search?appid=583950&q=' + card_name,
      onload: function (response) {
        var result = $(response.responseText).find('#result_0');
        if (result) {
          var price = result.find('.market_table_value > .normal_price').eq(0).text();
          if (!price) {
            price = '-';
            lscache.set('artifactfirecard_' + card_name, '-', rand(2, 5));
          }
          else {
            lscache.set('artifactfirecard_' + card_name, price, 10 + rand(2, 5));
          }
          console.log('getPrice (new) : ' + card_name + ' = ' + price);

          callback(price);
        }
      }
    });
  }

  function rand(min, max) {
    return Math.floor(Math.random() * max) + min;
  }
})();