美文网首页
使用Chrome下载 道客巴巴 的文档

使用Chrome下载 道客巴巴 的文档

作者: 饭桶2018 | 来源:发表于2018-09-20 20:58 被阅读0次

注意:此脚本仅供个人学习、交流之用,请尊重版权。

$("#continueButton").click();  // click the continue button
var pathname = window.location.pathname.split('/')[1].split('.')[0] // get the url name to name files

var refreshIntervalId = setInterval(download, 3000);  // define a callback loop to download 

function download() {
    var canvases = $("[id^=page_]");  // select all canvas
    var l = canvases.length;  
    var download_count = 0;
    var y_notload; // define Y position of page not loaded!
    
    // enumerate the canvas selections with reverse order 
    $(canvases.get().reverse()).each(function () {

        var canvas = $(this)[0]; // get the canvas from object
        var ls_flag = canvas.getAttribute('ls');  // get show sign, 1 for ok; 0 for not yet

        // inject download flag to canvas tag
        var down_flag = canvas.getAttribute('down_flag');
        if (down_flag === null) {
            canvas.setAttribute('down_flag', '0');
            down_flag = '0';
        }

        // download if download flag is 0 and show flag is 1
        if (down_flag.toString() == '0') {
            var filename = canvas.getAttribute('id');
            var y = parseInt(filename.split('_')[1]);
            if (ls_flag.toString() == '1') {
                var png = canvas.toDataURL('image/png');
                var down = document.createElement('a');
                down.href = png;
                down.download = pathname + '_' + filename + '.png';
                down.click();
                console.log(filename + ' downloading...');
                canvas.setAttribute('down_flag', '1');
                download_count = download_count + 1;
            } else {
                // get the number of the canvas  not loaded
                y_notload = y;
            }
        } else {
            download_count = download_count + 1;
        }
    })

    // scroll to the canvas if not loaded 
    // https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect
    // use getBoundingClientRect to find the Y position of every canvas
    if (!!y_notload){
        console.log('Scroll To ' + y_notload.toString());
        scrollTo(0, $("#page_" + y_notload).parent()[0].getBoundingClientRect().y - $("#page_1").parent()[0].getBoundingClientRect().y)
    }

    var left = l - download_count;
    console.log(left.toString() + ' lefted');

    if (left == 0) {
        console.log('Finished!');
        clearInterval(refreshIntervalId);
    }

}

相关文章

网友评论

      本文标题:使用Chrome下载 道客巴巴 的文档

      本文链接:https://www.haomeiwen.com/subject/hyfonftx.html