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

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

当前为 2023-02-17 提交的版本,查看 最新版本

// ==UserScript==
// @name         职业标准系统PDF浏览器查看下载
// @namespace    L-biaozhun
// @version      0.3.6
// @description  职业标准系统的PDF增加 使用浏览器阅读器查看、使用浏览器下载pdf 功能
// @author       L
// @match        http://biaozhun.osta.org.cn/
// @match        http://biaozhun.osta.org.cn/index.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 = {};

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>';
    LBiaozhunPdfInit = true;
}

setTimeout(function() {
    $('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() {})
    $(document).on('click', '.download', async function() {
        let type = $(this).attr('data-type');
        let get = 'http://biaozhun.osta.org.cn/api/v1/profession/get/';
        let code = $(this).attr('data-code');
        let name = $(this).attr('data-name');
        let codee = $(this).attr('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);
}