Skip to content

Instantly share code, notes, and snippets.

@cwparsons
Last active August 29, 2015 13:56
Show Gist options
  • Save cwparsons/9166207 to your computer and use it in GitHub Desktop.
Save cwparsons/9166207 to your computer and use it in GitHub Desktop.
Helps speed up the process of selling cards on Steam by calculating the average price over the past week.
javascript:(function()%7B(function(g%2Cl%2Cm%2Cb)%7Bfunction%20n(a)%7Bif(a.responseJSON%26%26a.responseJSON.prices)%7Ba%3Da.responseJSON.prices%3Bfor(var%20d%3D0%2Ce%3D0%2Cc%3Da.length-1%2Cb%3Dh.hours%3B0%3C%3Dc%26%260%3C%3Db%3Bc--%2Cb--)%7Bvar%20f%3DparseInt(a%5Bc%5D%5B2%5D.replace(%22%20sold%22%2C%22%22))%3B0!%3D%3Df%26%26(e%2B%3Df%2Cd%2B%3Da%5Bc%5D%5B1%5D*f)%7Da%3DMath.ceil(d%2Fe*100*h.taxes)%2F100%3Bg(%22market_sell_buyercurrency_input%22).value%3D%22%24%22%2Ba%3Bm.OnBuyerPriceInputKeyUp()%3Bg(%22market_sell_dialog_accept_ssa%22).checked%3D%22checked%22%7Delse%20k(a)%7Dfunction%20k(a)%7Bconsole.log(%22Failure%20to%20get%20request%22%2Ca)%7Dvar%20h%3D%7Btaxes%3A1.1%2Chours%3A168%7D%3Bdocument.querySelectorAll(%22.item_market_action_button%22)%5B0%5D.click()%3BsetTimeout(function()%7Bnew%20l.Request(%22http%3A%2F%2Fsteamcommunity.com%2Fmarket%2Fpricehistory%2F%22%2C%7Bmethod%3A%22get%22%2Cparameters%3A%7Bappid%3Ab.appid%2Cmarket_hash_name%3Ab.selectedItem.market_hash_name%7D%2ConSuccess%3An%2ConFailure%3Ak%7D)%7D%2C250)%7D)(%24%2CAjax%2CSellItemDialog%2Cg_ActiveInventory)%7D)()
// Bookmarklet to help sell cards on Steam
// http://steamcommunity.com/id/{username}/inventory
(function($, Ajax, SellItemDialog, g_ActiveInventory) {
var config = {
taxes: 1.1,
hours: 24 * 7
};
// Click on the sell button for a card.
var buttonSell = document.querySelectorAll('.item_market_action_button')[0];
buttonSell.click();
// Waiting a half second for the initial graph to load.
// This may not be necessary.
setTimeout(sendRequest, 250);
function sellPrice(prices) {
var totalCost = 0,
sales = 0;
// `prices` is made up of the last month of sales by the hour,
// then the last few months of sales by the day. Here, we just average
// the cost over the past week (starting from the end of the array).
for (var i = prices.length - 1, j = config.hours; i >= 0 && j >= 0; i--, j--) {
var sold = parseInt(prices[i][2].replace(' sold', ''));
if (sold !== 0) {
sales += sold;
totalCost += prices[i][1] * sold;
}
}
// Round to the nearest second decimal place, while also adding in our tax.
return Math.ceil(totalCost / sales * 100 * config.taxes) / 100;
}
function sendRequest() {
var appId = g_ActiveInventory.appid,
marketHashName = g_ActiveInventory.selectedItem.market_hash_name;
new Ajax.Request( 'http://steamcommunity.com/market/pricehistory/', {
method: 'get',
parameters: {
appid: appId,
market_hash_name: marketHashName
},
onSuccess: onSuccess,
onFailure: onFailure
});
}
function onSuccess (transport) {
if (transport.responseJSON && transport.responseJSON.prices) {
// Get the sell price.
var price = sellPrice(transport.responseJSON.prices);
// Fill in the buyer pays input.
$('market_sell_buyercurrency_input').value = '$' + price;
// Force the re-check on the you receive input.
SellItemDialog.OnBuyerPriceInputKeyUp();
// Check the agreement checkbox
$('market_sell_dialog_accept_ssa').checked = 'checked';
} else {
onFailure(transport);
}
}
function onFailure (transport) {
console.log('Failure to get request', transport);
}
})($, Ajax, SellItemDialog, g_ActiveInventory);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment