89 lines
No EOL
4.5 KiB
JavaScript
89 lines
No EOL
4.5 KiB
JavaScript
chrome.runtime.onMessage.addListener((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");
|
|
var selectedTab = getTabselText();
|
|
console.log("Selected tab:", selectedTab);
|
|
if (selectedTab === "Summary")
|
|
{
|
|
var relatedHorses = Array.from(document.querySelectorAll('.pedigree a')).map(link => link.href);
|
|
var response = setHorsePedigreeAPI(request.data.id, relatedHorses);
|
|
console.log("Response from API:", response);
|
|
sendResponse({ success: true, data: response });
|
|
}
|
|
else if (selectedTab === "Training")
|
|
{
|
|
var training = document.querySelector('.top:nth-child(8)') ? document.querySelector('.top:nth-child(8)').innerText : document.querySelector('.top:nth-child(6)').innerText;
|
|
var response = setHorseTrainingAPI(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();
|
|
|
|
var response = setHorseGeneticsAPI(request.data.id, colorGenetics);
|
|
console.log("Response from API:", response);
|
|
sendResponse({ success: true, data: response });
|
|
}
|
|
else if (selectedTab === "Achievements")
|
|
{
|
|
var genetics = Array.from(document.querySelectorAll('.genetic_stats')).map(x => x.innerText).filter(text => /poor|below average|average|good|very good/i.test(text));
|
|
var response = setHorseGeneticsAPI(request.data.id, genetics);
|
|
console.log("Response from API:", response);
|
|
}
|
|
else if (selectedTab === "Health")
|
|
{
|
|
var health = [...document.querySelector("#tab_health2 p").innerText.matchAll(/:\s*(\w+)/g)].map(match => match[1]);
|
|
var 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');
|
|
if (tabElement) {
|
|
return tabElement.textContent.trim();
|
|
}
|
|
return null;
|
|
} |