Skip to content

Instantly share code, notes, and snippets.

@Zalvie
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Zalvie/77e42341c4b6dc8b4e41 to your computer and use it in GitHub Desktop.
Save Zalvie/77e42341c4b6dc8b4e41 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Steam Card Buyer
// @namespace http://leakforums.org/
// @version 0.2
// @description Messy script to buy cards
// @author You
// @include http://steamcommunity.com*
// @include https://steamcommunity.com*
// @match *://steamcommunity.com/*/gamecards/*
// @grant none
// @run-at document-end
// ==/UserScript==
/*
idfk
*/
var country = 'NO'
var currency = 9
var price_url = 'http://steamcommunity.com/market/itemordershistogram?country=' + country + '&language=english&currency=' + currency + '&item_nameid='
var price_format = ''
var cards = {}
var current_index = 0;
var next_card = null;
$J.ajaxSetup({
xhrFields: {
withCredentials: true
}
});
/*
if($J('.badge_craft_button').first()[0] !== undefined) {
$J.each([0,1,2,3,4], function(i, e) {
$J('.badge_craft_button').click()
});
}
*/
var $found = $J('.badge_card_to_collect_links > a[href^="http://steamcommunity.com/market/listings/753"]')
$J('.gamecards_inventorylink').append($J('<div class="buy_cards"><span class="wallet"></span><br><span class="found"></span><br><table class="cards"><tr></tr><tr><td align="right"><b>Total:</b></td><td><button class="btn_grey_grey btn_medium" id="plus5" style="padding: 5px;width: 100%">+5</button></td><td class="total"></td></tr><tr></tr><tr><td colspan="3"><button type="button" id="buy_button" class="btn_green_white_innerfade btn_medium_wide" style="padding: 10px 20px;width: 100%">Buy Cards</button></td></table></div>'))
update_currency = function(callback) {
$J.get('http://steamcommunity.com/market/', function (content) {
g_rgWalletInfo = JSON.parse(content.split('g_rgWalletInfo = ').pop().split(';').shift().trim());
callback(g_rgWalletInfo)
})
}
amount_change = function(item_id, price, e) {
quantity = +($J(e).val())
cards[+(item_id)].quantity = quantity
$J('#price_'+item_id).text(format_price(price*quantity))
calculate_total()
}
handleChange = function(e) {
quantity = +($J(this).val())
item_id = +($J(this).attr('item_id'))
price = +($J(this).attr('price'))
cards[item_id].quantity = quantity
$J('#price_'+item_id).text(format_price(price*quantity))
calculate_total()
}
update_status = function(item_id, status) {
$J('.status_' + item_id).text(status)
}
check_order = function(co_card) {
$J.get('/market/getbuyorderstatus/', {sessionid: g_sessionID, buy_orderid: co_card.orderid}, function(json) {
if(co_card.checks == 0)
buy_card()
console.log(co_card)
co_card.checks++;
left = co_card.quantity-(+(json.quantity_remaining))
update_status(co_card.item_id, 'Bought '+ left +'/'+ co_card.quantity)
if (left == co_card.quantity) {
$J('.status_' + co_card.item_id).parent().css('color', '#19D319')
return
} else {
console.log('wait')
co_card.checks++;
if(co_card.checks >= 50) {
update_status(co_card.item_id, 'Waited for too long');
return;
}
}
setTimeout(function() {
check_order(co_card);
}, 500);
})
}
buy_card = function() {
setTimeout(function() {
for (card in cards) break;
console.log(card)
card = cards[card]
console.log(card)
if(!card) {
return
}
delete cards[card.item_id]
if(card.status != 0) {
console.log('Tried to buy', card.name, 'twice')
return
}
if(card.quantity == 0)
return
card.status = 1
update_status(card.item_id, 'Buying Card')
$J.post('https://steamcommunity.com/market/createbuyorder/', {sessionid: g_sessionID, currency: currency, appid: 753, market_hash_name: card.hash_name, price_total: card.price*card.quantity, quantity: card.quantity}, function(json) {
if(!json.success) {
update_status(card.item_id, 'Failed to buy card')
console.log(json)
return;
}
card.orderid = json.buy_orderid;
update_status(card.item_id, 'Waiting...')
check_order(card)
});
}, 300);
}
calculate_total = function() {
total = 0
$J.each(cards, function(i, e) {
total += e.price * e.quantity
})
$J('.total').text(format_price(total))
}
format_price = function(price) {
return price_format.replace('{}', ((price) / 100).toFixed(2))
}
append_to_table = function(item_id) {
if(cards[item_id] === undefined) {
console.log('Bad build_form item_id =', item_id)
return;
}
card = cards[item_id]
$J('.cards').prepend($J('<tr>\
<td align="right">' + card.name + '</td><td>\
<input style="padding: 0;width: 28px;text-align: center;" class="card_amount btn_grey_grey btn_medium" id="amount_'+item_id+'" value="1" min="0" max="5" item_id="'+item_id+'" price="'+card.price+'" />\
<td id="price_'+item_id+'">'+format_price(card.price)+'</td><td class="status_'+item_id+'"></td></tr>'))
$J('.card_amount').on('change', handleChange);
calculate_total()
}
fetch_item_price = function(item_id) {
$J.get(price_url + item_id, function(json) {
if(json.success != 1) {
console.log('was not a success at fetch_item_price =', item_id)
return false
}
price = +(json.lowest_sell_order)
if(price < 5) {
console.log('price was lower than 0.005? at fetch_item_price =', item_id, 'price =', price)
return false
}
price += 10
price_format = json.price_prefix + '{}' + json.price_suffix
cards[item_id].price = price
append_to_table(item_id)
}).fail(function() {
console.log('error fetch_item_price =', item_id)
alert('error')
return false
})
}
is_already_buying = function(data) {
return data.indexOf('CancelMarketBuyOrder') != -1
}
fetch_item = function(url) {
$J.get(url, function(data) {
if(is_already_buying(data)) {
console.log('already buying url =', url)
return false
}
item_id = +(data.split('Market_LoadOrderSpread( ').pop().split(' );').shift())
hash_name = data.split('"market_hash_name":"').pop().split('",').shift()
name = data.split('market_listing_item_name" style="">').pop().split('</').shift()
cards[item_id] = {'url': url, 'price': false, 'name': name,'hash_name': hash_name, 'quantity': 1, 'item_id': item_id, 'status': 0, 'orderid': -1, 'checks': 0, 'bought': 0}
$J('.found').text('Fetched: '+Object.keys(cards).length+'/' + $found.length)
fetch_item_price(item_id)
}).fail(function() {
//alert('error on fetch_item url = ' + url)
})
}
$J('#buy_button').click(function() {
$J(this).hide()
buy_card()
})
$J('#plus5').click(function() {
$J('.card_amount').val(5).change()
})
update_currency(function(c) {
currency = c['wallet_currency'];
country = c['wallet_country'];
$J('.wallet').text('Wallet Balance: ' + +(c['wallet_balance']) / 100)
$J('.found').text('Fetched: 0/' + $found.length)
$J.each($found, function(i, e) {
fetch_item($J(e).attr('href'))
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment