| 397 | } |
| 398 | |
| 399 | public loadRemoteScript(url: string, id?: string, cb?: any): Promise<HTMLElement> { |
| 400 | if (!url) { |
| 401 | return Promise.reject(new Error('URL is required to load remote script')) |
| 402 | } |
| 403 | if (id && document.getElementById(id)) { |
| 404 | return Promise.resolve(document.getElementById(id) as HTMLElement) |
| 405 | } |
| 406 | if (url.startsWith('/')) { |
| 407 | const real_url = import.meta.env.VITE_API_BASE_URL.replace('/api/v1', '') |
| 408 | url = real_url + url |
| 409 | } |
| 410 | return new Promise<HTMLElement>((resolve, reject) => { |
| 411 | // 改用传统的script标签加载方式 |
| 412 | const script = document.createElement('script') |
| 413 | script.src = url |
| 414 | script.id = id || `remote-script-${Date.now()}` |
| 415 | |
| 416 | script.onload = () => { |
| 417 | if (cb) cb() |
| 418 | resolve(script) |
| 419 | } |
| 420 | |
| 421 | script.onerror = (error) => { |
| 422 | console.error(`Failed to load script from ${url}:`, error) |
| 423 | reject(new Error(`Failed to load script from ${url}`)) |
| 424 | } |
| 425 | |
| 426 | document.head.appendChild(script) |
| 427 | }) |
| 428 | } |
| 429 | /* public loadRemoteScript(url: string, id?: string, cb?: any): Promise<HTMLElement> { |
| 430 | if (!url) { |
| 431 | return Promise.reject(new Error('URL is required to load remote script')) |