netnr / Get_V2EX_Link.js
2019-07-09 16:39
提取V2EX回复的链接
  Get_V2EX_Link.js javascript
(function (startNo, endNo) {
    var cells = document.getElementById("Main").children[3].children, lista = [], listmd = [], listnot = [];
    for (var i = 0; i < cells.length; i++) {
        var ci = cells[i];
        if (!ci.id || ci.id.indexOf('r_') == -1) {
            continue;
        }
        var rc = ci.querySelector('.reply_content');
        var ra = rc.getElementsByTagName('A')[0];
        var no = ci.querySelector('.no').innerHTML * 1, link = null;
        if (no < startNo || no > endNo) {
            continue;
        }
        if (ra) {
            link = ra.href;
        } else {
            rc.innerText.replace(/^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+/, function (x) { link = x });
        }
        if (link && [".png", ".jpg", ".gif"].indexOf(link.substring(link.lastIndexOf('.'))) == -1 && link.indexOf('v2ex.com') == -1 && link.indexOf('csdn.net') == -1) {
            link = link.substring(0, 30);
            lista.push({
                no: no,
                link: link[link.length - 1] == "/" ? link.substring(0, link.length - 1) : link
            });
        } else {
            listnot.push(no);
        }
    }
    console.log(lista);

    window.uuid = {
        lista,
        listmd,
        listnot,
        startTime: new Date().valueOf(),
        catch: 0,
        getmd: function () {
            console.log(listmd.join('\n'));
        }
    };

    if (lista.length) {
        lista.forEach(function (x) {
            fetch("https://bird.ioliu.cn/v2?url=" + x.link).then(x => x.text()).then(function (data) {
                var hast;
                data.replace(/\n/g, "").replace(/<title>.*<\/title>/, function (y) {
                    hast = y.substring(7, y.length - 8).trim();
                })
                if (hast) {
                    listmd.push('- [' + hast.substring(0, 30) + '](' + x.link + ')');
                } else {
                    listmd.push('- [' + x.link + '](' + x.link + ')');
                }
            }).catch(function () {
                uuid.catch++;
                listmd.push('- [' + x.link + '](' + x.link + ')');
            })
        })
    }

    uuid.si = setInterval(function () {
        console.log(listmd.length);
        if (lista.length == listmd.length || new Date().valueOf() - uuid.startTime > 1000 * 20) {
            clearInterval(uuid.si);
            uuid.getmd();
        }
    }, 1000 * 3)

})(1, 100)
ˆ