Greasy Fork 还支持 简体中文。

知网下载工具(支持Endnote+PDF)

1.支持学位论文PDF导出; 2.支持在论文详情页直接导出RIS!

2021-04-16 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         知网下载工具(支持Endnote+PDF)
// @namespace    https://blog.cuger.cn
// @version      0.2.1
// @description  1.支持学位论文PDF导出; 2.支持在论文详情页直接导出RIS!
// @author       Dorad
// @include      https://kns.cnki.net/kns8/defaultresult/index
// @include      https://kns.cnki.net/kcms/detail/detail.aspx*
// @run-at       document-idle
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        GM_downlaod
// @grant        GM_xmlhttpRequest
// ==/UserScript==


(function() {
    var site=window.location.href.toString();
    if(site.indexOf('kcms/detail/detail.aspx')!=-1){
        // 详情页
        var operateBtns = document.getElementById('DownLoadParts').getElementsByClassName('operate-btn')[0];
        //operateBtns.removeChild(operateBtns[0]);
        console.log(operateBtns);
        /*
        * RIS Export
        */
        // get fileId
        var fileId = document.getElementById('paramfilename').value;
        var dbName = document.getElementById('paramdbname').value;
        var title = document.getElementsByClassName('wx-tit')[0].children[0].text;
        var risExportBtn = document.createElement('li');
        risExportBtn.setAttribute("class", "btn-dlpdf");
        risExportBtn.innerHTML = '<a>EndNote导出</a>';
        risExportBtn.onclick = function(){
            console.log('RIS Export: '+title+', '+fileId+', '+dbName);
            downloadByFilename(fileId, dbName, title);
        }
        operateBtns.append(risExportBtn);
        pdfDownload();
    }else{
        // 搜索页
        let wrapper = document.getElementsByClassName('wrapper')[0];
        console.log(wrapper);
        var observer = new MutationObserver(function(changeList){
            updateRowItems();
            // console.log(changeList);
        })
        observer.observe(wrapper,{
            attributes: true,
            subtree: true,
            characterData: true,
        });
    }
})();


function updateRowItems(){
    if(document.getElementsByClassName('result-table-list').length==0){
        return;
    }
    var table = document.getElementsByClassName('result-table-list')[0];
    // console.log(table)
    var rows=table.getElementsByTagName('tbody')[0].getElementsByTagName('tr');
    for(var i=1;i<rows.length;i++){
        var row = rows[i];
        if(row.getAttribute('ris')){
            continue;
        }else{
            var fileId = row.getElementsByClassName('seq')[0].children[0].value;
            var operats = row.getElementsByClassName('operat')[0];
            // replace the download with pdf
            var downloadPdf = operats.children[0];
            // replace the html read with ris export
            var exportRis = operats.children[1];
            console.log(fileId);
            row.setAttribute('ris',true);
        }
    }
}

function downloadByFilename(fileId, dbName, name){
    GM_xmlhttpRequest({
        method: "POST",
        url:"https://kns.cnki.net/KNS8/manage/ShowExport",
        data: "filename="+fileId+"&displaymode=EndNote&orderparam=0&ordertype=desc&selectfield=&dbname="+dbName+"&random="+Math.random(),
        headers: {
            'Connection': 'keep-alive',
            'Accept': 'text/plain, */*; q=0.01',
            'X-Requested-With': 'XMLHttpRequest',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36',
            'Content-Type': 'application/x-www-form-urlencoded',
            'Origin': 'https://kns.cnki.net',
            'Sec-Fetch-Site': 'same-origin',
            'Sec-Fetch-Mode': 'cors'
        },
        onload: function(res) {
            if (res.status == 200) {
                var text = res.responseText;
                console.log(text);
                var a = document.createElement('a');
                var objE = document.createElement("div");
                objE.innerHTML = text;
                text = objE.firstElementChild.firstElementChild.innerHTML;
                text=text.replaceAll('<br>','\r\n')
                console.log(text);
                // a.href = 'data:application/x-EndNote-tagged; charset=utf-8,' + encodeURIComponent(text);
                a.href = 'data:application/x-EndNote-tagged; charset=utf-8,' + encodeURIComponent(text);
                //supported by chrome 14+ and firefox 20+
                a.download = name+'.ris';
                //needed for firefox
                document.getElementsByTagName('body')[0].appendChild(a);
                //supported by chrome 20+ and firefox 5+
                a.click();
            }
        }
    });
}

function pdfDownload(){
    var allLis, thisLi, newLi, aPDF, bPDF, allLinks, thisLink, pageType, pfType, myurl, i;
    pageType = true;
    pfType = true;
    myurl = window.location.href;

    allLinks = document.evaluate(
        '//a[@href]',
        document,
        null,
        XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
        null);

    allLis = document.evaluate(
        "//li[@class]",
        document,
        null,
        XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
        null);

    if (myurl.indexOf("detail.aspx") != -1) {
        pageType = false;
    } //false 为详情页面
    if (document.title.indexOf(" - 中国知网") != -1) {
        pfType = false;
    } //false 为新平台

    if ((pfType === true) & (pageType === false)) {
        for (i = 0; i < allLis.snapshotLength; i++) {
            thisLi = allLis.snapshotItem(i);
            if (thisLi.getAttribute("class").indexOf("readol") != -1) {
                newLi = document.createElement('li');
                newLi.setAttribute("class", "pdf");
                aPDF = '<a target="_blank" href="' + thisLi.firstChild.href.replace("&dflag=readonline", "&dflag=pdfdown") + '">PDF下载</a>';
                newLi.innerHTML = aPDF;
                thisLi.parentNode.insertBefore(newLi, thisLi.nextSibling);
            }
        }
    }

    if ((pfType === false) & (pageType === false)) {
        for (i = 0; i < allLinks.snapshotLength; i++) {
            thisLink = allLinks.snapshotItem(i);
            if (thisLink.href && thisLink.href.indexOf("download.aspx?filename=") != -1 && thisLink.innerHTML.indexOf("整本") != -1) {
                thisLink.innerHTML = thisLink.innerHTML.replace("整本", "CAJ");
                bPDF = thisLink.href;
            }
            if (thisLink.href && thisLink.href.indexOf("download.aspx?filename=") != -1 && thisLink.innerHTML.indexOf("分页") != -1) {
                thisLink.innerHTML = thisLink.innerHTML.replace("分页", "PDF");
                thisLink.href = bPDF.replace("nhdown", "pdfdown");
            }
        }
    }

    if (pageType === true) {
        for (i = 0; i < allLinks.snapshotLength; i++) {
            thisLink = allLinks.snapshotItem(i);
            if (thisLink.href && thisLink.href.indexOf("download.aspx?filename=") != -1 && thisLink.href.indexOf("&dflag") == -1) {
                thisLink.href = thisLink.href + "&dflag=pdfdown";
            }
            if (thisLink.href && thisLink.href.indexOf("download.aspx?filename=") != -1 && thisLink.href.indexOf("&dflag=nhdown") != -1) {
                thisLink.href = thisLink.href.replace("nhdown", "pdfdown");
            }
            if (thisLink.href && thisLink.href.indexOf("download.aspx?filename=") != -1 && thisLink.href.indexOf("&dflag=pdfdown") != -1 && (thisLink.href.indexOf("=CMFD") != -1 || thisLink.href.indexOf("=CDFD") != -1 )) {
                thisLink.href = thisLink.href.replace("pdfdown", "nhdown");
            }
        }
    }
}