2026-01-24 17:45:54 +03:30
|
|
|
(function() {
|
|
|
|
|
'use strict';
|
|
|
|
|
//debugger
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Store config in window object
|
|
|
|
|
window.__APP_CONFIG__ = window.__APP_CONFIG__ || {};
|
|
|
|
|
window.__CONFIG_READY__ = false;
|
|
|
|
|
|
|
|
|
|
// Create a promise that will be resolved when config is loaded
|
|
|
|
|
let configResolve;
|
|
|
|
|
let configReject;
|
|
|
|
|
window.__CONFIG_PROMISE__ = new Promise((resolve, reject) => {
|
|
|
|
|
configResolve = resolve;
|
|
|
|
|
configReject = reject;
|
|
|
|
|
});
|
|
|
|
|
|
2026-06-24 01:46:30 +03:30
|
|
|
const VERSION_URL = 'https://abshode-service.montgit.ir/server/get_version';
|
|
|
|
|
const CONFIG_URLS = [
|
|
|
|
|
'https://nerkh.gold/get_user',
|
|
|
|
|
'https://abshode-service.montgit.ir/server/get_user'
|
|
|
|
|
];
|
|
|
|
|
const CONFIG_SUCCESS_URL_KEY = 'CONFIG_Success_URL';
|
2026-01-24 17:45:54 +03:30
|
|
|
|
2026-06-24 01:46:30 +03:30
|
|
|
// Timeout to ensure main.ts loads even if config fails
|
|
|
|
|
const CONFIG_TIMEOUT = 12000;
|
2026-01-24 17:45:54 +03:30
|
|
|
let configTimeoutId;
|
|
|
|
|
let mainScriptLoaded = false;
|
|
|
|
|
|
2026-06-24 01:46:30 +03:30
|
|
|
function getProtocol() {
|
|
|
|
|
return window.location.protocol === 'https:' ? 'https:' : 'http:';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function sortConfigUrls(urls) {
|
|
|
|
|
const lastSuccessUrl = localStorage.getItem(CONFIG_SUCCESS_URL_KEY);
|
|
|
|
|
|
|
|
|
|
return [...urls].sort((a, b) => {
|
|
|
|
|
if (a === lastSuccessUrl) return -1;
|
|
|
|
|
if (b === lastSuccessUrl) return 1;
|
|
|
|
|
return 0;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function fetchJson(url, timeout = 3500) {
|
|
|
|
|
const controller = new AbortController();
|
|
|
|
|
const timeoutId = window.setTimeout(() => controller.abort(), timeout);
|
|
|
|
|
|
2026-01-24 17:45:54 +03:30
|
|
|
try {
|
2026-06-24 01:46:30 +03:30
|
|
|
const response = await fetch(url, {
|
2026-01-24 17:45:54 +03:30
|
|
|
method: 'GET',
|
|
|
|
|
headers: {
|
2026-06-24 01:46:30 +03:30
|
|
|
Accept: 'application/json'
|
2026-01-24 17:45:54 +03:30
|
|
|
},
|
2026-06-24 01:46:30 +03:30
|
|
|
cache: 'no-store',
|
|
|
|
|
signal: controller.signal
|
2026-01-24 17:45:54 +03:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
2026-06-24 01:46:30 +03:30
|
|
|
throw new Error('Request failed: ' + response.status);
|
2026-01-24 17:45:54 +03:30
|
|
|
}
|
|
|
|
|
|
2026-06-24 01:46:30 +03:30
|
|
|
const contentType = response.headers.get('content-type') || '';
|
|
|
|
|
|
|
|
|
|
if (!contentType.includes('application/json')) {
|
2026-01-24 17:45:54 +03:30
|
|
|
const text = await response.text();
|
|
|
|
|
throw new Error('Response is not JSON. Received: ' + text.substring(0, 100));
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-24 01:46:30 +03:30
|
|
|
return await response.json();
|
|
|
|
|
} finally {
|
|
|
|
|
window.clearTimeout(timeoutId);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-24 17:45:54 +03:30
|
|
|
|
2026-06-24 01:46:30 +03:30
|
|
|
async function loadVersion() {
|
|
|
|
|
try {
|
|
|
|
|
await fetchJson(VERSION_URL, 3500);
|
|
|
|
|
console.log('Version loaded from:', VERSION_URL);
|
2026-01-24 17:45:54 +03:30
|
|
|
} catch (error) {
|
2026-06-24 01:46:30 +03:30
|
|
|
console.warn('Version check failed from', VERSION_URL, error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function fetchConfig() {
|
|
|
|
|
const protocol = getProtocol();
|
|
|
|
|
const urls = sortConfigUrls(
|
|
|
|
|
CONFIG_URLS.map((url) => url.replace(/^https?:/, protocol))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
for (let index = 0; index < urls.length; index++) {
|
|
|
|
|
const url = urls[index];
|
|
|
|
|
const timeout = index === urls.length - 1 ? 5000 : 3500;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const config = await fetchJson(url, timeout);
|
|
|
|
|
|
|
|
|
|
if (!config || typeof config !== 'object') {
|
|
|
|
|
throw new Error('Config response is invalid');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
localStorage.setItem(CONFIG_SUCCESS_URL_KEY, url);
|
|
|
|
|
return { config, url };
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.warn('Config failed from', url, error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new Error('Cannot load config from any server');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function applyConfig(config) {
|
|
|
|
|
window.__APP_CONFIG__ = config;
|
|
|
|
|
window.__CONFIG_READY__ = true;
|
|
|
|
|
|
|
|
|
|
if (config?.user?.name_fa) {
|
|
|
|
|
document.title = 'پنل مدیریت ' + '-' + config.user.name_fa;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (config?.user?.avatar) {
|
|
|
|
|
const faviconUrl = config.user.avatar;
|
|
|
|
|
const existingIcons = document.querySelectorAll('link[rel*="icon"]');
|
|
|
|
|
existingIcons.forEach((icon) => icon.remove());
|
|
|
|
|
|
|
|
|
|
const link = document.createElement('link');
|
|
|
|
|
link.rel = 'icon';
|
|
|
|
|
link.type = 'image/png';
|
|
|
|
|
link.href = faviconUrl + '?v=' + Date.now();
|
|
|
|
|
document.head.appendChild(link);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function finishConfigLoad(config, error) {
|
|
|
|
|
if (configResolve) {
|
|
|
|
|
configResolve(config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
window.dispatchEvent(new CustomEvent('configError', { detail: error }));
|
|
|
|
|
} else {
|
|
|
|
|
window.dispatchEvent(new CustomEvent('configReady', { detail: config }));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (configTimeoutId) {
|
|
|
|
|
clearTimeout(configTimeoutId);
|
|
|
|
|
configTimeoutId = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loadMainScript();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fetch config from server
|
|
|
|
|
async function loadConfig() {
|
|
|
|
|
try {
|
|
|
|
|
await loadVersion();
|
|
|
|
|
|
|
|
|
|
const { config, url } = await fetchConfig();
|
|
|
|
|
applyConfig(config);
|
|
|
|
|
|
|
|
|
|
console.log('Config loaded successfully from', url, config);
|
|
|
|
|
finishConfigLoad(config);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.warn('Config loader: Could not fetch config from any endpoint');
|
2026-01-24 17:45:54 +03:30
|
|
|
console.warn('Error details:', error.message);
|
2026-06-24 01:46:30 +03:30
|
|
|
console.warn('App will continue with empty config.');
|
|
|
|
|
|
2026-01-24 17:45:54 +03:30
|
|
|
window.__APP_CONFIG__ = {};
|
|
|
|
|
window.__CONFIG_READY__ = true;
|
2026-06-24 01:46:30 +03:30
|
|
|
finishConfigLoad({}, error);
|
2026-01-24 17:45:54 +03:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Function to dynamically load main.ts script
|
|
|
|
|
function loadMainScript() {
|
|
|
|
|
//const config= window.__APP_CONFIG__
|
|
|
|
|
// Check if main.ts is already loaded
|
|
|
|
|
if (mainScriptLoaded || document.querySelector('script[src="/src/main.ts"]')) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mainScriptLoaded = true;
|
|
|
|
|
|
|
|
|
|
/*const manifestLink = document.createElement("link")
|
|
|
|
|
manifestLink.rel = "manifest"
|
|
|
|
|
manifestLink.id= "manifestsdfsdfsdfsdfsdfsdf"
|
|
|
|
|
manifestLink.href = `https://nerkh.gold/get_user/${config.user.id}/monifist.json`
|
|
|
|
|
|
|
|
|
|
document.head.appendChild(manifestLink)*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import('/src/main.ts').then(() => {
|
|
|
|
|
console.log('Main script loaded and executed');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/*const script = document.createElement('script');
|
|
|
|
|
script.type = 'module';
|
|
|
|
|
script.src = '/src/main.ts';
|
|
|
|
|
script.async = true;*/
|
|
|
|
|
|
|
|
|
|
// Handle script load errors
|
|
|
|
|
/*script.onerror = function() {
|
|
|
|
|
console.error('Failed to load main.ts script');
|
|
|
|
|
mainScriptLoaded = false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
script.onload = function() {
|
|
|
|
|
console.log('Main script loaded successfully');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
document.body.appendChild(script);*/
|
|
|
|
|
|
|
|
|
|
//console.log('Main script loading started...');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set timeout to ensure main.ts loads even if config fails
|
|
|
|
|
configTimeoutId = setTimeout(function() {
|
|
|
|
|
if (!mainScriptLoaded) {
|
|
|
|
|
console.warn('Config loader: Timeout reached. Loading main.ts with empty config.');
|
|
|
|
|
// Set default empty config
|
|
|
|
|
window.__APP_CONFIG__ = window.__APP_CONFIG__ || {};
|
|
|
|
|
window.__CONFIG_READY__ = true;
|
|
|
|
|
|
|
|
|
|
// Resolve promise with empty config
|
|
|
|
|
if (configResolve) {
|
|
|
|
|
configResolve({});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loadMainScript();
|
|
|
|
|
}
|
|
|
|
|
}, CONFIG_TIMEOUT);
|
|
|
|
|
|
|
|
|
|
// Start loading config immediately
|
|
|
|
|
loadConfig();
|
|
|
|
|
})();
|
|
|
|
|
|