Greasy Fork

Greasy Fork is available in English.

NSFC_conclusion_downloader

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

当前为 2021-02-22 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

/* globals jQuery, $, jspdf */
(function() {
    'use strict';

    // 加载图片获得图片的width, height 属性
    function asyncImageLoader(url){
        return new Promise((resolve, reject) => {
            var image = new Image();
            image.src = url;
            image.onload = () => resolve(image);
            image.onerror = () => reject(new Error('could not load image'));
        })
    }

    const conclusionReportTab = $('#conclusion-report-tab'); // 定位结题报告窗口
    const downloadBtn = $('<button id="download_report" type="button" class="btn btn-link pull-right text-nowrap">(下载全文PDF)</button>'); // 准备交互按钮
    conclusionReportTab.before(downloadBtn); // 添加交互按钮

    downloadBtn.click(async () => {
        downloadBtn.prop('disabled', true);
        if (!/该项目暂无结题报告/.test(conclusionReportTab.text())) {
            // 获得项目信息: 编号(加密后)、批准号、项目名称
            const dependUintID = location.pathname.match(/conclusionProject\/(.+)/)[1];
            const projectID = $('#basic-tab > div:nth-child(1) > div.col-md-10').text();
            const projectName = $('#basic-tab > div:nth-child(2) > div.col-md-10').text();

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

            // 核心下载方法
            for (let i = 1;;i++) {
                downloadBtn.text(`正在下载第 ${i} 页`);
                const { data: requestData } = await $.post('/baseQuery/data/completeProjectReport', {id: dependUintID, index: i});
                const image = await asyncImageLoader(requestData.url);
                doc.addPage([image.width, image.height], image.width < image.height ? 'p' : 'l');
                doc.addImage(image, "JPEG", 0, 0, image.width, image.height);

                if (!requestData.hasnext) break;
            }

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