Greasy Fork

知网PDF下载助手

知网已经支持直接下载带目录的PDF格式论文, 本脚本已意义不大

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

// ==UserScript==
// @name           知网PDF下载助手
// @version        3.7.14
// @author         Supernova
// @description    知网已经支持直接下载带目录的PDF格式论文, 本脚本已意义不大
// @match          http*://*.cnki.net/*
// @match          http*://*.cnki.net.*/*
// @match          http*://*/defaultResult/index*
// @match          http*://*/KNS8/AdvSearch*
// @match          http*://*/kcms/detail/detail.aspx*
// @exclude        http://image.cnki.net/*
// @run-at         document-idle
// @icon           https://cnki.net/favicon.ico
// @grant          unsafeWindow
// @grant          GM_setClipboard
// @grant          GM_xmlhttpRequest
// @grant          GM_notification
// @grant          GM_setValue
// @grant          GM_getValue
// @grant          GM_deleteValue
// @license        MIT
// @namespace https://github.com/supernovaZhangJiaXing/Tampermonkey/
// ==/UserScript==

'use strict';
var $ = unsafeWindow.jQuery;

$(document).ready(function() {
    var myurl = window.location.href;
    var isDetailPage = myurl.match(/(article|detail\.aspx)/g) ? true: false; // 点进文献后的详情页

    if (isDetailPage === false) {
        // 对应普通检索和高级检索
        if (window.location.href.indexOf("kns8") != -1 || window.location.href.indexOf("KNS8") != -1){
            $(document).ajaxSuccess(function(event, xhr, settings) {
                if (settings.url.indexOf('/Brief/GetGridTableHtml') + 1) {
                    var down_btns = $('.downloadlink');
                    var titles = $(".fz14");
                    for (var i = 0; i < down_btns.length; i++) {
                        var article_url = titles.eq(i).attr('href');
                        down_btns.eq(i).css('background-color', '#C7FFFF').mouseover(function(e){
                            this.title="CAJ下载";
                        });
                        // var pdf_down_btn = down_btns.eq(i).clone().css('background-color', '#C7FFC7').mouseover(function(e){
                        //     this.title="PDF下载";
                        // });
                        // if (GM_getValue('pdf_url') !== "") {
                        //     GM_deleteValue('pdf_url');
                        // }
                        // // 获取文章的真实地址
                        // GM_xmlhttpRequest({
                        //     method: "GET",
                        //     url: article_url,
                        //     synchronous: true,
                        //     headers: {
                        //         Referer: 'https://kns.cnki.net/kns8/defaultresult/index',
                        //         'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.68"
                        //     },
                        //     redirectionLimit: 10,
                        //     onload: function(response) {
                        //         console.log(response.finalUrl)
                        //         GM_xmlhttpRequest({
                        //             method: "GET",
                        //             url: response.finalUrl, // 跳转后的最终链接
                        //             synchronous: true,
                        //             headers: {
                        //                 Referer: 'https://kns.cnki.net/kns8/defaultresult/index',
                        //                 'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.68"
                        //             },
                        //             onload: function(response) {
                        //                 // pdf的跳转链接(非直链)
                        //                 GM_setValue('pdf_url', $("#pdfDown", response.responseText).attr("href"));
                        //             }
                        //         });
                        //     }
                        // });
                        // pdf_down_btn.attr('href', GM_getValue('pdf_url'));
                        // GM_deleteValue('pdf_url');
                        // down_btns.eq(i).after(pdf_down_btn);
                    }
                    // $('.bulkdownload.export').eq(0).after($('.bulkdownload.export').eq(0).clone().html($('.bulkdownload.export').eq(0).html().replace('下载', 'PDF'))
                    //                                       .removeClass('bulkdownload').click(function () { // 点击下载按钮后的行为
                    //     // 获取到勾选的文献, 下载其pdf版
                    //     for (var i = 0; i < $('input.cbItem').length; i++) {
                    //         if ($('input.cbItem').eq(i).attr('checked') == 'checked') { // 只针对勾选中的i
                    //             sleep(1000).then(
                    //                 window.open($('.downloadlink').eq(2 * i + 1).attr('href'))
                    //             );
                    //         }
                    //     }
                    //     $.filenameClear();
                    // }).css('background-color', '#C7FFC7'));
                    $('.bulkdownload.export').eq(0).html($('.bulkdownload.export').eq(0).html().replace('下载', 'CAJ'))
                                                          .removeClass('bulkdownload').click(function () { // 点击下载按钮后的行为
                        // 获取到勾选的文献, 下载其caj版
                        for (var i = 0; i < $('input.cbItem').length; i++) {
                            if ($('input.cbItem').eq(i).attr('checked') == 'checked') { // 只针对勾选中的i
                                sleep(1000).then(
                                    window.open($('.downloadlink').eq(i).attr('href'))
                                );
                            }
                        }
                        $.filenameClear();
                    }).css('background-color', '#C7FFFF');
                }
                $('th').eq(8).css('width', '14%');
            });
        }
    }
    else {
        // 只对"博硕论文"详情页做优化, 否则影响期刊页面的显示
        // 来自: https://greasyfork.org/zh-CN/scripts/371938
        if (location.search.match(/dbcode=C[DM][FM]D/i) || $('.btn-html').html() == undefined) {
            $(".btn-dlcaj").first().html($(".btn-dlcaj").first().html().replace("CAJ整本", "CAJ"));
            $(".btn-dlpdf").first().html($(".btn-dlpdf").first().html().replace("PDF整本", "PDF"));
        }
        // 移除关于CAJ的信息
        $(".fl.info").find("div").remove();
    }
});

// https://stackoverflow.com/a/951057/7795128
function sleep(time) {
  return new Promise((resolve) => setTimeout(resolve, time));
}

function get_content(cnt_list){
    var contents = "";
    cnt_list.children().each(function () {
        var cnt_item = $(this).find('a').text();
        var cnt_page = $(this).find('.page').text().split('-')[0];
        var prev_space_length = $(this).attr('class').split('-')[1] * 4;
        contents = contents + cnt_item.trim().replace(/&nbsp;/g, " ").padStart(prev_space_length + cnt_item.trim().length, ' ') + "\t" + cnt_page + "\r\n";
    })
    return contents;
}

// 来自: https://greasyfork.org/zh-CN/scripts/371938
function manage_contents(xhr) {
    var cnt_list = $(".ls-chapters", xhr.responseText);
    var contents = get_content(cnt_list); // 目录内容
    // 添加目录复制
    $('.btn-dlpdf').first().after($('<li class="btn-dlpdf"><a href="javascript:void(0);">目录复制</a></li>').click(function() {
        GM_setClipboard(contents); // 运用油猴脚本自带的复制函数
        GM_notification('目录已复制到剪贴板');
    }));
    // 添加目录下载
    $('.btn-dlpdf').first().after($('<li class="btn-dlcaj"><a>目录下载</a></li>').click(function() {
        var data = new Blob([contents],{type:"text/plain; charset=UTF-8"});
        $(this).find('a').attr("download", '目录_' + $('.wx-tit h1:first-child()').text().trim() + '.txt');
        $(this).find('a').attr("href", window.URL.createObjectURL(data));
        window.URL.revokeObjectURL(data);
    }));
}