diff --git a/extension/changelog.json b/extension/changelog.json index 394d097d0..3967bbfd8 100644 --- a/extension/changelog.json +++ b/extension/changelog.json @@ -9,7 +9,8 @@ { "message": "Display when Live Networth was last updated after 1 hour global cache.", "contributor": "Kwack" }, { "message": "Fix stats estimates on the attack page not showing up.", "contributor": "DeKleineKobini" }, { "message": "Fix weapon experience on the attack page not showing up.", "contributor": "DeKleineKobini" }, - { "message": "Fix fair fight modifier on the attack page not showing up.", "contributor": "DeKleineKobini" } + { "message": "Fix fair fight modifier on the attack page not showing up.", "contributor": "DeKleineKobini" }, + { "message": "Fix Popup Market Search for Torn v2 API.", "contributor": "TheFoxMan" } ], "changes": [ { "message": "Adapt cheap item highlights to work with the item market overhaul.", "contributor": "DeKleineKobini" }, diff --git a/extension/pages/popup/popup.js b/extension/pages/popup/popup.js index 6a285df4b..ac221da2e 100644 --- a/extension/pages/popup/popup.js +++ b/extension/pages/popup/popup.js @@ -740,10 +740,22 @@ async function setupMarketSearch() { const viewItem = document.find("#market #item-information"); viewItem.find(".market").classList.add("tt-hidden"); + const item = torndata.items[id]; + viewItem.find(".circulation").textContent = formatNumber(item.circulation); + viewItem.find(".value").textContent = `$${formatNumber(item.market_value)}`; + viewItem.find(".name").textContent = item.name; + viewItem.find(".name").href = `https://www.torn.com/imarket.php#/p=shop&step=shop&type=&searchname=${item.name}`; + viewItem.find(".image").src = item.image; + + viewItem.classList.remove("tt-hidden"); + + showLoadingPlaceholder(viewItem.find(".market").parentElement, true); + if (ttCache.hasValue("livePrice", id)) { handleMarket(ttCache.get("livePrice", id)); + showLoadingPlaceholder(viewItem.find(".market").parentElement, false); } else { - fetchData("torn", { section: "market", id, selections: ["bazaar", "itemmarket"] }) + fetchData("tornv2", { section: "market", id, selections: ["bazaar", "itemmarket"] }) .then((result) => { handleMarket(result); @@ -751,39 +763,26 @@ async function setupMarketSearch() { }) .catch((error) => { document.find(".error").classList.remove("tt-hidden"); - document.find(".error").textContent = error.error; - }); + document.find(".error").textContent = error.message; + }).finally(() => showLoadingPlaceholder(viewItem.find(".market").parentElement, false)); } - const item = torndata.items[id]; - viewItem.find(".circulation").textContent = formatNumber(item.circulation); - viewItem.find(".value").textContent = `$${formatNumber(item.market_value)}`; - viewItem.find(".name").textContent = item.name; - viewItem.find(".name").href = `https://www.torn.com/imarket.php#/p=shop&step=shop&type=&searchname=${item.name}`; - viewItem.find(".image").src = item.image; - - viewItem.classList.remove("tt-hidden"); - function handleMarket(result) { const list = viewItem.find(".market"); list.innerHTML = ""; let found = false; - for (const type of Object.keys(result)) { - let text; - if (type === "itemmarket") text = "Item Market"; - else text = capitalizeText(type); - - const wrap = document.newElement({ type: "div" }); - - wrap.appendChild(document.newElement({ type: "h4", text })); - - if (result[type]) { - found = true; - - for (const item of result[type].slice(0, 3)) { - wrap.appendChild( + if (!isSellable(id) && !result.bazaar && !result.itemmarket.listings.length) { + list.classList.add("untradable"); + list.innerHTML = "Item is not sellable!"; + } else { + // Bazaar listings. + const bazaarWrap = document.newElement({ type: "div" }); + bazaarWrap.appendChild(document.newElement({ type: "h4", text: "Bazaar" })); + if (result.bazaar) { + for (const item of result.bazaar.slice(0, 3)) { + bazaarWrap.appendChild( document.newElement({ type: "div", class: "price", @@ -792,21 +791,39 @@ async function setupMarketSearch() { ); } } else { - wrap.appendChild( + bazaarWrap.appendChild( document.newElement({ type: "div", class: "price no-price", - text: "No price found.", + text: "No listings found.", }) ); } - - list.appendChild(wrap); - } - - if (!isSellable(id) && !found) { - list.classList.add("untradable"); - list.innerHTML = "Item is not sellable!"; + list.appendChild(bazaarWrap); + + // Item market listings. + const itemMarketWrap = document.newElement({ type: "div" }); + itemMarketWrap.appendChild(document.newElement({ type: "h4", text: "Item Market" })); + if (result.itemmarket?.listings?.length) { + for (const item of result.itemmarket.listings.slice(0, 3)) { + itemMarketWrap.appendChild( + document.newElement({ + type: "div", + class: "price", + text: `${item.amount}x | $${formatNumber(item.price)}`, + }) + ); + } + } else { + itemMarketWrap.appendChild( + document.newElement({ + type: "div", + class: "price no-price", + text: "No listings found.", + }) + ); + } + list.appendChild(itemMarketWrap); } viewItem.find(".market").classList.remove("tt-hidden"); } diff --git a/extension/scripts/global/functions/torn.js b/extension/scripts/global/functions/torn.js index 9b88f44ff..a8b3a86e2 100644 --- a/extension/scripts/global/functions/torn.js +++ b/extension/scripts/global/functions/torn.js @@ -1462,7 +1462,7 @@ function isSellable(id) { 1009, // Halloween Basket 1010, // Halloween Basket 1011, // Halloween Basket - ].includes(item.id) + ].includes(item.id || parseInt(id)) ); }