移动系统liao
3 天以前 df64ac247493594de867fa0fd89f518446eff118
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
/**
 *通过监听当前页面的JS的SRC来判断当前网页是否有更新
 *使用方法   main.js   import checkUpdate from "/@/utils/auto-update";
 */
 
let lastSrcs: any[] | null; //上次js地址集合
// const scriptReg = /(?<=<script.*src=["']).*?(?=["'])/gm; //IOS 不支持断言匹配
const scriptReg = /<script.*?src=['"](.*?)['"]/gm;
 
/**
 * 获取最新的js集合
 * @returns
 */
async function extractNewScripts() {
    const html = await fetch('/?_t=' + Date.now()).then((res) => res.text());
    scriptReg.lastIndex = 0;
    const result = html.match(scriptReg);
    return result;
}
/**
 * 判断是否有更新
 * @returns
 */
async function checkUpdate() {
    const newScripts = await extractNewScripts();
    if (!lastSrcs) {
        lastSrcs = newScripts;
        return false;
    }
    if (newScripts == null) return false;
    let result = false;
    if (lastSrcs.length !== newScripts.length) {
        result = true;
    }
    for (let i = 0; i < lastSrcs.length; i++) {
        if (lastSrcs[i] !== newScripts[i]) {
            result = true;
            break;
        }
    }
    lastSrcs = newScripts;
    return result;
}
 
/**
 * 定时器定时检测是否更新,有更新则执行回调函数
 * @param callbackFn
 * @param interval
 */
function checkUpdateInterval(callbackFn: any, interval: number = 60000) {
    setTimeout(async () => {
        const willUpdate = await checkUpdate();
        if (willUpdate) {
            callbackFn();
        }
        checkUpdateInterval(callbackFn, interval);
    }, interval);
}
 
export default checkUpdateInterval;