Greasy Fork

来自缓存

Greasy Fork is available in English.

知网下载助手

解析CNKI论文PDF格式下载地址,论文搜索结果页面和硕博论文详述页面的caj格式下载链接替换为pdf格式下载链接,知网空间页面增加pdf格式下载链接

当前为 2020-12-06 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         知网下载助手
// @namespace    [email protected]
// @version      1.8.0
// @description  解析CNKI论文PDF格式下载地址,论文搜索结果页面和硕博论文详述页面的caj格式下载链接替换为pdf格式下载链接,知网空间页面增加pdf格式下载链接
// @author       wyn665817
// @match        *://*.cnki.net/*
// @include      */brief.aspx*
// @include      */detail.aspx*
// @include      */DefaultResult/Index*
// @include      */CatalogViewPage.aspx*
// @include      */Article/*
// @connect      cnki.net
// @run-at       document-end
// @grant        unsafeWindow
// @grant        GM_xmlhttpRequest
// @grant        GM_setClipboard
// @grant        GM_registerMenuCommand
// @supportURL   http://greasyfork.icu/zh-CN/scripts/371938/feedback
// @license      MIT
// ==/UserScript==

var $ = unsafeWindow.jQuery,
url = location.pathname,
i = 0;

if (!$) {
} else if (url.match(/brief\.aspx$/)) {
    url = $('.briefDl_Y, .briefDl_D', '.GridTableContent').attr('href', reUrl);
    GM_registerMenuCommand('切换下载链接', change);
} else if (url.match(/defaultresult\/index$/i)) {
    $(document).ajaxSuccess(function() {
        if (arguments[2].url.indexOf('/Brief/GetGridTableHtml') + 1) url = $('.downloadlink').attr('href', reUrl);
    });
    GM_registerMenuCommand('切换下载链接', change);
} else if (url.match(/detail\.aspx$/) && location.search.match(/dbcode=C[DM][FM]D&/i)) {
    url = $('a:contains(分章下载)').attr('href') || '?';
    url = 'https://chn.oversea.cnki.net/kcms/download.aspx' + url.replace(/%20/g, '').match(/\?.*/)[0];
    GM_xmlhttpRequest({method: 'GET', url: url, onload: done});
    url = $('.operate-btn a').attr('href', function() {
        var tip = $(this).text().trim();
        if (!tip.match(/^分[页章]下载$/)) return tip == '整本下载' ? $(this).attr('href', reUrl).data('PDF') : this.href;
        tip = this.href.replace(/kns8?(?!\/)/, 'chn.oversea').replace(/%20/g, '').replace(/kcms/, 'kns').replace(/\.aspx/, '');
        return $(this).data('CAJ', this.href).data('PDF', tip + '&cflag=pdf').data('PDF');
    });
    GM_registerMenuCommand('切换下载链接', change);
} else if (url.match(/CatalogViewPage\.aspx$/)) {
    $('#downLoadFile img').last().attr('src', function() {
        return this.src.replace('CAJ', 'PDF');
    }).next().attr('href', reUrl).text('PDF全文下载').attr('title', 'PDF全文下载');
} else if (url.match(/^\/Article\//) && !$('#ty_pdf').length) {
    var $dom = $('#ty_caj').clone().insertBefore('#ty_caj');
    $dom.attr('id', 'ty_pdf').find('a').attr('href', reUrl).text('PDF全文下载');
    $dom.parent().attr('id', 'down_1');
}

function reUrl(index, href) {
    i = 0;
    return $(this).data('CAJ', href).data('PDF', href.replace(/&dflag=\w*|$/, '&dflag=pdfdown')).data('PDF');
}

function change() {
    var type = ['PDF', 'CAJ'][++i % 2];
    url.attr('href', function() {
        return $(this).data(type) || this.href;
    });
}

function done(xhr) {
    var list = $('tr', xhr.responseText).map(function() {
        var $dom = $(this).find('a, td:last');
        return $dom.eq(0).html().trim().replace(/ /g, ' ') + '\t' + $dom.eq(1).text().trim().split('-')[0];
    }).get().join('\r\n').replace(/ {4}/g, '\t'),
    blob = new Blob([list]);
    $('<li class="btn-dlpdf"><a href="javascript:void(0);">复制目录</a></li>').prependTo('.operate-btn').click(function() {
        GM_setClipboard(list);
        alert('目录已复制到剪贴板');
    }).toggle(!!list);
    $('<li class="btn-dlcaj"><a>下载目录</a></li>').prependTo('.operate-btn').toggle(!!list).children().each(function() {
        this.download = $('.wx-tit h1').text().trim() + '_目录.txt';
        this.href = URL.createObjectURL(blob);
    }).css('margin-right', '3px');
}