diegokfu / Barter working with Tremorgames

// ==UserScript==
// @name        Barter working with Tremorgames
// @namespace   asdasd
// @include     *://barter.vg/u/*/o*
// @version     0.2
// @grant       GM_xmlhttpRequest
// @require     https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js
// @author      Diego Ferreira
// ==/UserScript==

function parseSearch(text){
  text.replace("&", "%26");
  text.replace(":", "%3A");
  text.replace("®", "");
  text.replace("’", "");
  text.replace("'", "");
  text.replace(",", "");
  text.replace("=", "");
  text.replace("`", "");
  text.replace("?", "");
  text.replace("/", "");
  text.replace("|", "");
  return text;
}

function handle(currentId){
  GM_xmlhttpRequest({
    method: 'GET',
    url: "https://barter.vg/i/"+currentId.split("_")[1].split(",")[0],
    onload: function(r) {
      var xmlString = r.responseText
      , parser = new DOMParser()
      , doc = parser.parseFromString(xmlString, "text/html");
      var name=doc.title.replace("(Steam)", "Steam Game");
      
      itemName=parseSearch(name);
      console.log('search for', itemName);
      
      GM_xmlhttpRequest({
        method: 'GET',
        url: "http://www.tremorgames.com/achievements/ajax_search_shop.php?searchterm="+itemName+"&format=json",
        onload: function(rr) {
          var data =JSON.parse(rr.responseText)[0];
          
          if (data != null && data != undefined){
            var ItemName= data.ItemName;
            var ItemID=data.ItemID;
            var ItemsRemaining=data.ItemsRemaining;
            var PriceCoins=data.PriceCoins;
            
            if(document.getElementById(currentId) == null){
              
              if(ItemsRemaining > 0){
                $('#'+currentId.split("_")[0]).after('<font id="'+currentId+'" size="2">['+PriceCoins+'] <font color="green">'+ItemsRemaining+' left</font></font>');
              } else {
                $('#'+currentId.split("_")[0]).after('['+PriceCoins+'] <font id="'+currentId+'" color="red" size="2">Out!</font>');
              }
            
          }
            
          } else {
            $('#'+currentId.split("_")[0]).after('<font color="red">Not Found</font>');
          }
          
         

        }  
      });
    }  
  });
}

$('input').click(function(){
  currentId=$(this).attr('id')+"_"+$(this).attr('value');
  handle(currentId);
});