// ==UserScript==
// @name Steam.Supply
// @version 0.3
// @downloadURL https://steam.supply/dist/dl/steamsupply.user.js
// @updateURL https://steam.supply/dist/dl/steamsupply.user.js
// @icon https://steam.Supply/dist/img/ss.png
// @namespace https://steam.supply
// @description Displays Selling & Buying rates of steam trades :) more features in the future probably
// @author NotV
// @grant GM.getValue
// @grant GM.setValue
// @grant GM.deleteValue
// @grant GM_xmlhttpRequest
// @match http://*.steamcommunity.com/tradeoffer*
// @match https://*.steamcommunity.com/tradeoffer*
// @match http://*.steam.supply/*
// @match https://*.steam.supply/*
// ==/UserScript==
var ssapikey = '';
async function main() {
try {
ssapikey = await GM.getValue('ssapikey', '');
} catch (e) {
//Not sure if this is needed but noticed on firefox GM.getValue threw execeptions in other scripts and i do intend to port this to firefox
}
if (window.location.host === "steam.supply") {
if (window.location.pathname.includes("/Userscript")) {
ssapikey = document.getElementById("myssapikey").innerHTML;
GM.setValue('ssapikey', ssapikey);
}
}
if (window.location.host === "steamcommunity.com") {
if (window.location.pathname.includes("/tradeoffer") && !window.location.pathname.includes("/tradeoffer/new/")) {
var y = document.getElementsByClassName('offerheader');
if (!ssapikey) {
y[0].insertAdjacentHTML('beforeend', '
');
} else {
y[0].insertAdjacentHTML('beforeend', '
Check offer via Steam.Supply
');
if (document.getElementById("co")) {
document.getElementById("co").addEventListener("click", checkReady);
}
}
} else if (window.location.pathname.includes("/tradeoffer/new/") && window.location.search.includes("partner=58105264")) {
let url = new URL(document.URL);
let note = url.searchParams.get("ssnote");
if (note) {
document.getElementById('trade_offer_note').value = note;
}
let coupon = url.searchParams.get("sscoupon");
if (coupon) {
document.getElementById('inventory_select_their_inventory').click();
requestCoupon(coupon);
}
}
}
}
function checkRate() {
document.getElementById('thisrate').innerHTML = "Inventory is loading....";
let sendmine = {};
let sendtheres = {};
let mine = document.querySelectorAll('#your_slots .trade_slot .item');
let theres = document.querySelectorAll('#their_slots .trade_slot .item');
let myarray = [...mine];
myarray.forEach(div => {
if (!div.rgItem.market_hash_name) {
throw 'Inventories not loaded is there a better way to check? cLoadsInFlight don\'t seem to matter...';
}
let appid = div.rgItem.appid == 753 ? div.rgItem.market_fee_app : div.rgItem.appid; //probably verify how many items this works with.
if (!sendmine[div.rgItem.appid]) {
sendmine[div.rgItem.appid] = {};
}
if (!sendmine[div.rgItem.appid][div.rgItem.contextid]) {
sendmine[div.rgItem.appid][div.rgItem.contextid] = {};
}
if (!sendmine[div.rgItem.appid][div.rgItem.contextid][appid]) {
sendmine[div.rgItem.appid][div.rgItem.contextid][appid] = {};
}
if (!sendmine[div.rgItem.appid][div.rgItem.contextid][appid][div.rgItem.market_hash_name]) {
sendmine[div.rgItem.appid][div.rgItem.contextid][appid][div.rgItem.market_hash_name] = parseInt(div.rgItem.amount);
} else {
sendmine[div.rgItem.appid][div.rgItem.contextid][appid][div.rgItem.market_hash_name] = sendmine[div.rgItem.appid][div.rgItem.contextid][appid][div.rgItem.market_hash_name] + parseInt(div.rgItem.amount);
}
});
let therearray = [...theres];
therearray.forEach(div => {
if (!div.rgItem.market_hash_name) {
throw 'Inventories not loaded is there a better way to check? cLoadsInFlight don\'t seem to matter...';
}
let appid = div.rgItem.appid == 753 ? div.rgItem.market_fee_app : div.rgItem.appid; //probably verify how many items this works with.
if (!sendtheres[div.rgItem.appid]) {
sendtheres[div.rgItem.appid] = {};
}
if (!sendtheres[div.rgItem.appid][div.rgItem.contextid]) {
sendtheres[div.rgItem.appid][div.rgItem.contextid] = {};
}
if (!sendtheres[div.rgItem.appid][div.rgItem.contextid][appid]) {
sendtheres[div.rgItem.appid][div.rgItem.contextid][appid] = {};
}
if (!sendtheres[div.rgItem.appid][div.rgItem.contextid][appid][div.rgItem.market_hash_name]) {
sendtheres[div.rgItem.appid][div.rgItem.contextid][appid][div.rgItem.market_hash_name] = parseInt(div.rgItem.amount);
} else {
sendtheres[div.rgItem.appid][div.rgItem.contextid][appid][div.rgItem.market_hash_name] = sendtheres[div.rgItem.appid][div.rgItem.contextid][appid][div.rgItem.market_hash_name] + parseInt(div.rgItem.amount);
}
});
let y = document.getElementById('thisrate');
//Check if its trading for items before trying
if (!Object.keys(sendmine).length || !Object.keys(sendtheres).length) {
y.innerHTML = "Not a trade we can validate :)";
return;
}
document.getElementById('thisrate').innerHTML = "Checking rate....";
GM_xmlhttpRequest({
method: "POST",
url: "https://steam.supply/API/" + ssapikey + "/getrate",
data: "steamid=" + unsafeWindow.g_ulTradePartnerSteamID + "&mine=" + encodeURIComponent(JSON.stringify(sendmine)) + "&them=" + encodeURIComponent(JSON.stringify(sendtheres)),
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
onload: function(response) {
y.innerHTML = JSON.parse(response.responseText).html;
}
});
}
function requestCoupon(id) {
try {
document.getElementById('appselect_option_them_753_3').click();
let inv = unsafeWindow.UserThem.rgContexts[753][3].inventory.rgInventory;
Object.keys(inv).forEach(function(item) {
if (inv[item].id == id) {
unsafeWindow.MoveItemToTrade(inv[item].element);
return true; //lets save resource and end loop
}
});
} catch (e) {
//Inventory not loaded
setTimeout(function() {
requestCoupon(id);
}, 1500)
}
}
function checkReady() {
try {
checkRate();
} catch (e) {
window.setTimeout(checkReady, 1500);
}
}
main();