<!DOCTYPE html>
<html>

<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta charset="utf-8" />
    <title>NPM 包版本检测更新</title>

    <link rel="stylesheet" href="https://npm.elemecdn.com/bootstrap@4.6.0/dist/css/bootstrap.min.css" />

</head>

<body>
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-12 mt-3">
                <textarea class="form-control nr-txt1" placeholder="NPM 包版本检测更新"></textarea>
            </div>
        </div>
    </div>
    <iframe src="https://ss.js.org/diff#hide-menu" scrolling="no" frameborder="0"
        style="width:100%;visibility:hidden;"></iframe>
</body>

</html>
var cv = {
    de: document.querySelector('iframe'),
    title: document.title,
    init: function () {
        document.querySelector('.nr-txt1').oninput = function () {
            cv.parse(this.value)
        }

        window.onload = window.onresize = function () {
            cv.resize()
        };
    },

    resize: function () {
        var ch = document.documentElement.clientHeight - cv.de.getBoundingClientRect().top - 10;
        cv.de.style.height = Math.max(100, ch) + "px";
    },

    parse: function (txt) {
        cv.otext = txt;
        cv.odata = [];

        txt.replace(/\/(\S+)@(\d+.\d+.\d+)\/(\S+)['|"]/gm, function (uri, pn, pv, pp) {
            var obj = {
                uri: uri.substring(0, uri.length - 1),
                package: pn.split('/').pop(),
                v0: pv,
                v1: null,
                error: null
            };
            if (obj.uri.includes('"')) {
                obj.uri = obj.uri.split('"')[0];
            }
            if (obj.uri.includes("'")) {
                obj.uri = obj.uri.split("'")[0];
            }

            if (cv.hosts == null) {
                cv.hosts = obj.uri.split('/' + obj.package)[0]
            }

            var vp = obj.uri.replace(cv.hosts, '').substring(1).split(obj.v0)[0];
            vp = vp.substring(0, vp.length - 1);
            if (obj.package != vp) {
                console.log(`已修正匹配模式包名异常:${obj.package} => ${vp}`)
                obj.package = vp;
            }

            cv.odata.push(obj);
        });

        console.table(cv.odata);

        cv.check();
    },

    check: function (i) {
        i = i || 0;
        console.log(`${i + 1} / ${cv.odata.length}`)
        document.title = `${i + 1} / ${cv.odata.length} ${cv.title}`
        if (i < cv.odata.length) {
            var obj = cv.odata[i];

            var uri = `https://registry.npmjs.org/${obj.package}`;
            uri = `https://cors.eu.org/${encodeURIComponent(uri)}`;

            fetch(uri).then(x => x.json()).then(res => {
                console.log(res)
                var latest = res["dist-tags"].latest;

                obj.v1 = latest;
                cv.odata[i] = obj;

                cv.check(i + 1);
            }).catch(err => {
                obj.error = err;
                cv.odata[i] = obj;

                cv.check(i + 1);
            })
        } else {
            var txt2 = cv.otext;
            cv.odata.forEach(obj => {
                if (obj.v1 != null) {
                    txt2 = txt2.replace(obj.uri, obj.uri.replace(obj.package + '@' + obj.v0, obj.package + '@' + obj.v1));
                }
            })

            document.title = cv.title;
            cv.setDiff(cv.otext, txt2);
        }
    },
    setDiff: function (origin, vary) {
        var iframe = cv.de;
        iframe.style.visibility = "visible";
        iframe.contentWindow.postMessage(JSON.stringify({ origin, vary }), iframe.src);
    }
}

cv.init();