From 41439f9f00882a7e9840a7bd6da5eb78f103ef73 Mon Sep 17 00:00:00 2001 From: Step7750 Date: Sun, 29 Jan 2017 14:18:00 -0700 Subject: [PATCH 1/3] Fixes Slow Listing Retrieval Delays the timeout for retrieving to 10ms. In the vast majority of cases, it will be retrieved in under 10ms. If it still takes longer, `retrieveListingInfoFromPage` rejects the promise. If the promise is rejected for a single listing, the listing text is updated (to prevent an infinite loop if they're on a new page). If the promise is rejected for 'Get all floats', it tries again. --- .gitignore | 1 + float.js | 49 ++++++++++++++++++++++++++++++++----------------- manifest.json | 2 +- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index 6311779b..df204e08 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ web-ext-artifacts/ +.idea diff --git a/float.js b/float.js index d8535699..45b75d53 100644 --- a/float.js +++ b/float.js @@ -15,14 +15,18 @@ const retrieveListingInfoFromPage = function(listingId) { return Promise.resolve(steamListingInfo[listingId]); } + // Reset, since this is a new page + steamListingInfo = {}; + window.postMessage({ type: 'requestListingInfo' }, '*'); return new Promise((resolve, reject) => { setTimeout(() => { - resolve((listingId != null) ? steamListingInfo[listingId] : steamListingInfo); - }, 0); + if (Object.keys(steamListingInfo).length === 0) reject(); + else resolve((listingId != null) ? steamListingInfo[listingId] : steamListingInfo); + }, 10); }); }; @@ -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}
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}
Paint Seed: ${itemInfo.paintseed}`; + msgdiv.innerHTML = text; } }; @@ -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(); }) @@ -112,6 +120,10 @@ const getAllFloats = function() { floatQueue.push({ listingId: id, inspectLink: inspectLink }); } + }, + () => { + // Try again + getAllFloats(); }); }; @@ -155,6 +167,9 @@ const getFloatButtonClicked = function(e) { .replace('%assetid%', listingData.asset.id); floatQueue.push({ listingId: id, inspectLink: inspectLink }); + }, + () => { + setFloatText(id, "Failed to obtain listing info, please try again"); }); }; @@ -193,7 +208,7 @@ const addButtons = function() { // check if we already have the float for this item if (id in floatData) { - showFloatText(id); + showFloat(id); } } @@ -223,5 +238,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); diff --git a/manifest.json b/manifest.json index 3fa70cb1..57774e20 100644 --- a/manifest.json +++ b/manifest.json @@ -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", From e87eea31506631438e888845b406cc84c2118387 Mon Sep 17 00:00:00 2001 From: Step7750 Date: Sun, 29 Jan 2017 14:24:24 -0700 Subject: [PATCH 2/3] Double Quotes -> Single Quotes Styling --- float.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/float.js b/float.js index 45b75d53..3bc011c1 100644 --- a/float.js +++ b/float.js @@ -169,7 +169,7 @@ const getFloatButtonClicked = function(e) { floatQueue.push({ listingId: id, inspectLink: inspectLink }); }, () => { - setFloatText(id, "Failed to obtain listing info, please try again"); + setFloatText(id, 'Failed to obtain listing info, please try again'); }); }; From 916fb248dba822109c145cca3879fd85e44f86c1 Mon Sep 17 00:00:00 2001 From: Step7750 Date: Mon, 30 Jan 2017 19:24:27 -0700 Subject: [PATCH 3/3] Stores and Resolves Promises Once Data is Fetched --- float.js | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/float.js b/float.js index 3bc011c1..ce022381 100644 --- a/float.js +++ b/float.js @@ -2,31 +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); } - // Reset, since this is a new page - steamListingInfo = {}; - window.postMessage({ type: 'requestListingInfo' }, '*'); return new Promise((resolve, reject) => { - setTimeout(() => { - if (Object.keys(steamListingInfo).length === 0) reject(); - else resolve((listingId != null) ? steamListingInfo[listingId] : steamListingInfo); - }, 10); + listingInfoPromises.push(resolve); }); }; @@ -120,10 +120,6 @@ const getAllFloats = function() { floatQueue.push({ listingId: id, inspectLink: inspectLink }); } - }, - () => { - // Try again - getAllFloats(); }); }; @@ -161,15 +157,16 @@ 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); floatQueue.push({ listingId: id, inspectLink: inspectLink }); - }, - () => { - setFloatText(id, 'Failed to obtain listing info, please try again'); }); };