From c414bc4b2f835ead349cf52dd56697aec427bb5d Mon Sep 17 00:00:00 2001 From: Nathan Date: Wed, 1 May 2024 00:32:56 +0000 Subject: [PATCH] Fix index requests --- web/bookmarklet-iframe.html | 38 ++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/web/bookmarklet-iframe.html b/web/bookmarklet-iframe.html index 4d06c422..0b3b1947 100644 --- a/web/bookmarklet-iframe.html +++ b/web/bookmarklet-iframe.html @@ -16,6 +16,14 @@ let lastSaveData = []; let saveData = []; + let index = null; + let isIndexReady = false; + fetch(`https://raw.githubusercontent.com/expitau/InfiniteCraftWiki/main/web/data/index.json`) + .then(res => res.json()) + .then(data => { + index = Object.fromEntries(Object.entries(data).map(x => [x[1][1], x[0]]));; + isIndexReady = true; + }); function sendDataToWiki(type, data = {}) { wikiTab.postMessage(JSON.stringify({ type, data }), `https://${window.location.host}`); @@ -43,21 +51,21 @@ return } - fetch(`https://raw.githubusercontent.com/expitau/InfiniteCraftWiki/main/web/data/index.json`) - .then(res => res.json()) - .then(indexData => { - let index = Object.fromEntries(Object.entries(indexData).map(x => [x[1][1], x[0]])); - saveData = JSON.parse(data.data).elements.map(a => index[a.text]).filter(x => x); - - if (saveData.join(",") !== lastSaveData.join(",")) { - console.log("Sending update", saveData, lastSaveData) - sendDataToWiki('update', saveData); - lastSaveData = saveData; - } else { - console.log('Save data matches!'); - } - - }); + if (!isIndexReady) { + setTimeout(() => { + processDataFromParent(data) + }, 1000); + return + } + saveData = JSON.parse(data.data).elements.map(a => index[a.text]).filter(x => x); + + if (saveData.join(",") !== lastSaveData.join(",")) { + console.log("Sending update", saveData, lastSaveData) + sendDataToWiki('update', saveData); + lastSaveData = saveData; + } else { + console.log('Save data matches!'); + } } // Heartbeat with wiki