Skip to content

Commit

Permalink
Merge pull request #4 from Step7750/fix/listinginfo
Browse files Browse the repository at this point in the history
Fixes Listing Info Retrieval Handling when Slow
  • Loading branch information
Step7750 authored Jan 31, 2017
2 parents c4760d5 + 916fb24 commit 5b0ff4d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
web-ext-artifacts/
.idea
52 changes: 32 additions & 20 deletions float.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,31 @@ let floatQueue = [];
let floatData = {};
let floatTimer;
let steamListingInfo = {};
let listingInfoPromises = [];

// retrieve g_rgListingInfo from page script
window.addEventListener('message', (e) => {
if (e.data.type == 'listingInfo') {
steamListingInfo = e.data.listingInfo;

// resolve listingInfoPromises
for (let promise of listingInfoPromises) promise(steamListingInfo);

listingInfoPromises = [];
}
});

const retrieveListingInfoFromPage = function(listingId) {
if (listingId != null && (listingId in steamListingInfo)) {
return Promise.resolve(steamListingInfo[listingId]);
return Promise.resolve(steamListingInfo);
}

window.postMessage({
type: 'requestListingInfo'
}, '*');

return new Promise((resolve, reject) => {
setTimeout(() => {
resolve((listingId != null) ? steamListingInfo[listingId] : steamListingInfo);
}, 0);
listingInfoPromises.push(resolve);
});
};

Expand All @@ -33,24 +37,30 @@ const getFloatData = function(listingId, inspectLink) {

return fetch(`https://api.csgofloat.com:1738/?url=${inspectLink}`)
.then((response) => {
if (response.ok) { return response.json(); }
if (response.ok) return response.json();
return response.json().then((err) => { throw err; });
});
};

const showFloatText = function(listingId) {
const showFloat = function(listingId) {
let itemInfo = floatData[listingId];

if (itemInfo) setFloatText(listingId, `Float: ${itemInfo.floatvalue}<br>Paint Seed: ${itemInfo.paintseed}`, true);
};

const setFloatText = function(listingId, text, removeButton) {
let floatDiv = document.querySelector(`#item_${listingId}_floatdiv`);

if (floatDiv) {
// Remove the "get float" button
let floatButton = floatDiv.querySelector('.floatbutton');
if (floatButton) { floatDiv.removeChild(floatButton); }

let itemInfo = floatData[listingId];
if (removeButton) {
// Remove the "get float" button
let floatButton = floatDiv.querySelector('.floatbutton');
if (floatButton) { floatDiv.removeChild(floatButton); }
}

// Show the float and paint seed to the user
// Show the text to the user
let msgdiv = floatDiv.querySelector('.floatmessage');
msgdiv.innerHTML = `Float: ${itemInfo.floatvalue}<br>Paint Seed: ${itemInfo.paintseed}`;
msgdiv.innerHTML = text;
}
};

Expand All @@ -73,11 +83,9 @@ const processFloatQueue = function() {

getFloatData(lastItem.listingId, lastItem.inspectLink)
.then((data) => {
let itemInfo = data.iteminfo;

floatData[lastItem.listingId] = itemInfo;
floatData[lastItem.listingId] = data.iteminfo;

showFloatText(lastItem.listingId);
showFloat(lastItem.listingId);

processFloatQueue();
})
Expand Down Expand Up @@ -149,7 +157,11 @@ const getFloatButtonClicked = function(e) {
let id = row.id.replace('listing_', '');

retrieveListingInfoFromPage(id)
.then((listingData) => {
.then((steamListingData) => {
let listingData = steamListingData[id];

if (!listingData) return;

let inspectLink = listingData.asset.market_actions[0].link
.replace('%listingid%', id)
.replace('%assetid%', listingData.asset.id);
Expand Down Expand Up @@ -193,7 +205,7 @@ const addButtons = function() {

// check if we already have the float for this item
if (id in floatData) {
showFloatText(id);
showFloat(id);
}
}

Expand Down Expand Up @@ -223,5 +235,5 @@ floatTimer = setInterval(() => { addButtons(); }, 500);
processFloatQueue();

const logStyle = 'background: #222; color: #fff;';
console.log('%c CSGOFloat Market Checker (v1.1.0) by Step7750 ', logStyle);
console.log('%c CSGOFloat Market Checker (v1.1.1) by Step7750 ', logStyle);
console.log('%c Changelog can be found here: https://github.com/Step7750/CSGOFloat-Extension ', logStyle);
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "CSGOFloat Market Checker",
"short_name": "CSGOFloat",
"version": "1.1.0",
"version": "1.1.1",
"description": "Dedicated API for fetching the float value and paint seed of CSGO items on the market",
"icons": {
"16": "icons/16.png",
Expand Down

0 comments on commit 5b0ff4d

Please sign in to comment.