Keep the responsive layout refactor while bringing in modular updates for support UI, pricing, categories, and config loading. Co-authored-by: Cursor <cursoragent@cursor.com>
233 lines
6.9 KiB
JavaScript
233 lines
6.9 KiB
JavaScript
;(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
|
|
})
|
|
const protocol = window.location.protocol === 'https:' ? 'https:' : 'http:';
|
|
|
|
const CONFIG_URLS = [
|
|
`${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`,
|
|
]
|
|
const CONFIG_Success_URL = localStorage.getItem('CONFIG_Success_URL')
|
|
const sortedConfigUrls = [...CONFIG_URLS].sort((a, b) => {
|
|
if (a === CONFIG_Success_URL) return -1
|
|
if (b === CONFIG_Success_URL) return 1
|
|
return 0
|
|
})
|
|
|
|
// Timeout to ensure main.ts loads even if config fails (5 seconds)
|
|
const CONFIG_TIMEOUT = 5000
|
|
let configTimeoutId
|
|
let mainScriptLoaded = false
|
|
|
|
async function getConfigsFromUrl(url, timeout = 8000) {
|
|
const controller = new AbortController()
|
|
const id = setTimeout(() => controller.abort(), timeout)
|
|
|
|
try {
|
|
const response = await fetch(url, {
|
|
method: 'GET',
|
|
headers: {
|
|
Accept: 'application/json',
|
|
},
|
|
cache: 'no-cache',
|
|
signal: controller.signal,
|
|
})
|
|
|
|
return response
|
|
} catch (error) {
|
|
if (error.name === 'AbortError') {
|
|
throw new Error(`Request timeout after ${timeout}ms`)
|
|
}
|
|
throw error
|
|
} finally {
|
|
clearTimeout(id)
|
|
}
|
|
}
|
|
|
|
let retryGetConfig = 3;
|
|
// Fetch config from server
|
|
async function loadConfig() {
|
|
try {
|
|
let response = undefined
|
|
for (let i = 0; i < sortedConfigUrls.length; i++) {
|
|
try {
|
|
response = await getConfigsFromUrl(sortedConfigUrls[i],i<sortedConfigUrls.length-1?2000:5000)
|
|
if (!response.ok) {
|
|
} else {
|
|
localStorage.setItem('CONFIG_Success_URL', sortedConfigUrls[i])
|
|
break
|
|
}
|
|
} catch (e) {
|
|
console.log('e ===>', e)
|
|
continue;
|
|
}
|
|
}
|
|
if (!response?.ok) {
|
|
if (retryGetConfig>0){
|
|
retryGetConfig--;
|
|
return loadConfig()
|
|
}
|
|
throw new Error('Failed to fetch config: ' + response.status)
|
|
}
|
|
// Check if response is actually JSON
|
|
const contentType = response.headers.get('content-type')
|
|
if (!contentType || !contentType.includes('application/json')) {
|
|
const text = await response.text()
|
|
throw new Error('Response is not JSON. Received: ' + text.substring(0, 100))
|
|
}
|
|
|
|
const config = await response.json()
|
|
|
|
// Store config in window object
|
|
window.__APP_CONFIG__ = config
|
|
window.__CONFIG_READY__ = true
|
|
|
|
// Early splash background — full viewport before Vue mounts
|
|
const projectName = config?.user?.name
|
|
if (projectName === 'boursecu') {
|
|
const splashBg = '#133050'
|
|
document.documentElement.style.backgroundColor = splashBg
|
|
document.body.style.backgroundColor = splashBg
|
|
const metaTheme = document.querySelector('meta[name="theme-color"]')
|
|
if (metaTheme) metaTheme.setAttribute('content', splashBg)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
// Resolve the promise
|
|
if (configResolve) {
|
|
configResolve(config)
|
|
}
|
|
|
|
// Dispatch custom event to notify that config is ready
|
|
window.dispatchEvent(new CustomEvent('configReady', { detail: config }))
|
|
|
|
console.log('Config loaded successfully:', config)
|
|
|
|
// Clear timeout if config loaded successfully
|
|
if (configTimeoutId) {
|
|
clearTimeout(configTimeoutId)
|
|
configTimeoutId = null
|
|
}
|
|
|
|
// Load main.ts after config is ready
|
|
loadMainScript()
|
|
} catch (error) {
|
|
console.warn('Config loader: Could not fetch config from', CONFIG_URLS)
|
|
console.warn('Error details:', error.message)
|
|
console.warn(
|
|
'This is normal if the endpoint does not exist yet. App will continue with empty config.'
|
|
)
|
|
// Set default empty config on error
|
|
window.__APP_CONFIG__ = {}
|
|
window.__CONFIG_READY__ = true
|
|
|
|
// Resolve the promise even on error (with empty config)
|
|
if (configResolve) {
|
|
configResolve({})
|
|
}
|
|
|
|
window.dispatchEvent(new CustomEvent('configError', { detail: error }))
|
|
|
|
// Clear timeout if error occurred
|
|
if (configTimeoutId) {
|
|
clearTimeout(configTimeoutId)
|
|
configTimeoutId = null
|
|
}
|
|
|
|
// Load main.ts even on error (with empty config)
|
|
loadMainScript()
|
|
}
|
|
}
|
|
|
|
// 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()
|
|
})()
|