Advertisement
ClayRabbit

Steam Trade Offer Enhancer (modified)

Nov 28th, 2014
871
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        Steam Trade Offer Enhancer (mod)
  3. // @namespace   localhost
  4. // @description Browser script to enhance Steam trade offers.
  5. // @include     /^https?:\/\/steamcommunity\.com\/(id|profiles)\/.*\/tradeoffers.*/
  6. // @include     /^https?:\/\/steamcommunity\.com\/tradeoffer.*/
  7. // @version     1.2.0
  8. // @author      HusKy
  9. // ==/UserScript==
  10.  
  11. function getUrlParam(paramName) {
  12.     var params = window.location.search.split(/\?|\&/);
  13.     for(i = 0; i < params.length; i++) {
  14.         var currentParam = params[i].split("=");
  15.         if(currentParam[0] === paramName) {
  16.             return currentParam[1];
  17.         }
  18.     }
  19. }
  20.  
  21. var tradeOfferPage = {
  22.     evaluate_items: function(items) {
  23.         var result = {};
  24.         var ref=0, rec=0, scr=0;
  25.  
  26.         result._total = items.find("div.trade_item").length;
  27.  
  28.         items.find("div.trade_item").each(function() {
  29.             var img = jQuery(this).find("img").attr("src");
  30.             var quality = jQuery(this).css("border-top-color");
  31.  
  32.                 if (img.indexOf("/fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZULUrsm1j-9xgEbZQsUYhTkhzJWhsO0Mv6NGucF1YJitJYM3G87xVN-NeXsNm5iIAGWA_IMBaBopV3pXX5hvZZiB4O08r5IOVK41_IxDn4/")>0)
  33.                     rec++;
  34.                 else if (img.indexOf("/fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZULUrsm1j-9xgEbZQsUYhTkhzJWhsO1Mv6NGucF1Yww48IM2jRulwUsMrvmMWUwJ12aV_RYDKdi8lm4CyEw7MU3AoSwrr9IOVK492K3n-o/")>0)
  35.                     ref++;
  36.                 else if (img.indexOf("/fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZULUrsm1j-9xgEbZQsUYhTkhzJWhsPZAfOeD-VOnohsvJRQjzc-wwd5N-exZWY0dgCWUqVYDaZtoVu0UXVmvJI6A4fi9fUWJ1usHCsatA/")>0)
  37.                     scr++;
  38.            
  39.            
  40.             if(result[img] === undefined)
  41.                 result[img] = {};
  42.  
  43.             if(result[img][quality] === undefined) {
  44.                 result[img][quality] = 1;
  45.             } else {
  46.                 result[img][quality]++;
  47.             }
  48.         });
  49.         result._refsum=Math.floor((ref+rec/3+scr/9)*100)/100;
  50.  
  51.         return result;
  52.     },
  53.  
  54.     dump_summary: function(tradeoffer, type, items) {
  55.         if(items._total <= 0) return;
  56.        
  57.         var htmlstring = "Summary (" + items._total + " " + (items._total === 1 ? "item" : "items") +(items._refsum>0?" [Metal: "+items._refsum.toFixed(2)+" ref]":"")+ "):<br>";
  58.  
  59.         for(var prop in items) {
  60.             if(prop.indexOf("_")===0) continue;
  61.  
  62.             var item_type = items[prop];
  63.             for(var quality in item_type) {
  64.                 htmlstring += "<span class=\"summary_item\" style=\"background-image: url('" + prop + "'); border-color: " + quality + ";\"><span class=\"summary_badge\">" + item_type[quality] + "</span></span>";
  65.             }
  66.         }
  67.  
  68.         htmlstring += "<br><br>Items:<br>";
  69.         tradeoffer.find("div." + type + " > div.tradeoffer_items_header")
  70.                   .after("<div class=\"tradeoffer_items_summary\">" + htmlstring + "</div>");
  71.     }
  72. };
  73.  
  74. var tradeOfferWindow = {
  75.     evaluate_items: function(items) {
  76.         var result = {};
  77.         result._total = 0;
  78.         var ref=0, rec=0, scr=0;
  79.  
  80.         var slot_inner = items.find("div.slot_inner");
  81.  
  82.         slot_inner.each(function() {
  83.             if(jQuery(this).html() !== "" && jQuery(this).html() !== null) {
  84.                 result._total++;
  85.                 var item = jQuery(this).find("div.item");
  86.  
  87.                 var img = item.find("img").attr("src");
  88.                 var quality = item.css("border-top-color");
  89.  
  90.                 if (img=="https://steamcommunity-a.akamaihd.net/economy/image/fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZULUrsm1j-9xgEbZQsUYhTkhzJWhsO0Mv6NGucF1YJitJYM3G87xVN-NeXsNm5iIAGWA_IMBaBopV3pXX5hvZZiB4O08r5IOVK41_IxDn4/96fx96f")
  91.                     rec++;
  92.                 else if (img=="https://steamcommunity-a.akamaihd.net/economy/image/fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZULUrsm1j-9xgEbZQsUYhTkhzJWhsO1Mv6NGucF1Yww48IM2jRulwUsMrvmMWUwJ12aV_RYDKdi8lm4CyEw7MU3AoSwrr9IOVK492K3n-o/96fx96f")
  93.                     ref++;
  94.                 else if (img=="https://steamcommunity-a.akamaihd.net/economy/image/fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZULUrsm1j-9xgEbZQsUYhTkhzJWhsPZAfOeD-VOnohsvJRQjzc-wwd5N-exZWY0dgCWUqVYDaZtoVu0UXVmvJI6A4fi9fUWJ1usHCsatA/96fx96f")
  95.                     scr++;
  96.                
  97.                
  98.                 if(result[img] === undefined)
  99.                     result[img] = {};
  100.  
  101.                 if(result[img][quality] === undefined) {
  102.                     result[img][quality] = 1;
  103.                 } else {
  104.                     result[img][quality]++;
  105.                 }
  106.             }
  107.         });
  108.         result._refsum=Math.floor((ref+rec/3+scr/9)*100)/100;
  109.        
  110.         return result;
  111.     },
  112.  
  113.     dump_summary: function(target, type, items) {
  114.         if(items._total <= 0) return;
  115.  
  116.         var htmlstring = type + " summary (" + items._total + " " + (items._total === 1 ? "item" : "items") +(items._refsum>0?" [Metal: "+items._refsum.toFixed(2)+" ref]":"")+ "):<br>";
  117.  
  118.         for(var prop in items) {
  119.             if(prop.indexOf("_")===0) continue;
  120.  
  121.             var item_type = items[prop];
  122.             for(var quality in item_type) {
  123.                 htmlstring += "<span class=\"summary_item\" style=\"background-image: url('" + prop + "'); border-color: " + quality + ";\"><span class=\"summary_badge\">" + item_type[quality] + "</span></span>";
  124.             }
  125.         }
  126.  
  127.         target.append(htmlstring);
  128.     },
  129.  
  130.     summarise: function() {
  131.         var target = jQuery("div.tradeoffer_items_summary");
  132.         target.html("");
  133.  
  134.         var mine = jQuery("div#your_slots");
  135.         var other = jQuery("div#their_slots");
  136.  
  137.         var my_items = this.evaluate_items(mine);
  138.         var other_items = this.evaluate_items(other);
  139.  
  140.         this.dump_summary(target, "My", my_items);
  141.         if(other_items._total > 0) target.append("<br><br>");
  142.         this.dump_summary(target, "Their", other_items);
  143.     },
  144.  
  145.     init: function() {
  146.         var self = this;
  147.  
  148.         // something is loading
  149.         var isReady = jQuery("img[src$='throbber.gif']:visible").length <= 0;
  150.  
  151.         // our partner's inventory is also loading at this point
  152.         var itemParamExists = getUrlParam("for_item") !== undefined;
  153.         var hasBeenLoaded = true;
  154.  
  155.         if(itemParamExists) {
  156.             // format: for_item=<appId>_<contextId>_<itemId>
  157.             var item = getUrlParam("for_item").split("_");
  158.             hasBeenLoaded = jQuery("div#inventory_" + UserThem.strSteamId + "_" + item[0] + "_" + item[1]).length > 0;
  159.         }
  160.  
  161.         if(isReady && (!itemParamExists || hasBeenLoaded)) {
  162.             setTimeout(function() {
  163.                 self.summarise();
  164.             }, 500);
  165.  
  166.             return;
  167.         }
  168.  
  169.         if(itemParamExists && hasBeenLoaded) {
  170.             setTimeout(self.deadItem.bind(self), 5000);
  171.             return;
  172.         }
  173.  
  174.         setTimeout(function() {
  175.             self.init();
  176.         }, 250);
  177.     },
  178.  
  179.     deadItem: function() {
  180.         var deadItemExists = jQuery("a[href$='_undefined']").length > 0;
  181.         var item = getUrlParam("for_item").split("_");
  182.  
  183.         if(deadItemExists) {
  184.             unsafeWindow.g_rgCurrentTradeStatus.them.assets = [];
  185.             RefreshTradeStatus(g_rgCurrentTradeStatus, true);
  186.             alert("Seems like the item you are looking to buy (ID: " + item[2] + ") is no longer available. You should check other users backpack and see if it's still there.");
  187.         } else {
  188.             // Something was loading very slowly, restart init...
  189.             this.init();
  190.         }
  191.     }
  192. };
  193.  
  194. jQuery(function() {
  195.  
  196. var location = window.location.pathname;
  197.  
  198. // Append CSS style.
  199. var style = "<style type='text/css'>" +
  200.             ".tradeoffer_items_summary { color: #fff; font-size: 12px; }" +
  201.             ".summary_item { padding: 3px; margin: 0 2px 2px 0; background-color: #3C352E;background-position: center; background-size: 48px 48px; background-repeat: no-repeat; border: 1px solid; font-size: 16px; width: 48px; height: 48px; display: inline-block; }" +
  202.             ".summary_badge { padding: 1px 3px; border-radius: 4px; background-color: #0099CC; border: 1px solid #003399; font-size: 12px; }" +
  203.             ".btn_custom { border-width: 0; background-color: black; border-radius: 2px; font-family: Arial; color: white; line-height: 20px; font-size: 12px; padding: 0 15px; vertical-align: middle; cursor: pointer; }" +
  204.             "</style>";
  205. jQuery(style).appendTo("head");
  206.  
  207. // Trade offer page with multiple trade offers ...
  208. if(location.indexOf("tradeoffers") > -1) {
  209.  
  210.     // Retrieve all trade offers.
  211.     var trade_offers = jQuery("div.tradeoffer");
  212.  
  213.     if(trade_offers.length > 0) {
  214.         trade_offers.each(function() {
  215.             var others = jQuery(this).find("div.primary > div.tradeoffer_item_list");
  216.             var mine = jQuery(this).find("div.secondary > div.tradeoffer_item_list");
  217.  
  218.             // Evaluate both sides.
  219.             var other_items = tradeOfferPage.evaluate_items(others);
  220.             var my_items = tradeOfferPage.evaluate_items(mine);
  221.  
  222.             // Dump the summaries somewhere ...
  223.             tradeOfferPage.dump_summary(jQuery(this), "primary", other_items);
  224.             tradeOfferPage.dump_summary(jQuery(this), "secondary", my_items);
  225.         });
  226.     }
  227.  
  228.     // Single trade offer window ...
  229. } else if(location.indexOf("tradeoffer") > -1) {
  230.  
  231.     // Append new divs ...
  232. //    jQuery("div.trade_left div.trade_box_contents").append("<div class=\"trade_rule selectableNone\"/><div class=\"item_adder\"/>");
  233.     jQuery("div.filter_ctn").before("<div class=\"item_adder\"/>");
  234. //    jQuery("div.item_adder").append("<div class=\"selectableNone\">Add multiple items:</div>");
  235.     val=GM_getValue("amount_control");
  236.     jQuery("div.item_adder").append("<input id=\"amount_control\" class=\"filter_search_box\" type=\"text\" placeholder=\"16\" value=\""+(val?val:"")+"\" size=3  style='top:0'> ");
  237.     jQuery("div.item_adder").append("<button id=\"btn_additems\" type=\"button\" class=\"btn_custom\">Add</button>");
  238.     jQuery("div.item_adder").append(" <button id=\"btn_additems\" type=\"button\" class=\"btn_custom\">Reverse Add</button>");
  239.     jQuery("div.item_adder").append(" &nbsp <button id=\"btn_keys\" type=\"button\" class=\"btn_custom\">Keys</button>");
  240.     jQuery("div.item_adder").append(" <button id=\"btn_refs\" type=\"button\" class=\"btn_custom\">Refs</button>");
  241.     jQuery("div.item_adder").append(" <button id=\"btn_craft\" type=\"button\" class=\"btn_custom\">Metal</button>");
  242.  
  243.     jQuery("div.trade_left div.trade_box_contents").append("<div class=\"trade_rule selectableNone\"/><div class=\"tradeoffer_items_summary\"/>");
  244.  
  245.    
  246.     // Refresh summaries whenever ...
  247.     jQuery("body").click(function() {
  248.         setTimeout(function() {
  249.             tradeOfferWindow.summarise();
  250.         }, 500);
  251.     });
  252.  
  253.     jQuery("button#btn_refs").click(function() {
  254.         var x=jQuery("input#filter_control");
  255.         if (x) {
  256.             x.val("Refined metal");
  257.             x.click();
  258.         }
  259.     });
  260.  
  261.     jQuery("button#btn_craft").click(function() {
  262.         var x=jQuery("input#filter_control");
  263.         if (x) {
  264.             x.val(" metal");
  265.             x.click();
  266.         }
  267.     });
  268.  
  269.     jQuery("button#btn_keys").click(function() {
  270.         var x=jQuery("input#filter_control");
  271.         if (x) {
  272.             x.val("Mann Co. Supply Crate Key");
  273.             x.click();
  274.         }
  275.     });
  276.  
  277.    
  278.     // Handle item auto adder
  279.     jQuery("button#btn_additems").click(function() {
  280.      
  281.         // Do not add items if the offer cannot be modified
  282.         if(jQuery("div.modify_trade_offer:visible").length > 0) return;
  283.  
  284.         // Collect all items
  285.         var inventory = jQuery("div.inventory_ctn:visible");
  286.         var items = inventory.find("div.itemHolder").filter(function() {
  287.             return jQuery(this).css("display") !== "none";
  288.         }).find("div.item").filter(function() {
  289.             return jQuery(this).css("display") !== "none";
  290.         });
  291.  
  292.         // Get amount value
  293.         var amount = parseInt(jQuery("input#amount_control").val());
  294.         if(isNaN(amount)) amount = 16;
  295.         else GM_setValue( "amount_control", amount );
  296.         if(items.length < amount) amount = items.length;
  297.  
  298.         // Add all items
  299.         if (this.innerText=='Add') {
  300.             for(i = 0; i < amount; i++) {
  301.                 setTimeout(MoveItemToTrade, i * 50, items[i]);
  302.             }
  303.         } else {
  304.             for(i = items.length-1; amount>0 ; i--) {
  305.                 setTimeout(MoveItemToTrade, (items.length-i) * 50, items[i]);
  306.                 amount--;
  307.             }
  308.         }
  309.         // Refresh summaries
  310.         setTimeout(function() {
  311.             tradeOfferWindow.summarise();
  312.         }, amount * 50 + 500);
  313.     });
  314.  
  315.     tradeOfferWindow.init();
  316.  
  317.     var itemParam = getUrlParam("for_item");
  318.     if(itemParam !== undefined) {
  319.         var item = itemParam.split("_");
  320.  
  321.         unsafeWindow.g_rgCurrentTradeStatus.them.assets.push({
  322.             "appid":item[0],
  323.             "contextid":item[1],
  324.             "assetid":item[2],
  325.             "amount":1
  326.         });
  327.  
  328.         RefreshTradeStatus(g_rgCurrentTradeStatus, true);
  329.     }
  330. }
  331.  
  332. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement