Greasy Fork

Greasy Fork is available in English.

NSFC_conclusion_downloader

帮助你直接下载国自然结题报告

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         NSFC_conclusion_downloader
// @namespace    https://blog.rhilip.info/
// @version      1.7
// @description  帮助你直接下载国自然结题报告
// @author       Rhilip
// @match        https://kd.nsfc.gov.cn/finalDetails*
// @require      https://unpkg.com/[email protected]/dist/jspdf.umd.min.js
// @require      https://unpkg.com/[email protected]/dist/jquery.js
// ==/UserScript==

/* globals $, jspdf */

(async function() {
    'use strict';

    // 准备交互按钮
    const downloadBtn = $('<div type="button" id="download_conclusion" class="el-button el-button--default el-button--medium is-round">下载全文</div>');

    // 点击交互按钮时需要开始下载操作
    downloadBtn.click(async () => {
        downloadBtn.prop('disabled', true).addClass('is-disabled');
        if (!/暂无结题报告全文/.test(conclusionReportTab.text())) {
            // 获得项目信息: 编号(加密后)、批准号、项目名称
            const urlParams = new URLSearchParams(location.search);
            const dependUintID = urlParams.get('id');
            const projectID = $('.basic_info > div.el-row div:contains("项目批准号:") + div').text();
            const projectName = $('.basic_info > div.el-row div:contains("项目名称:") + div').text();

            // 准备需要的PDF文件,并删除初始页
            const doc = new jspdf.jsPDF();
            doc.deletePage(1);
            doc.setDocumentProperties({
                title: `${projectID} ${projectName}`,
                subject: location.href,
                creator: 'NSFC_conclusion_downloader'
            });

            // 核心下载方法
            const image = new Image();
            for (let i=1;;i++) {
                downloadBtn.text(`正在下载第 ${i} 页`);

                // 获得图片链接
                const { data: requestData } = await $.post('/api/baseQuery/completeProjectReport', {id: dependUintID, index: i});

                // 获得Blob形式的imageData,这样可以防止image.src和jsPDF.addImage会产生两次图片请求,浪费带宽
                // (实际变成了一次请求服务器和两次请求本地blob)
                try {

                    const imageDataAsBlob = await $.ajax({
                        url: requestData.url,
                        method: 'GET',
                        xhrFields: { responseType: 'blob'}
                    });
                    // 加载图片并获得图片的 width, height 属性
                    image.src = URL.createObjectURL(imageDataAsBlob);
                    await image.decode();

                    // 将图片添加进PDF中
                    doc.addPage([image.width, image.height], image.width < image.height ? 'p' : 'l');
                    doc.addImage(image, "PNG", 0, 0, image.width, image.height);
                } catch (e) {
                    break; // 如果中间有任何失败,则直接break
                }

                if (requestData.hasnext === false) break; // hasnext可能为null,此处应该明确为false才break
            }

            // 我们并没法 await 保存过程,所以直接显示下载完成就好,浏览器处理好会自动显示下载文件
            doc.save(`${projectID} ${projectName}.pdf`);
            downloadBtn.text('下载完成');
        }
    });

    // 将交互按钮插入到页面中
    const observer = new MutationObserver(mutations => {
        if ($('#related').length > 0 && $('#download_conclusion').length == 0) {
            $('#related div.verticalBar_T').after(downloadBtn);
        }
    });

    observer.observe(document.body, {
        childList: true,
        subtree: true
    });
})();