fix some bug
This commit is contained in:
+80
-69
@@ -1,13 +1,9 @@
|
||||
(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) => {
|
||||
@@ -15,14 +11,11 @@
|
||||
configReject = reject;
|
||||
});
|
||||
|
||||
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 VERSION_SUCCESS_URL_KEY = 'VERSION_Success_URL';
|
||||
const CONFIG_SUCCESS_URL_KEY = 'CONFIG_Success_URL';
|
||||
const VERSION_RETRY_ROUNDS = 5;
|
||||
const CONFIG_RETRY_ROUNDS = 5;
|
||||
|
||||
// Timeout to ensure main.ts loads even if config fails
|
||||
const CONFIG_TIMEOUT = 12000;
|
||||
let configTimeoutId;
|
||||
let mainScriptLoaded = false;
|
||||
@@ -31,6 +24,40 @@
|
||||
return window.location.protocol === 'https:' ? 'https:' : 'http:';
|
||||
}
|
||||
|
||||
function getVersionUrls() {
|
||||
const protocol = getProtocol();
|
||||
|
||||
return [
|
||||
'https://nerkh.gold/get_version',
|
||||
'https://tablo.talasevin.ir/get_version',
|
||||
'https://abshode-service.montgit.ir/server/get_version',
|
||||
protocol + '//212.23.201.173:8381/get_version',
|
||||
protocol + '//91.212.174.137:4040/server/get_version'
|
||||
];
|
||||
}
|
||||
|
||||
function getConfigUrls() {
|
||||
const protocol = getProtocol();
|
||||
|
||||
return [
|
||||
protocol + '//nerkh.gold/get_user',
|
||||
protocol + '//tablo.talasevin.ir/get_user',
|
||||
protocol + '//abshode-service.montgit.ir/server/get_user',
|
||||
protocol + '//212.23.201.173:8381/get_user',
|
||||
protocol + '//91.212.174.137:4040/server/get_user'
|
||||
];
|
||||
}
|
||||
|
||||
function sortVersionUrls(urls) {
|
||||
const lastSuccessUrl = localStorage.getItem(VERSION_SUCCESS_URL_KEY);
|
||||
|
||||
if (!lastSuccessUrl) {
|
||||
return urls.slice();
|
||||
}
|
||||
|
||||
return [lastSuccessUrl].concat(urls.filter((url) => url !== lastSuccessUrl));
|
||||
}
|
||||
|
||||
function sortConfigUrls(urls) {
|
||||
const lastSuccessUrl = localStorage.getItem(CONFIG_SUCCESS_URL_KEY);
|
||||
|
||||
@@ -41,6 +68,12 @@
|
||||
});
|
||||
}
|
||||
|
||||
function clearStaleSuccessUrl(key, url) {
|
||||
if (localStorage.getItem(key) === url) {
|
||||
localStorage.removeItem(key);
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchJson(url, timeout = 3500) {
|
||||
const controller = new AbortController();
|
||||
const timeoutId = window.setTimeout(() => controller.abort(), timeout);
|
||||
@@ -72,36 +105,50 @@
|
||||
}
|
||||
}
|
||||
|
||||
async function loadVersion() {
|
||||
try {
|
||||
await fetchJson(VERSION_URL, 3500);
|
||||
console.log('Version loaded from:', VERSION_URL);
|
||||
} catch (error) {
|
||||
console.warn('Version check failed from', VERSION_URL, error);
|
||||
async function loadVersion(rounds = VERSION_RETRY_ROUNDS) {
|
||||
const urls = sortVersionUrls(getVersionUrls());
|
||||
|
||||
for (let round = 1; round <= rounds; round++) {
|
||||
for (let index = 0; index < urls.length; index++) {
|
||||
const url = urls[index];
|
||||
const timeout = index === urls.length - 1 ? 6000 : 3500;
|
||||
|
||||
try {
|
||||
await fetchJson(url, timeout);
|
||||
localStorage.setItem(VERSION_SUCCESS_URL_KEY, url);
|
||||
console.log('Version loaded from:', url);
|
||||
return;
|
||||
} catch (error) {
|
||||
clearStaleSuccessUrl(VERSION_SUCCESS_URL_KEY, url);
|
||||
console.warn('Version failed from', url, 'round:', round, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.warn('Version check failed from all endpoints');
|
||||
}
|
||||
|
||||
async function fetchConfig() {
|
||||
const protocol = getProtocol();
|
||||
const urls = sortConfigUrls(
|
||||
CONFIG_URLS.map((url) => url.replace(/^https?:/, protocol))
|
||||
);
|
||||
async function fetchConfig(rounds = CONFIG_RETRY_ROUNDS) {
|
||||
const urls = sortConfigUrls(getConfigUrls());
|
||||
|
||||
for (let index = 0; index < urls.length; index++) {
|
||||
const url = urls[index];
|
||||
const timeout = index === urls.length - 1 ? 5000 : 3500;
|
||||
for (let round = 1; round <= rounds; round++) {
|
||||
for (let index = 0; index < urls.length; index++) {
|
||||
const url = urls[index];
|
||||
const timeout = index === urls.length - 1 ? 7000 : 4000;
|
||||
|
||||
try {
|
||||
const config = await fetchJson(url, timeout);
|
||||
try {
|
||||
const config = await fetchJson(url, timeout);
|
||||
|
||||
if (!config || typeof config !== 'object') {
|
||||
throw new Error('Config response is invalid');
|
||||
if (!config || typeof config !== 'object') {
|
||||
throw new Error('Config response is invalid');
|
||||
}
|
||||
|
||||
localStorage.setItem(CONFIG_SUCCESS_URL_KEY, url);
|
||||
return { config, url };
|
||||
} catch (error) {
|
||||
clearStaleSuccessUrl(CONFIG_SUCCESS_URL_KEY, url);
|
||||
console.warn('Config failed from', url, 'round:', round, error);
|
||||
}
|
||||
|
||||
localStorage.setItem(CONFIG_SUCCESS_URL_KEY, url);
|
||||
return { config, url };
|
||||
} catch (error) {
|
||||
console.warn('Config failed from', url, error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,7 +195,6 @@
|
||||
loadMainScript();
|
||||
}
|
||||
|
||||
// Fetch config from server
|
||||
async function loadConfig() {
|
||||
try {
|
||||
await loadVersion();
|
||||
@@ -169,57 +215,24 @@
|
||||
}
|
||||
}
|
||||
|
||||
// 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({});
|
||||
}
|
||||
@@ -228,7 +241,5 @@
|
||||
}
|
||||
}, CONFIG_TIMEOUT);
|
||||
|
||||
// Start loading config immediately
|
||||
loadConfig();
|
||||
})();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user