chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => { if (request.action === "getHorseBasicData") { try { console.log("Getting horse data"); const idString = document.querySelector('.right:nth-child(2)').innerText.replace('#', ''); // Convert the ID to BigInt const id = BigInt(idString); // Converts to BigInt, equivalent to ulong in C# // Convert BigInt to string before sending const idStringForResponse = id.toString(); // Convert to string to prevent serialization issues const horseData = { id: idStringForResponse || "", // Stelle sicher, dass die ID vorhanden ist age: parseInt(document.querySelector('.right:nth-child(6)').innerText, 10) || 0, // Alter muss eine Zahl sein name: document.title.replace(/ - Horse Reality.*$/, '') || "Unknown", // Name darf nicht leer sein gender: document.querySelector('img.icon16')?.alt || "Unknown", // Geschlecht prüfen breed: document.querySelector('.right:nth-child(4)')?.innerText || "Unknown", // Rasse prüfen link: window.location.href || "" // Link sicherstellen }; console.log("Horse data gathered:", horseData); // Sending response back to background script sendResponse({ success: true, data: horseData }); } catch (error) { console.error("Error gathering horse data: " + error.message); sendResponse({ success: false, message: "Error gathering horse data: " + error.message }); } } else if (request.action === "getHorseCurrentData") { console.log("Getting current horse selected tab data"); const selectedTab = getTabselText(); console.log("Selected tab:", selectedTab); if (selectedTab === "Summary") { let relatedHorses = Array.from(document.querySelectorAll('.pedigree a')).map(link => link.href); let response = await setHorsePedigreeAPIAsync(request.data.id, relatedHorses); console.log("Response from API:", response); sendResponse({ success: true, data: response }); } else if (selectedTab === "Training") { let training = document.querySelector('.top:nth-child(8)') ? document.querySelector('.top:nth-child(8)').innerText : document.querySelector('.top:nth-child(6)').innerText; let response = await setHorseTrainingAPIAsync(request.data.id, training); console.log("Response from API:", response); sendResponse({ success: true, data: response }); } else if (selectedTab === "Genetics") { const colorGenetics = Array.from(document.querySelectorAll('.genetic_result')).map(x => x.innerText); const geneticPotential = Array.from(document.querySelectorAll('.genetic_stats')) .map(x => x.innerText.trim()) .filter(value => !isNaN(value) && value !== "") .map(value => parseFloat(value)); const totalGeneticPotential = (Array.from(document.querySelectorAll('.top div')).find(el => el.innerText.includes('GP'))?.innerText || "").replace("GP total: ", "").trim(); let response = setHorseGeneticsAPI(request.data.id, colorGenetics); console.log("Response from API:", response); sendResponse({ success: true, data: response }); } else if (selectedTab === "Achievements") { let genetics = Array.from(document.querySelectorAll('.genetic_stats')).map(x => x.innerText).filter(text => /poor|below average|average|good|very good/i.test(text)); // Walk, Trot, Canter, Gallop, Posture... let response = setHorseAchievementsAPI(request.data.id, genetics); // Second part of the achievements tab needed for show results // Placeholder let response2 = setHorseConformationAPI(request.data.id, { }); console.log("Response from API:", response); } else if (selectedTab === "Health") { let health = [...document.querySelector("#tab_health2 p").innerText.matchAll(/:\s*(\w+)/g)].map(match => match[1]); let response = setHorseHealthAPI(request.data.id, health); console.log("Response from API:", response); } else { console.error("Unknown or no tab selected."); } } return true; // This is necessary to allow asynchronous response }); function getTabselText() { const tabElement = document.querySelector('div.tabsel').textContent?.trim(); return tabElement; }