全国图书馆参考咨询联盟

下载DPF少操作一步,「文章下载」替换成「PDF下载」,点击直接下载。也可辅助Zotero translator的Superlib.js,直接抓取PDF。

目前為 2020-08-15 提交的版本,檢視 最新版本

// ==UserScript==
// @name           全国图书馆参考咨询联盟
// @description    下载DPF少操作一步,「文章下载」替换成「PDF下载」,点击直接下载。也可辅助Zotero translator的Superlib.js,直接抓取PDF。
// @author         018([email protected])
// @contributor    Rhilip
// @connect        *
// @grant          GM_xmlhttpRequest
// @grant          GM_setClipboard
// @grant          GM_addStyle
// @grant          GM_setValue
// @grant          GM_getValue
// @grant          GM_listValues
// @grant          GM_deleteValue
// @grant          GM_registerMenuCommand
// @require        https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js
// @require        https://gf.qytechs.cn/scripts/368137-encodeToGb2312/code/encodeToGb2312.js?version=601683
// @include        http://jour.ucdrs.superlib.net/*
// @version        0.1.0
// @run-at         document-end
// @namespace      http://018.ai
// ==/UserScript==

// This Userscirpt can't run under Greasemonkey 4.x platform
if (typeof GM_xmlhttpRequest === 'undefined') {
    alert('不支持Greasemonkey 4.x,请换用暴力猴或Tampermonkey')
    return
}

// 不属于的页面
if (!/jour.ucdrs.superlib.net/.test(location.host)) {
    return
}

;(function () {
    'use strict';

    $(document).ready(function () {
        if (location.href.includes('/views/specific')) {
            var as = $('.link a');
            if (as.length > 0) {
                loadHref(as.get(0));
            }
        } else if (location.href.includes('/searchJour')) {
            for (var a of $('.book1 .get a')) {
                if (a.textContent != '文章下载') continue;

                loadHref(a);
            }
        }
    })

    // 加载PDF页面
    function loadHref(a) {
        loadDoc(a.href, {a: a}, function(doc, responseDetail, meta) {
            var download = doc.querySelector('.download .down_bnt');
            if (download) {
                replaceHref($(meta.a), download.href)
            }
        });
    }

    // 替换href
    function replaceHref($a, pdfurl) {
        $a.attr('href', pdfurl);
        $a.html('PDF下载');
    }

    // 判断,空返回空字符串
    function opt(val) {
        if (!val) return '';

        if (val instanceof Array) {
            if (val.length > 0) {
                return val[0];
            }
        } else {
            return val;
        }
    }

    // 对使用GM_xmlhttpRequest返回的html文本进行处理并返回DOM树
    function page_parser(responseText) {
        // 替换一些信息防止图片和页面脚本的加载,同时可能加快页面解析速度
        responseText = responseText.replace(/s+src=/ig, ' data-src='); // 图片,部分外源脚本
        responseText = responseText.replace(/<script[^>]*?>[\S\s]*?<\/script>/ig, ''); //页面脚本
        return (new DOMParser()).parseFromString(responseText, 'text/html');
    }

    // 加载网页
    function loadDoc (url, meta, callback) {
        GM_xmlhttpRequest({
            method: 'GET',
            url: url,
            onload: function (responseDetail) {
                if (responseDetail.status === 200) {
                    let doc = page_parser(responseDetail.responseText)
                    callback(doc, responseDetail, meta)
                } else {
                    callback(undefined, responseDetail, meta)
                }
            }
        })
    }

    // get请求
    function doGet (url, meta, callback) {
        GM_xmlhttpRequest({
            method: 'GET',
            url: url,
            onload: function (responseDetail) {
                if (responseDetail.status === 200) {
                    callback(JSON.parse(responseDetail.responseText), responseDetail, meta)
                } else {
                    callback(undefined, responseDetail, meta)
                }
            }
        })
    }

    // post请求
    function doPost (url, headers, data, meta, callback) {
        GM_xmlhttpRequest({
            method: "POST",
            url: url,
            data: data,
            headers: headers,
            onload: function(responseDetail){
                if (responseDetail.status === 200) {
                    callback(JSON.parse(responseDetail.responseText), responseDetail, meta)
                } else {
                    callback(undefined, responseDetail, meta)
                }
            }
        })
    }
})()

QingJ © 2025

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