Mirror site is read only www.netnr.com
netnr/ dead_link_detection.js 2019-08-09 22:08
检测页面所有链接,找到死链(如导航页面维护清理失效的链接)
var dld = {
    //跨域代理
    proxyServer: ['https://cors.eu.org/', 'https://api.netnr.eu.org/link/', 'https://netnr.zme.ink/api/v1/Proxy?url='],
    proxyIndex: 0,
    proxyGet: (link) => {
        let ps = dld.proxyServer[dld.proxyIndex++];
        if (dld.proxyIndex >= dld.proxyServer.length) {
            dld.proxyIndex = 0;
        }
        return `${ps}${encodeURIComponent(link)}`;
    },

    resultOk: [],
    resultBad: [],

    checkLinks: document.links,
    checkIndex: 0,
    checkMax: 3,
    checkPending: [],

    init: () => {
        dld.start();
    },

    start: () => {
        // 开始检测一个链接
        const link = dld.checkLinks[dld.checkIndex];
        const url = dld.proxyGet(link);

        const promise = fetch(url).then(resp => {
            if (resp.ok) {
                dld.resultOk.push(resp);
            } else {
                dld.resultBad.push(link);
            }
        }).catch(error => {
            dld.resultBad.push(link);
        }).finally(() => {
            //完成代办
            dld.checkPending.splice(dld.checkPending.indexOf(promise), 1);
            console.debug(`ok: ${dld.resultOk.length}, bad: ${dld.resultBad.length}, progress: ${(dld.checkIndex + 1) - dld.checkPending.length}/${dld.checkLinks.length}`);

            //结束
            if (dld.checkIndex + 1 == dld.checkLinks.length && dld.checkPending.length == 0) {
                console.debug(dld.resultBad.join('\r\n'));
            } else if (dld.checkIndex + 1 < dld.checkLinks.length && dld.checkPending.length < dld.checkMax) {
                // 检测下一个链接
                dld.checkIndex++;
                dld.start();
            }
        });
        //加入代办
        dld.checkPending.push(promise);

        // 检测下一个链接
        if (dld.checkIndex + 1 < dld.checkLinks.length && dld.checkPending.length < dld.checkMax) {
            dld.checkIndex++;
            dld.start();
        }
    }
};

dld.init();