zvodd
10/31/2015 - 9:33 PM

Auto fills in Market Price in Steam inventory's sell dialog

Auto fills in Market Price in Steam inventory's sell dialog

// ==UserScript==
// @name         Steam, Quick Sell.
// @namespace    http://your.homepage/
// @version      0.1
// @description  Enter the market price on steam items automatically.
// @author       zv_odd
// @match        http://steamcommunity.com/id/*/inventory/
// @grant        none
// ==/UserScript==
var KeyEvent;

// main
SQS_main();
function SQS_main(){

    // Add a more convient sell button.
    var dropdown = document.getElementById("context_selector");
    var newbutton = document.createElement("div");
    // just a dirty copy of the sell button
    var bigButtonHTML = '<a style="float:right; margin-right: 20px;" class="item_market_action_button item_market_action_button_green" href="javascript:SellCurrentSelection()"><span class="item_market_action_button_edge item_market_action_button_left"></span><span class="item_market_action_button_contents">Sell</span><span class="item_market_action_button_edge item_market_action_button_right"></span><span class="item_market_action_button_preload"></span></a>';
    newbutton.innerHTML =  bigButtonHTML;
    dropdown.appendChild(newbutton);

    // replace the sell button function with a wrapper.
    var old_SCS = SellCurrentSelection;
    SellCurrentSelection = function(){
        console.log("Sell button clicked");
       
        // because there are 2 sidebars (1 default i.e. the 1st item) we need to find the active one.
        var right_sideBars = document.querySelectorAll(".inventory_page_right .inventory_iteminfo");
        var correct_sidebar;
        for (var i = 0; i < right_sideBars.length; i++){
            // look for element style not containing "display: none"
            var found_sb = right_sideBars[i];
            cssString = found_sb.getAttribute('style');
            if (cssString.search(/display:\s*none/) === -1){
                correct_sidebar = found_sb;
                break;
            }
        }
                // Get the element from the right side info tab containing market price
        var value_info_box = correct_sidebar.querySelector(".item_market_content .item_market_actions");
        
        
        var x = value_info_box.textContent;
        //split the text on the word  "Volume"
        var y = x.split('Volume')[0];
        // extract numbers and dots only
        var market_price = y.replace(/[^\.\d]*/g,'');

        // call the original sell SellCurrentSelection function
        old_SCS();

        // Wait for the sell dialog to load up.
        waitForElement("#market_sell_buyercurrency_input", function(market_price_input){
            console.log("Inputing market price: $"+market_price);

            // Inject market price value into "Buy Pays" field.
            market_price_input.value = "$"+market_price;

            // No need to simulate key event, we call the event mapped function directly.
            // function from <steamserve>/public/javascript/economy.js
            // this function updates the "You receive" price from the "Buy pays" price.
            SellItemDialog.OnBuyerPriceInputKeyUp();


            // Find and click the subscriber agreement checkbox.
            var checkbox_agree = document.querySelector("#market_sell_dialog_accept_ssa");
            if (checkbox_agree.checked !== true) {
                checkbox_agree.click();
                //SellItemDialog.OnAccept(); // Don't seem to need this
            }
        });
    };

}


// wait for an element to be found by the query selector, then fire callback.
function waitForElement(queryString, callBack){
    var el = document.querySelector(queryString);
    if (typeof el !== 'undefined'){
        callBack(el);
    }
    else{
        setTimeout(function(){
            waitForElement(queryString, callBack);
        }, 10);
    }
}