Last active
January 15, 2022 21:02
-
-
Save korden32/0c7653718ed3350df7db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Steam Market History Export | |
// @author KorDen | |
// @version 0.2.3 | |
// @grant none | |
// @include *//steamcommunity.com/market/ | |
// ==/UserScript== | |
( function() { | |
$J('.pick_and_sell_button > .item_market_action_button').before('<a href="javascript:void(0)" class="item_market_action_button item_market_action_button_blue nodisable" onclick="exportMarketHistory();" id="exportHistoryBtn"><span class="item_market_action_button_edge item_market_action_button_left"></span><span class="item_market_action_button_contents" id="exportHistoryTxt">History Export</span><span class="item_market_action_button_edge item_market_action_button_right"></span><span class="item_market_action_button_preload"></span></a> '); | |
})(); | |
contentEval ( function get_http_s(url, callback) { | |
var http = new window.XMLHttpRequest(); | |
http.onreadystatechange = function () { | |
if (this.readyState == 4) | |
callback(this.responseText, this.status); | |
}; | |
http.open('GET', url, true); | |
http.send(null); | |
} ); | |
contentEval ( 'var csvData = "", history = "";' ); | |
contentEval ( function exportMarketHistory() { | |
var loops = 0; | |
var count = 500; | |
var i = 1; | |
var lastError = "L"; | |
var tableFooter='</div></div>'; | |
var current = 0, total = 0; | |
var months=['skipme','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']; | |
var curYear, curMonth = 13; | |
{ | |
var date = new Date(); | |
curYear = date.getFullYear(); | |
} | |
$J('#myListings > div').hide(); | |
$J('.market_tab_well_tab').attr('class','market_tab_well_tab market_tab_well_tab_inactive'); | |
if($J('#tabHistoryExport').length == 0) { | |
$J('#tabContentsMyMarketHistory').after('\ | |
<div id="tabHistoryExport" class="my_listing_section market_content_block">\ | |
<span class="my_market_header_active">History export tool</span>\ | |
<div id="tabHistoryExportContent">\ | |
<div class="my_listing_section market_content_block"><img src=//steamcommunity.com/public/images/login/throbber.gif style="position:relative;top:10px;"> <span id="hLoaderInfo">Loading info...</span></div>\ | |
</div>\ | |
</div>') | |
} else { | |
$J('#tabHistoryExport').show(); | |
} | |
get_http_s("//steamcommunity.com/market/myhistory/render/?query=&start=0&count=1&l=english", function (txt, status) { | |
if(status == 200) { | |
total = JSON.parse(txt)["total_count"]; | |
loops = Math.ceil(total / count); | |
$J('#tabHistoryExportContent').html('<br>\ | |
Total: ' + total + '<br>\ | |
<span id="hLoaderInfo"></span>'); | |
$J("#tabHistoryExportContent > .market_listing_table_header").hide(); | |
doExport(); | |
} | |
}); | |
function doExport() { | |
$J('#exportHistoryBtn').removeAttr('onclick'); | |
$J('#exportHistoryBtn').attr('class', 'item_market_action_button item_market_action_button_blue btn_disabled'); | |
csvData = "#;Item Type;Item;Price;Acted;Actor URL;Transaction ID\n"; | |
current = total + 1; | |
if (loops) { | |
processor(); | |
} else { | |
parseDone(); | |
} | |
} | |
function processJSON(data) { | |
market = '<div>' + data['results_html'] + '</div>'; | |
$J(market).find(".market_listing_row").each(function () { | |
current--; | |
var row = $J(this); | |
if(row.attr("id").indexOf("_event_") == -1) { | |
var transId = $J(this).attr("id").match(/history_row_(\d+)_/)[1]; | |
var type = row.find(".market_listing_game_name").text().replace(';','_'); | |
var item = row.find(".market_listing_item_name").text().replace('\n',''); | |
//var listed = row.find(".market_listing_listed_date:last").text().trim(); | |
var acted = row.find(".market_listing_listed_date:first").text().trim(); | |
var price = row.find(".market_listing_price").text(); | |
var gainorloss = row.find(".market_listing_gainorloss").text(); | |
//var actor = row.find(".market_listing_whoactedwith_name_block").text().trim(); | |
var actor_url = row.find(".market_listing_owner_avatar a").attr('href'); | |
acted = acted.split(' '); | |
var month = months.indexOf(acted[1]); | |
if(curMonth < month) | |
curYear--; | |
curMonth = month; | |
acted = acted[0] + '.' + month + '.' + curYear; | |
gainorloss = (gainorloss.trim() == '+' ? '-' : '+'); | |
price = price.trim().replace(/( pуб.|\$)/,''); | |
csvData += current + ';' + type + ';' + item + ';' + gainorloss+price.trim() + ';' + acted + ';' + actor_url + ';' + transId + '_;\n'; | |
} | |
}); | |
} | |
function parseDone() { | |
$J('#hLoaderInfo').html('History loaded. You can now export it to CSV.<br>\ | |
<a href="data:application/csv;charset=utf-8,' + encodeURIComponent(csvData) + '" class="btn_small btn_darkblue_white_innerfade" id="exportCSVBtn" download="markethistory.csv"><span>Export to CSV</span></a>'); | |
} | |
function processor() { | |
$J('#hLoaderInfo').html('<img src=//steamcommunity.com/public/images/login/throbber.gif style="position:relative;top:10px;"> ' +lastError + 'oading ' + i + ' of ' + loops + ' history blocks...'); | |
get_http_s("//steamcommunity.com/market/myhistory/render/?query=&l=english&start=" + (count * (i - 1)) + "&count=" + count, function (txt, status) { | |
if(status == 200) { | |
txt = txt.replace(/[ ]src=/g," data-src="); | |
var data = JSON.parse(txt); | |
if(!data || !data['results_html'] || data['results_html'].indexOf("There was an error", 48) >-1) { | |
i--; | |
lastError = 'No result, rel'; | |
} else { | |
lastError = 'L'; | |
processJSON(data); | |
} | |
} else { | |
i--; | |
lastError = 'Error ' + status + ', rel'; | |
} | |
if (i == loops) { | |
parseDone(); | |
} else { | |
i++; | |
processor(); | |
} | |
}); | |
} | |
} ); | |
function contentEval(source) { | |
var script = document.createElement('script'); | |
script.setAttribute("type", "application/javascript"); | |
script.textContent = source; | |
document.body.appendChild(script); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment