职业标准系统PDF下载

好好学习

目前为 2023-01-17 提交的版本。查看 最新版本

// ==UserScript==
// @name         职业标准系统PDF下载
// @namespace    L-biaozhun
// @version      0.3.1
// @description  好好学习
// @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;

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}} <a href="javascript:void(0)" style="margin-left: 5px;" :data-code="item.id" :data-name="item.name" class="download" data-type="open">直接打开</span><a href="javascript:void(0)" style="margin-left: 6px;" :data-code="item.id" :data-name="item.name" :data-codee="item.code" class="download">下载</span>';
    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() {
        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 (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失败');
                return ;
            }

            var bStr = atob(pdf.data),
                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);
        }).fail(function() {
            layer.alert('下载失败');
        })
    })
}, 500);

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址