44 lines
1.4 KiB
JavaScript
44 lines
1.4 KiB
JavaScript
// ================================================================
|
|
// 00_config.js — Shared constants and utility helpers
|
|
// Runs first; everything here is available to all other modules.
|
|
// ================================================================
|
|
|
|
const uw = typeof unsafeWindow !== 'undefined' ? unsafeWindow : window;
|
|
const BASE_URL = 'https://grepo.haunter-pets.top';
|
|
|
|
// Read the clan key injected by the Loader before eval()
|
|
const CLAN_KEY = window.__GRC_CLAN_KEY || '';
|
|
|
|
// Returns a random integer between min and max (inclusive)
|
|
function randInt(min, max) {
|
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
}
|
|
|
|
// Schedules fn to run after a random ms delay, then reschedules itself
|
|
function jitterLoop(fn, minMs, maxMs) {
|
|
function schedule() {
|
|
setTimeout(async () => {
|
|
await fn();
|
|
schedule();
|
|
}, randInt(minMs, maxMs));
|
|
}
|
|
schedule();
|
|
}
|
|
|
|
function log(msg) {
|
|
console.log(`[GRC] ${msg}`);
|
|
}
|
|
|
|
function sleep(ms) {
|
|
return new Promise(r => setTimeout(r, ms));
|
|
}
|
|
|
|
// Wrapper around fetch() that automatically injects the X-Clan-Key header
|
|
// on every request. Use this instead of fetch() everywhere in the bot.
|
|
function apiFetch(url, options) {
|
|
options = options || {};
|
|
options.headers = options.headers || {};
|
|
options.headers['X-Clan-Key'] = CLAN_KEY;
|
|
return fetch(url, options);
|
|
}
|