Files
grepodata/Grepolis_Data_Sender.user.js

117 lines
4.4 KiB
JavaScript

// ==UserScript==
// @name Grepolis Data Sender (Deep Scan v5.2 — Gold Sell Tab & Town Snapshot)
// @namespace http://tampermonkey.net/
// @version 5.2
// @description Sends town stats always, and Premium Exchange gold market from "Sell resources for gold" tab only
// @author Dimitrios
// @match https://*.grepolis.com/game/*
// @grant unsafeWindow
// @updateURL https://git.haunter-pets.top/haunter/grepodata/raw/branch/main/Grepolis_Data_Sender.user.js
// @downloadURL https://git.haunter-pets.top/haunter/grepodata/raw/branch/main/Grepolis_Data_Sender.user.js
// ==/UserScript==
(function () {
'use strict';
console.log("💰 DeepScan v5.2 loaded — Town stats always, Premium gold tab if active");
const uw = typeof unsafeWindow !== "undefined" ? unsafeWindow : window;
let latestBasicPayload = null;
// 💱 Extract data ONLY from "Sell resources for gold" tab
function scrapeGoldMarketExchange() {
const exchange = {};
const resourceTypes = ["wood", "stone", "iron"];
const isSellTabActive =
document.querySelector("#premium_exchange .gp_page_caption.js-page-caption-1.active") &&
document.querySelector("#premium_exchange .gp_tab_page.js-page-1.active");
if (!isSellTabActive) {
console.warn("🚫 Sell tab not active — skipping gold extraction");
return null;
}
const blocks = document.querySelectorAll(
"#premium_exchange .gp_tab_page.js-page-1.active .resources_wrapper .resource"
);
blocks.forEach((block, i) => {
const type = resourceTypes[i] || `res_${i}`;
const currentEl = block.querySelector(".progressbar_bg .caption span.current");
const maxEl = block.querySelector(".progressbar_bg .caption span.max");
exchange[type] = {
current: currentEl?.textContent.trim() || "0",
max: maxEl?.textContent.trim() || "0"
};
});
console.log("📊 Gold Market Sell Tab data:", exchange);
return exchange;
}
// 📦 Send all town stats — always
function sendBasicStats() {
try {
const towns = uw.ITowns?.towns || {};
const player = uw.Game?.player_name || "unknown";
const player_id = uw.Game?.player_id || "unknown";
const stats = Object.values(towns).map(town => {
const res = town.resources();
const buildings = town.buildings()?.attributes ?? {};
const marketLevel = typeof town.getMarketplaceLevel === "function"
? town.getMarketplaceLevel()
: buildings.market || null;
return {
town_id: town.id,
town_name: town.name,
wood: res.wood,
stone: res.stone,
iron: res.iron,
population: res.population,
points: town.getPoints(),
buildings,
market: { level: marketLevel }
};
});
const goldMarket = scrapeGoldMarketExchange(); // may return null
latestBasicPayload = {
type: "basic",
timestamp: new Date().toISOString(),
player,
player_id,
towns: stats,
gold_market: goldMarket || {} // always include field
};
fetch("https://grepo.haunter-pets.top/api/grepolis-data", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(latestBasicPayload)
})
.then(res => res.text())
.then(txt => console.log("✅ Data sent:", txt))
.catch(err => console.error("❌ Failed to send:", err));
} catch (e) {
console.error("💥 Error sending data:", e);
}
}
// ⏱️ Timer: always sends town info; adds gold data only if Sell tab active
function runDataSendInterval() {
setInterval(() => {
console.log("🕒 Snapshot triggered — scanning town and gold sell tab...");
sendBasicStats();
}, 30000);
}
window.addEventListener("load", () => {
console.log("🚀 Grepolis page ready — DeepScan v5.2 booting...");
runDataSendInterval();
});
})();