职业标准系统PDF浏览器查看下载

职业标准系统的PDF增加 使用浏览器阅读器查看、使用浏览器下载pdf 功能

// ==UserScript==
// @name         职业标准系统PDF浏览器查看下载
// @namespace    L-biaozhun
// @version      0.3.7
// @description  职业标准系统的PDF增加 使用浏览器阅读器查看、使用浏览器下载pdf 功能
// @author       L
// @match        http://biaozhun.osta.org.cn/
// @match        http://biaozhun.osta.org.cn/index.html
// @match        http://biaozhun.osta.org.cn/pdfview.html*
// @grant        none
// @run-at       document-body
// @homepage     https://www.ihawo.com
// @license MIT
// ==/UserScript==

let dataTpl = document.querySelectorAll('.table_data');
let LBiaozhunPdfInit = false;
let pdfCacheMap = {};
let url = window.location.href;

if (url.indexOf('pdfview') != -1) {
    LBiaozhunPdfInit = true;
} else {
    if (dataTpl.length == 1) {
        let tpl = dataTpl[0];
        for(var i in tpl.children) {
            if (!isNaN(parseInt(i))) {
                tpl.children[i].style.cssText = "white-space: nowrap;"
            }
        }
        tpl.children[2].innerHTML = '{{item.name}} <br><div style="margin-top:-10px;"><a :href="item.web" target="_blank">网站查看</span><a href="javascript:void(0)" style="margin-left:10px;" :data-code="item.id" :data-name="item.name" class="download" data-type="open">浏览器查看</span><a href="javascript:void(0)" style="margin-left: 10px;" :data-code="item.id" :data-name="item.name" :data-codee="item.code" class="download">下载PDF</span></div>';
        tpl.innerHTML = tpl.innerHTML.replace(/item\.web/g, "item.web + '&name=' + item.name + '&codee=' + item.code")
        LBiaozhunPdfInit = true;
    }
}


setTimeout(function() {
    if (url.indexOf('pdfview') != -1) {
        let style = '<style>*{margin:0;}iframe{border:none}</style>';
        $('html').append(style);

        let codeMatch = url.match(/code=([^&]*)/);
        let nameMatch = url.match(/name=([^&]*)/);
        let codeeMatch = url.match(/codee=([^&]*)/);

        let code = codeMatch && codeMatch[1] || 0;
        let name = nameMatch && decodeURI(nameMatch[1]) || '';
        let codee = codeeMatch && codeeMatch[1] || '';

        if (code && name) {
           let div = '<div class="download" data-code="' + code + '" data-codee="' + codee + '" data-name="' + name + '" style="position:fixed;top: 4px;background: #fff;right: 5px;padding: 3px 10px;font-size: 12px;border-radius: 3px;cursor: pointer;box-shadow: 0 0 10px rgba(0,0,0,0.1);">下载</div>'
            $('html').append(div);
        }
    }
    $('html').append('<script src="https://cdn.bootcdn.net/ajax/libs/layer/3.5.1/layer.min.js"></script>');
    if (!LBiaozhunPdfInit) { setTimeout(function() {layer.alert('加载失败,请刷新页面重试');}, 1000) }
    $(document).on('click', '.download', async function() {
        let type = $(this).data('type');
        let get = 'http://biaozhun.osta.org.cn/api/v1/profession/get/';
        let code = $(this).data('code');
        let name = $(this).data('name');
        let codee = $(this).data('codee');
        let req = get + code;

        if (pdfCacheMap[code]) {
            pdfCacheMap[code]['times']++;
            processPdfData(type, name, codee, pdfCacheMap[code]['data']);
            return pdfCacheMap[code]['times'] > 2 && delete pdfCacheMap[code];
        }

        if (layer) {
            layer.load(0, {
                shade: [0.5, '#000'],
             })
        }
        $.ajax({
            url: req,
            dataType: 'json',
        }).always(function(){
            if (layer) {
                layer.closeAll();
            }
        }).done(function(pdf) {
            if (!pdf.data) {
                layer.alert('获取pdf失败:' + pdf.msg);
                return ;
            }

            pdfCacheMap[code] = {
                'data': pdf.data,
                'times': 1
            }
            processPdfData(type, name, codee, pdf.data);
        }).fail(function() {
            layer.alert('下载失败');
        })
    })
}, 500);

function processPdfData(type, name, codee, pdfData) {
    var bStr = atob(pdfData),
        n = bStr.length,
        unit8Array = new Uint8Array(n);

    while (n--) {
        unit8Array[n] = bStr.charCodeAt(n);
    }

    var blob = new Blob([unit8Array], { type: 'application/pdf' });
    var url = window.URL.createObjectURL(blob);

    if (type == 'open') {
        return window.open(url);
    }

    let a = document.createElement("a");
    let event = new MouseEvent("click");
    a.download = name + '_' + codee + '.pdf';
    a.href = url;
    a.dispatchEvent(event);
}