// ==UserScript==
// @name 中国知网CNKI硕博论文PDF下载
// @version 1.1.4
// @namespace https://greasyfork.org/users/244539
// @icon https://www.cnki.net/favicon.ico
// @description 添加知网文献、硕博论文PDF下载按钮,支持搜索列表、详情页,下载论文章节目录,批量下载文献,一键切换CAJ和PDF格式
// @author zoglmk
// @match http*://*.cnki.net
// @match http*://cdmd.cnki.com.cn/Article/CDMD-*
// @match http*://*/kcms/detail/detail.aspx?*dbcode=*
// @match http*://*/*/*/kcms/detail/detail.aspx?*dbcode=*
// @match http*://*/*/*/kcms/detail*
// @match http*://*/https/*/kcms*
// @match http*://*/KCMS/detail/detail.aspx?*dbcode=*
// @match http*://*/kns*/defaultresult/index*
// @match http*://*/https/*/kns8/defaultresult/index
// @match http*://*/https/*/kcms/detail/detail.aspx?
// @match http*://*/KNS8/AdvSearch?*
// @match http*://*/kns8/AdvSearch?*
// @match http*://*/kns/brief/*result*
// @match http*://*/kcms/Detail/DownDetail.aspx*
// @match http*://*/KNS8/DefaultResult/index*
// @match http*://*/kns8/DefaultResult/Index*
// @match http*://*/kns8/defaultresult/index*
// @match http*://*/https/*/KNS8/DefaultResult/*
// @match http*://*/kcms2/article/abstract?*
// @match http*://如果没有匹配到可以尝试在这里填写域名,或者按照上面格式填写关键词,然后保存,刷新重试/*
// @run-at document-idle
// @grant unsafeWindow
// @license MIT
// @downloadURL none
// ==/UserScript==
//文献详情页面
var $ = unsafeWindow.jQuery;
(function() {
'use strict';
function saveFile(name,data) { // 生成目录txt
const blob = new Blob([data],{type:'text/plain'});
const link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = name + ".txt";
link.click();
window.URL.revokeObjectURL(link.href);
}
function add_cate_dl_btn() { // 添加目录下载按钮
var other_btns = document.getElementsByClassName('other-btns')[0];
var li2 = document.createElement('li');
var a2 = document.createElement('a');
li2.className = "btn-diy";
li2.style = "width:auto;height:23px;line-height:22px;background-color:#3f8af0;border-radius:3px;";
a2.innerHTML = "目录下载";
a2.className = "a-diy";
a2.style = "color:#ffffff;padding: 2px 10px;";
a2.href = "javascript:void(0)";
li2.appendChild(a2);
other_btns.appendChild(li2);
}
function get_cate(url){
var content = "";
$.ajax({ // 从分章下载页获取目录
url:url,
type:"get",
dataType:'html',
async:false,
success:function(data){
var cate_ul = $('.ls-chapters',data)[0];
var title = cate_ul.getElementsByClassName('txt')
var page = cate_ul.getElementsByClassName('page')
$.each(title,function(index,val){
content += $(title[index]).text() + " " + $(page[index]).text().split("-")[0] + "\n";
});
}
});
// console.log(content);
return content;
}
var url = window.location.href;
if(url.indexOf('detail.aspx') != -1 || url.indexOf('abstract') != -1 ){
var wx_title = document.getElementsByClassName('wx-tit')[0].innerText;
if(document.getElementsByClassName('catalog-list').length>0){ // 获取目录
var catalog_url = document.getElementsByClassName('operate-btn')[0].getElementsByTagName('a')[3].href; // 分章下载链接
var cate_content = get_cate(catalog_url);
// console.log(cate_content);
add_cate_dl_btn(); // 添加目录下载按钮
}
$(document).on("click",".a-diy",function(){saveFile(wx_title,cate_content)}); // 下载目录按钮监听
var dllink = document.getElementsByClassName('operate-btn');
var tmp_id = document.getElementsByClassName('btn-phone')[0].getElementsByTagName('a')[0].href;
console.log(/id=(.*?)&/.exec(tmp_id)[1]);
var dlurl = JSON.parse(localStorage.getItem("fnlist"))["tmpfn_" + /id=(.*?)&/.exec(tmp_id)[1]];
for (var i = 0;i < dllink.length; i++) { // 添加PDF下载按钮
var li = document.createElement('li');
var a1 = document.createElement('a');
li.className = "btn-dlpdf";
a1.innerHTML = "PDF 下载";
a1.href = dlurl;
a1.style.backgroundColor="#fb4376";
li.appendChild(a1);
dllink[i].appendChild(li);
}
};
if(url.indexOf('DownDetail.aspx') != -1){
function add_cate_dl_btn() { // 添加目录下载按钮
var other_btns = document.getElementsByClassName('title')[0];
var li2 = document.createElement('li');
var a2 = document.createElement('a');
li2.className = "btn-diy";
li2.style = "width:auto;height:23px;line-height:22px;background-color:#3f8af0;border-radius:3px;list-style: none;";
a2.innerHTML = "目录下载";
a2.className = "a-diy";
a2.style = "color:#ffffff;padding: 2px 10px;";
a2.href = "javascript:void(0)";
li2.appendChild(a2);
other_btns.appendChild(li2);
}
add_cate_dl_btn();
var content = "";
var ul = $('.ls-chapters')[0];
var title = ul.getElementsByClassName('txt');
var page = ul.getElementsByClassName('page');
$.each(title,function(index,val){
content += $(title[index]).text() + ".................................................." + $(page[index]).text() + "\n";
});
var filename = document.getElementsByClassName('title')[0].innerText;
$(document).on("click",".a-diy",function(){saveFile(filename,content)}); // 下载目录按钮监听
};
})();
// 知网空间学位论文
// 可能基本没啥用
(function() {
var url = window.location.href;
if(url.indexOf('cdmd.cnki.com.cn') != -1){
var cnki_space_id = url.match(/-([0-9]+).htm/)[1];
console.log(cnki_space_id);
var ty_caj = document.getElementById("ty_caj");
var newe = document.createElement("div");
var newe_a = document.createElement('a');
newe.className = "down_button";
newe.id = "ty_pdf";
newe_a.innerHTML = "PDF 下载(跳)";
newe_a.href = "https://kns.cnki.net/kcms/detail/detail.aspx?dbcode=CMFD&dbname=CMFDTEMP&filename=" + cnki_space_id + ".nh";
newe_a.target = "_blank";
newe.appendChild(newe_a);
document.getElementById("down_3").insertBefore(newe,ty_caj);
}
})();
// 搜索结果页面
(function() {
function downloadFile(url) {
window.open(url,'_blank')
}
function getOverseaUrl(){
var oversea_url = $('#OverSeaURL').attr('value').split("/")[2];
// console.log(oversea_url);
return oversea_url;
}
function showToast(remainingTime, duration = 3000) {
let toast = document.getElementById("toast");
toast.innerHTML = "请等待 1 分钟后再执行批量下载。\n\n剩余时间:" + remainingTime + "秒";
toast.style.display = "block";
setTimeout(function() {
toast.style.display = "none";
}, duration);
let interval = setInterval(function() {
remainingTime--;
toast.innerHTML = "请等待 1 分钟后再执行批量下载。\n\n剩余时间:" + remainingTime + "秒";
if (remainingTime <= 0) {
clearInterval(interval);
toast.style.display = "none";
}
}, 1000);
}
function get_chs(url){ // 返回简中域名
var chs_url = "";
let cnki_reg = /(?[a-z0-9A-Z]+)(?[._-])cnki(?[._-])net/gi;
if(url == "oversea.cnki.net"){
chs_url = "chn." + url;
}else if(url.match(".*cnki-net.*") || url.match(".*webvpn.*")){
chs_url = "chn-" + url.replace("net","net-s");
}else if(url.match(cnki_reg)){
chs_url = url.replace(cnki_reg,"chn$oversea$cnki$net");
}else{
// console.log("4: ",chs_url)
};// 如还有其它未匹配格式添加在此处
// console.log(chs_url);
return chs_url;
}
var over_url = get_chs(getOverseaUrl());
function getlist(){
var l = localStorage.getItem("FileNameS").split(",");
var urls = [];
if (l == ""){
urls = [];
au = [];
}else{
$.each(l,function(index,val){
var u = $("input[value='"+ l[index] +"']").parent("td").parent("tr").find(".downloadlink")[0].href;
urls.push(u);
});
}
au = urls;
}
var au = [];
$(document).ajaxSuccess(function() {
if (arguments[2].url.indexOf('/Brief/GetGridTableHtml') + 1) {
$('.downloadlink').attr('href',function(){return this.href.replace(this.href.split("/")[2],over_url)});
// 传值
localStorage.removeItem("fnlist");
var dllink = document.getElementsByClassName('cbItem');
var simgle_name = {};
$.each(dllink,function(index){
var u = $("input[value='"+ dllink[index].value +"']").parent("td").parent("tr").find(".downloadlink")[0].href;
//console.log(u);
simgle_name["tmpfn_" + dllink[index].value.split("!")[1]] = u;
localStorage.setItem("fnlist",JSON.stringify(simgle_name));
});
// console.log(localStorage.getItem("fnlist"));
$(document).on("click","#selectCheckAll1",function(){
getlist();
})
$("tr input[name='CookieName']").click(function(){
getlist();
})
$(".toolbar-col").after(``);
};
//*** 格式下载开关
$(".diy-choose-btn").each(function(){
var texts = $(this).attr('data-toggle').split('|');
$(this).siblings('.choose-text').text(this.checked?texts[0]:texts[1]);
$("#dd").text(this.checked?"批量下载"+texts[0]:"批量下载"+texts[1]);
});
$(".diy-choose-btn").off("change");
$(".diy-choose-btn").on("change", function(){
var texts = $(this).attr('data-toggle').split('|');
$(this).siblings('.choose-text').text(this.checked?texts[0]:texts[1]);
$("#dd").text(this.checked?"批量下载"+texts[0]:"批量下载"+texts[1]);
if(this.checked){
$('.downloadlink').attr('href',function(){return this.href.replace(this.href.split("/")[2],over_url)});
}else{
$('.downloadlink').attr('href',function(){return this.href.replace(this.href.split("/")[2], window.location.host)});
}
});
});
let isOpen = false;
$(document).on("click","#dd",function(){
//console.log(au);
if(au.length == 0){
alert("未选择!\n\n出现此提示,请刷新网页重新搜索,并先点击【清除全部】,然后再多选。\n\n为避免批量下载导致知网封锁IP,每次最多选择 5 篇。");
isOpen = false;
return false;
}else if(au.length > 5){
alert("为避免批量下载导致知网封锁IP,每次最多选择 5 篇。\n\n间隔时间:1 分钟。");
isOpen = false;
return false;
}else{
if (localStorage.getItem("lastExecuted")) {
// 获取上次执行的时间戳
const lastExecuted = localStorage.getItem("lastExecuted");
const now = new Date().getTime();
if (now - lastExecuted < 60000 && isOpen == true) {
showToast((60 - Math.floor((now - lastExecuted) / 1000))); // 显示浮动提示框
return;
}else{
for(var i=0;i