yangyin
2024-12-11 104450e6be387c9cc2fc9c9f6ab196e16da25fe1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// 获取 `keywords` 标签
const keywordsTag = document.querySelector("head>meta[name='keywords']");
// 获取 `description` 标签
const descriptionTag = document.querySelector("head>meta[name='description']");
// 获取 `title` 标签
const title = document.querySelector('head>title');
// 获取 `favicon` 标签
const faviconIcon = document.querySelector("head>link[rel='icon']");
 
const briefCorpName = window.globalConfig.SoftWareInfo.globalTitle;
const sysName = window.globalConfig.SoftWareInfo.globalViceTitle;
const icon = window.globalConfig.SoftWareInfo.favicon;
 
keywordsTag.content = `${briefCorpName},${sysName}`;
descriptionTag.content = `${briefCorpName},${sysName}`;
title.innerText = sysName;
faviconIcon.href = icon + `?v=1`;
 
//#region ====================== LCP Promise ======================
const loadBaseLCP = new Promise((resolve, reject) => {
    try {
        // 检查是否支持 PerformanceObserver 和 LCP 指标
        if ('PerformanceObserver' in window && PerformanceObserver.supportedEntryTypes.includes('largest-contentful-paint')) {
            const observer = new PerformanceObserver((list) => {
                window.list = list.getEntries();
                for (const entry of list.getEntries()) {
                    resolve(entry);
                    observer.disconnect(); // 完成后断开观察者
                }
            });
            observer.observe({ type: 'largest-contentful-paint', buffered: true });
        } else {
            // 不支持时 使用onLoad 加载
            window.addEventListener('load', () => {
                resolve(true);
            });
        }
    } catch (error) {
        reject(error);
    }
});
//#endregion
 
//#region ====================== 加载script ======================
 
function loadScript(src, callback) {
    const script = document.createElement('script');
    script.setAttribute('type', 'text/javascript');
    script.setAttribute('src', src);
    script.async = true;
    script.onload = () => callback();
    script.onerror = () => callback(new Error(`Failed to load ${src}`));
    document.head.appendChild(script);
}
window.eventList = {};
 
loadBaseLCP.then(() => {
    window.eventList.amisSdkJsPromise = new Promise((resolve, reject) => {
        loadScript('/static/amis/sdk/sdk.js', (error) => {
            if (error) {
                reject(error);
                return;
            }
            resolve();
        });
    });
});
 
//#endregion
 
//#region ====================== 加载样式 ======================
function loadStyles(styles) {
    for (const path of styles) {
        const style = document.createElement('link');
        style.setAttribute('rel', 'stylesheet');
        style.setAttribute('type', 'text/css');
        style.setAttribute('href', path);
        // 设置 print 可以避免阻塞页面渲染
        style.setAttribute('media', 'print');
        style.onload = () => {
            style.setAttribute('media', 'all');
        };
        document.head.appendChild(style);
    }
}
 
loadBaseLCP.then(() => {
    loadStyles(['/static/amis/sdk/sdk.css', '/static/amis/sdk/helper.css', '/static/amis/sdk/iconfont.css']);
 
});
 
//#endregion