Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Steam Inventory - Add Buy & Sell orders
- // @namespace madjoki
- // @include http://steamcommunity.com/*/inventory*
- // @include http://steamcommunity.com/market*
- // @include https://steamcommunity.com/*/inventory*
- // @include https://steamcommunity.com/market*
- // @version 16
- // @downloadURL https://gist.githubusercontent.com/nikop/a80168b747a9693b9175/raw/steam_market.user.js
- // @grant none
- // ==/UserScript==
- "use strict";
- var selectedItem = null;
- var wallet = null;
- var cache2 = {};
- (function (){
- try
- {
- // Ensure we have wallet data (user is logged in)
- if (typeof(window.g_rgWalletInfo) === "undefined" || g_rgWalletInfo && g_rgWalletInfo.success === false)
- return;
- wallet = window.g_rgWalletInfo;
- // Ensure we have valid wallet currency. Default to US$.
- if (typeof(wallet.wallet_country) === "undefined" || typeof(wallet.wallet_currency) === "undefined" || wallet.wallet_country == '' || !wallet.wallet_currency) {
- wallet.wallet_country = 'US';
- wallet.wallet_currency = 1;
- }
- $J('\
- <style type="text/css">\
- #inv_settings { display: none; }\
- .item_market_orders table { margin: auto; }\
- .orders_summary { text-align: center; }\
- .orders_table { margin-bottom: 0.5em; }\
- .market_commodity_orders_table tr:nth-child(even) { background-color: #262626; }\
- .market_commodity_orders_table td, .market_commodity_orders_table th { min-width: 100px; text-align: center; }\
- </style>').appendTo("head");
- $J('body').append('\
- <div id="inv_settings" class="market_modal_dialog">\
- <div class="market_dialog_title">\
- <span>\
- Settings\
- <span class="market_dialog_cancel">\
- <a href="#" class="market_dialog_title_cancel">Close<span class="market_dialog_title_cancel_X">X</span></a>\
- </span>\
- </div>\
- <div class="market_dialog_contents">\
- </div>\
- </div>');
- setInterval(function ()
- {
- if (g_ActiveInventory && selectedItem != g_ActiveInventory.selectedItem)
- updateWithSelectedItem();
- }, 200);
- // Inventory page
- if (/inventory/.exec(window.location.pathname))
- {
- // Check SSA agreement.
- $J('#market_sell_dialog_accept_ssa') .attr('checked', 'checked');
- // Styles
- // Add div to both iteminfo sections. Steam alternates between two to provide fade animation.
- for (var i = 0; i < 2; i++) {
- $J('#iteminfo' + i + '_market_content') .append('<div id="iteminfo' + i + '_market_orders" class="item_market_actions item_market_orders"></div>');
- $J('#iteminfo' + i + '_market_orders') .hide();
- }
- $J('#inventories').on( 'click', '.itemHolder, .newitem', updateWithSelectedItem);
- // We're in the iframe, need to show only the quicksell buttons if the item is in the inventory
- if(/market_hash_name/.exec(window.location.search)) {
- // Prevent the page from creating a popup after clicking the item
- window.Economy_UseResponsiveLayout = function() { return false; };
- // Make the iframe look seamless in the parent page
- $J("body").css("background-color", "transparent");
- $J("body").append($J("#iteminfo0_item_market_actions"));
- $J("body > *").hide();
- // Find the item in the URL
- let market_hash_name = decodeURIComponent(/market_hash_name=([^&;#]+)/.exec(window.location.search)[1].replace(/\+/g, " "));
- selectItem(market_hash_name);
- // If the item is posted for sale, the inventory pages reload and the selection changes so need to refresh the quicksell buttons
- let oldOnSuccess = SellItemDialog.OnSuccess;
- let myOnSuccess = function(transport) {
- SellItemDialog.OnSuccess = oldOnSuccess;
- SellItemDialog.OnSuccess(transport);
- SellItemDialog.OnSuccess = myOnSuccess;
- let removeModal = function() { $J("#sellButtons", parent.document.body).removeClass("sellButtonsModal"); };
- // The trade requires confirmation so there is an additional dialog that we need to hook into
- if(transport.responseJSON && transport.responseJSON.requires_confirmation) {
- $J("div.newmodal_background").click(removeModal);
- $J("div.newmodal:contains('Additional confirmation needed') div.btn_grey_white_innerfade.btn_medium").click(removeModal);
- $J("div.newmodal:contains('Additional confirmation needed') div.newmodal_close").click(removeModal);
- }
- else {
- removeModal();
- selectItem(market_hash_name);
- }
- };
- SellItemDialog.OnSuccess = myOnSuccess;
- }
- }
- // Market listing page
- else if(/market\/listing/.exec(window.location.pathname))
- {
- // Modal and regular style for the iframe
- $J("head").append("\
- <style type='text/css'>\
- .sellButtonsModal { position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: 1000; }\
- #sellButtons {\
- background-image: url('http://steamcommunity-a.akamaihd.net/public/images/login/throbber.gif');\
- background-position: top center; \
- background-repeat: no-repeat;\
- border: 0px;\
- overflow: hidden;\
- }\
- </style>");
- let appid;
- for(let _appid in g_rgAssets)
- appid = _appid;
- let market_hash_name;
- for(let context in g_rgAssets[appid])
- for(let id in g_rgAssets[appid][context])
- market_hash_name = g_rgAssets[appid][context][id].market_hash_name;
- let iframe = $J("<iframe scrolling='no' id='sellButtons' src='http://steamcommunity.com/my/inventory/?modal=1&market=1&market_hash_name=" + market_hash_name + "#" + appid + "'></iframe>");
- let sellButton = $J("a.market_commodity_buy_button:Contains('Sell')");
- sellButton.after(iframe);
- // Add click handlers to sell buttons to make iframe modal
- iframe.load(function() {
- let iframeBody = iframe[0].contentWindow.document.body;
- let buttonContainers = $J("#iteminfo0_item_market_actions, #iteminfo1_item_market_actions", iframeBody);
- // Remove modal class from iframe when the close button on the modal sell dialog is clicked
- let removeModal = function() { iframe.removeClass("sellButtonsModal"); };
- $J("#market_sell_dialog div.newmodal_close", iframeBody).click(removeModal);
- buttonContainers.on("DOMSubtreeModified", function() {
- let buttons = $J(".item_market_action_button", iframeBody);
- buttons.click(function() {
- // Wait until modal background loads and hook into it to remove modal class from iframe when clicked
- let timerId = setInterval(function() {
- let modalBackground = $J("div.newmodal_background", iframeBody);
- if(modalBackground.length > 0) {
- clearInterval(timerId);
- modalBackground.click(removeModal);
- }
- }, 100);
- iframe.addClass("sellButtonsModal");
- });
- });
- });
- }
- // Market Page
- else if (/market/.exec(window.location.pathname))
- {
- /*$J('div.my_listing_section:eq(0) div.market_listing_my_price .market_table_value').each(function (i, el)
- {
- var price = SellItemDialog.GetPriceValueAsInt( $J(el).children('.market_listing_price').text() );
- var feeInfo = CalculateAmountToSendForDesiredReceivedAmount( price, g_rgWalletInfo['wallet_publisher_fee_percent_default'] );
- $J(el).append(" (" + v_currencyformat(price + feeInfo.fees, GetCurrencyCode(g_rgWalletInfo.wallet_currency)) + ")");
- });*/
- }
- }
- catch (err)
- {
- window.alert(err);
- }
- })();
- function showSettings()
- {
- window.showModal('inv_settings');
- }
- function closeSettings()
- {
- window.hideModal('inv_settings');
- }
- function get_cents(text) {
- return GetPriceValueAsInt(text);
- }
- function currencyformat(cents) {
- return window.v_currencyformat(cents, window.GetCurrencyCode(wallet.wallet_currency));
- }
- function unique_values(array) {
- return $J.grep(array, function (el, index) {
- return index == $J.inArray(el, array);
- });
- }
- function triggerMouseEvent(node, eventType) {
- var clickEvent = document.createEvent('MouseEvents');
- clickEvent.initEvent(eventType, true, true);
- node.dispatchEvent(clickEvent);
- }
- function set_sell_price(text)
- {
- $J('#market_sell_buyercurrency_input').val(text);
- // Let Steam handle and validate changed value
- SellItemDialog.OnBuyerPriceInputKeyUp(null);
- }
- function quick_sell_click() {
- // Open sell window
- window.SellCurrentSelection();
- set_sell_price( $J(this).children('.item_market_action_button_contents') .text() );
- triggerMouseEvent($J('#market_sell_dialog_accept') [0], 'click');
- // Prevent default handler
- return false;
- }
- function get_buy_orders_by_market_url(market_url, callback) {
- if (localStorage.getItem('marketid_' + market_url) === null) {
- var oReq = new XMLHttpRequest();
- oReq.onload = function () {
- //
- var m = /Market_LoadOrderSpread\( (\d+) \)/.exec(this.responseText);
- if (m) {
- localStorage['marketid_' + market_url] = m[1];
- get_buy_orders_by_market_id(m[1], callback);
- }
- };
- oReq.onerror = function () {
- console.error(this.statusText);
- }
- oReq.open('get', market_url);
- oReq.send();
- } else {
- get_buy_orders_by_market_id(localStorage['marketid_' + market_url], callback);
- }
- }
- function get_buy_orders_by_market_id(market_id, callback) {
- var history_url = '//steamcommunity.com/market/itemordershistogram?country=' + wallet.wallet_country + '&language=' + window.g_strLanguage + '¤cy=' + wallet.wallet_currency + '&item_nameid=' + market_id;
- /*if (history_url in cache2) {
- callback(cache2[history_url]);
- } */
- var oReq = new XMLHttpRequest();
- oReq.onload = function () {
- var j = JSON.parse(this.responseText);
- cache2[history_url] = j;
- callback(j);
- }
- oReq.onerror = function () {
- console.error(this.statusText);
- }
- oReq.open('get', history_url);
- oReq.send();
- }
- function addQuickSellButtons(itemid, prices)
- {
- $J.each(prices, function (index, value) {
- var btn = $J('<a href="#" class="item_market_action_button item_market_action_button_green"><span class="item_market_action_button_edge item_market_action_button_left"></span><span class="item_market_action_button_contents"></span><span class="item_market_action_button_edge item_market_action_button_right"></span><span class="item_market_action_button_preload"></span></a>');
- $J(btn) .children('.item_market_action_button_contents') .text(currencyformat(value));
- $J(btn) .click(quick_sell_click);
- $J('#iteminfo' + itemid + '_item_market_actions') .append(btn);
- });
- }
- function updateWithSelectedItem() {
- var itemid = window.iActiveSelectView;
- selectedItem = window.g_ActiveInventory.selectedItem;
- $J('#iteminfo' + itemid + '_market_orders').hide();
- // Item needs to be marketable
- if (!selectedItem.marketable)
- return;
- // Show buy & sell orders for commodities
- if (selectedItem.commodity)
- {
- $J('#iteminfo' + itemid + '_market_orders').show();
- $J('#iteminfo' + itemid + '_market_orders').html('Loading buy & sell orders...');
- // Get market id, so we can fetch buy & sell orders
- get_buy_orders_by_market_url($J('#iteminfo' + itemid + '_item_market_actions > div > div a:eq(0)').attr('href'), function (j)
- {
- $J('#iteminfo' + itemid + '_market_orders') .html('\
- <div class="orders_summary" id="sell_order_summary_' + itemid + '"></div>\
- <div class="orders_table" id="sell_order_table_' + itemid + '"></div>\
- <div class="orders_summary" id="buy_order_summary_' + itemid + '"></div>\
- <div class="orders_table" id="buy_order_table_' + itemid + '"></div>'
- );
- $J('#sell_order_summary_' + itemid).html(j.sell_order_summary);
- $J('#sell_order_table_' + itemid).html(j.sell_order_table);
- $J('#buy_order_summary_' + itemid).html(j.buy_order_summary);
- $J('#buy_order_table_' + itemid).html(j.buy_order_table);
- var buyOrderMax = get_cents($J('#buy_order_summary_' + itemid + ' .market_commodity_orders_header_promote:eq(1)') .text());
- var sellOrderMin = get_cents($J('#sell_order_summary_' + itemid + ' .market_commodity_orders_header_promote:eq(1)') .text());
- var common_values = [];
- if (sellOrderMin) {
- common_values.push(sellOrderMin);
- // 3 cents is min price
- if (sellOrderMin > 3)
- common_values.push(sellOrderMin - 1);
- } else {
- sellOrderMin = 3;
- }
- if (buyOrderMax)
- common_values.push(buyOrderMax);
- var extra_values = [];
- for (var i = sellOrderMin - 5; i <= sellOrderMin + 5; i++)
- if (i > 2 && i >= buyOrderMax && common_values.indexOf(i) == - 1)
- extra_values.push(i);
- addQuickSellButtons(itemid, unique_values(common_values));
- addQuickSellButtons(itemid, unique_values(extra_values));
- });
- }
- // Otherwise show just quick sell buttons
- else
- {
- addQuickSellButtons(itemid, [3, 4, 5, 6, 7, 8, 9, 10]);
- return;
- }
- }
- // For use in iframe; if it finds a market_hash_name item in the list of inventory pages, it hides everything but the quicksell buttons, otherwise it removes the iframe
- function selectItem(market_hash_name) {
- let parentSellButton = $J("a.market_commodity_buy_button:Contains('Sell')", parent.document.body);
- // Might contain old quicksell buttons, so need to hide it
- $J("#iteminfo0_item_market_actions").hide();
- // Wait until the inventory is ready and select the item
- let timerId = setInterval(function() {
- if(g_ActiveInventory && g_ActiveInventory.bNeedsRepagination === false) {
- let pages = $J.grep(g_ActiveInventory.pageList, function(page) {
- let items = $J.grep(page.children, function(item) {
- if(typeof item.rgItem == "undefined")
- return false;
- return item.rgItem.market_hash_name == market_hash_name;
- });
- // Found the item in the inventory
- if(items.length > 0) {
- $J("> div > a", items[0]).click();
- // Hide the big sell button in the parent
- parentSellButton.hide();
- // Make only the sell buttons visible
- $J("#iteminfo0_item_market_actions > div").hide();
- $J("#iteminfo0_item_market_actions").show();
- // This one pops up after an ajax call returns so need to hide it this way
- let timerId = setInterval(function() {
- if($J("#es_item0").is(":visible")) {
- $J("#es_item0").hide();
- clearInterval(timerId);
- }
- }, 300);
- return true;
- }
- return false;
- });
- // The item was not found so remove the iframe
- if(pages.length == 0) {
- parentSellButton.show();
- $J("#sellButtons", parent.document.body).remove();
- }
- clearInterval(timerId);
- }
- }, 300);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement