importScripts("./API.js"); chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { if (message.action === "updateHorseData") { updateHorseButton(); // Call the function } }); async function updateHorseButton() { console.log("Updating horse data"); // Query the active tab chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) { if (tabs.length === 0) { console.error("No active tab found."); return; } const activeTabId = tabs[0].id; // First: Get horse basic data chrome.tabs.sendMessage( activeTabId, { action: "getHorseBasicData" }, async function (response) { if (!response || !response.success) { console.error("Failed to get horse data from content script."); return; } console.log("Horse data received:", response.data); const horseData = response.data; // Check if horse exists via API const existingHorse = await getHorseAPI(horseData.id); if (!existingHorse || existingHorse.message === "Horse not found") { console.warn("Horse not found in the API."); return; } // Update horse data in the API await setBaseDataHorseAPI(horseData.id, horseData); // Second: Get horse current data chrome.tabs.sendMessage(activeTabId, { action: "getHorseCurrentData", data: { id: horseData.id } }, function (currentDataResponse) { if (!currentDataResponse) { console.error("Failed to get current horse data."); return; } console.log("Current horse data received:", currentDataResponse); } ); } ); }); }