Skip to content

Instantly share code, notes, and snippets.

@bmwalters
Last active March 18, 2020 12:34
Show Gist options
  • Save bmwalters/72dd493a248b7d4ff174def9a62bc828 to your computer and use it in GitHub Desktop.
Save bmwalters/72dd493a248b7d4ff174def9a62bc828 to your computer and use it in GitHub Desktop.
Displays the exact wear 'float value' of CS:GO items in Steam inventories.
// ==UserScript==
// @name Steam Inventory Float Value Displayer
// @description Displays the exact wear 'float value' of CS:GO items in Steam inventories.
// @namespace zerf
// @include http://steamcommunity.com/id/*/inventory*
// @include http://steamcommunity.com/profiles/*/inventory*
// @include https://steamcommunity.com/id/*/inventory*
// @include https://steamcommunity.com/profiles/*/inventory*
// @version 1.1
// @grant GM_xmlhttpRequest
// @grant GM_getValue
// @grant GM_setValue
// ==/UserScript==
let apikey = GM_getValue("steam_apikey");
let apiurl = "http://api.steampowered.com/IEconItems_730/GetPlayerItems/v0001";
let debug = false;
let log = (...msgs) => { console.log("[SIFV]", ...msgs); };
let dlog = (...msgs) => { if (debug) { log(...msgs); } }
let floatValueData = {};
// oddly enough, we don't have float_value data on anything
// in g_ActiveInventory, so we have to make an API call
let performAPICall = function() {
// g_ActiveUser is null in the first tick...
setTimeout(() => {
let currentSteamId = unsafeWindow.g_ActiveUser.strSteamId;
GM_xmlhttpRequest({
method: "GET",
url: `${apiurl}/?key=${apikey}&SteamID=${currentSteamId}`,
onload: function(response) {
let data = JSON.parse(response.responseText);
if (!(data && data.result && data.result.items)) {
log("Bad JSON received from Steam API!");
}
dlog(`Received ${data.result.items.length} items from the API`);
for (let item of data.result.items) {
if (!item.attributes) {
dlog(`Item ${item.id} has no attributes.`);
continue;
}
let wearAttribute = item.attributes.find((attr) => {
return attr.defindex == 8;
});
if (!wearAttribute) {
dlog(`Could not find wear attribute for item ${item.id}.`);
continue;
}
floatValueData[item.id] = wearAttribute.float_value;
}
dlog("Successfully fetched API float value data.");
}
});
}, 0);
}
let activeItemChanged = function(infoElement) {
let selectedItem = unsafeWindow.g_ActiveInventory.selectedItem;
if (!(selectedItem)) {
log("No active item!");
return;
}
dlog("Active item changed", selectedItem.id);
let wearValue = floatValueData[selectedItem.id];
if (wearValue == null) {
dlog("Active item float value data has not been loaded!");
return;
}
let floatValuePercent = ((1 - wearValue) * 100).toFixed(3);
// add float value element to inventory panel
let descriptors = infoElement.querySelector(".item_desc_descriptors");
if (!descriptors) {
log("Failed to grab item descriptor container element!");
return;
}
let exteriorDescriptor = Array.from(descriptors.children).find((item) => {
return item.innerText.indexOf("Exterior: ") !== -1;
});
if (exteriorDescriptor) {
let floatValueElement = document.createElement("span");
floatValueElement.innerText = ` (${floatValuePercent}%)`;
exteriorDescriptor.appendChild(floatValueElement);
} else {
exteriorDescriptor = document.createElement("div");
exteriorDescriptor.classList.add("descriptor");
descriptors.insertBefore(exteriorDescriptor, descriptors.firstChild);
let floatValueElement = document.createElement("span");
floatValueElement.innerText = `Exterior: ${floatValuePercent}%`;
exteriorDescriptor.appendChild(floatValueElement);
}
dlog("Appended float value element.");
}
let setupObserver = function() {
let inventoryRight = document.querySelector(".inventory_page_right");
if (!inventoryRight) {
log("Failed to grab inventory right page element!");
return;
}
let observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type != "childList") { return; }
if (!mutation.target.classList.contains("hover_item_name")) { return; }
// a descendant has been added to the right page
activeItemChanged(mutation.target.parentElement.parentElement);
});
});
observer.observe(inventoryRight, { childList: true, subtree: true });
}
let promptForAPIKey = function() {
let prompt_text = "Please enter your Steam web API key. "
+ "This can be generated at https://steamcommunity.com/dev/apikey";
let apikey = prompt(prompt_text);
if (apikey) {
GM_setValue("steam_apikey", apikey);
location.reload();
}
}
if (apikey) {
performAPICall();
setupObserver();
log("Loaded.");
} else {
log("User has no API key!");
promptForAPIKey();
}
@Gunjubasik
Copy link

Hello. Can you show me, please, how use script on steam market CSGO? Im finded one script on github - but i dont know where i should take this info - || -s -i [ -g ] .
P.s. Thnx for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment