// ==UserScript==
// @name CNKI Batch Downloader (Bilingual)
// @name:zh-CN 知网CNKI论文PDF批量下载-双语版
// @namespace https://greasyfork.org/zh-CN/users/236397-hust-hzb
// @version 1.0
// @icon https://www.cnki.net/favicon.ico
// @description Batch download CNKI papers/theses PDF (Bilingual, Smart monitoring, Auto verification)
// @description:zh-CN 知网文献、硕博论文PDF批量下载 (中英双语,智能驻守,自动核对)
// @author HUST HuangZhenbin
// @license MIT
// @match *://*.cnki.net/*
// @run-at document-idle
// @grant unsafeWindow
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_xmlhttpRequest
// @grant GM_download
// @grant GM_registerMenuCommand
// @downloadURL https://update.greasyfork.icu/scripts/560374/CNKI%20Batch%20Downloader%20%28Bilingual%29.user.js
// @updateURL https://update.greasyfork.icu/scripts/560374/CNKI%20Batch%20Downloader%20%28Bilingual%29.meta.js
// ==/UserScript==
(function() {
'use strict';
// --- 配置与状态 ---
let useWebVPN = GM_getValue('useWebVPN', false);
// 默认语言检测:如果浏览器语言包含 'zh' 则为 'zh',否则为 'en'
const defaultLang = navigator.language.includes('zh') ? 'zh' : 'en';
let currentLang = GM_getValue('cnki_lang', defaultLang);
const DEFAULT_MIN_DELAY = 5000;
const DEFAULT_MAX_DELAY = 10000;
const DEFAULT_FOLDER = "CNKI_Downloads";
let isRunning = false;
let lastCheckedIndex = null;
// --- 国际化文本字典 ---
const i18n = {
zh: {
title: "📚 CNKI 批量下载助手",
version: "v1.0",
close: "关闭",
guide_title: "使用前请务必检查以下配置,否则无法自动下载:",
guide_browser: "浏览器设置:请关闭“下载前询问每个文件的保存位置”(设置 -> 下载 -> 询问保存位置 -> 关)。",
guide_tamper: "油猴权限:请允许 Tampermonkey 扩展访问“管理下载”权限(扩展管理 -> 详情 -> 允许访问文件URL/下载)。",
guide_overwrite: "文件去重:下载时若本地存在同名文件,脚本将直接替换覆盖。",
mask_title: "任务已暂停:需要人工验证",
mask_desc: "检测到知网验证码拦截。
已为您自动打开验证窗口,请在新窗口中手动点击下载并完成滑块验证。
验证成功且开始下载后,关闭那个窗口,回来点击下方按钮。",
btn_resume: "✅ 我已解除,继续下载",
btn_stop_task: "⏹ 停止任务",
report_title: "📊 下载结果核对报告",
report_retry: "🔄 重试失败项目",
report_close: "关闭报告",
label_folder: "📂 归档文件夹:",
label_vpn: "开启 WebVPN 模式",
btn_scan: "🔍 1. 扫描当前页",
btn_start: "▶ 2. 开始下载选中",
btn_verify: "🛠️ 仅打开验证页",
btn_clear: "🗑 清空列表",
btn_reset_history: "🧹 清除历史记录",
tip_shift: "💡 提示: 按住 Shift 键点击复选框可进行批量多选。文件名纯净,同名文件将自动覆盖。",
th_check: "选",
th_no: "No.",
th_title: "标题",
th_author: "作者/来源",
th_status: "状态",
status_wait: "待下载",
status_done: "✔ 完成",
status_error: "✘ 失败",
status_pay: "💰 需付费",
status_nopdf: "⚪ 无PDF",
status_exists: "🔁 已存在",
status_running: "⟳ 解析中...",
status_downloading: "⬇ 下载中...",
status_skip: "⚠ 跳过",
status_ready: "等待操作...",
status_stopped: "🚫 任务已手动停止",
status_scanned: "扫描完成,新增 {new} 条,共 {total} 条。",
status_total: "列表共 {total} 条文献",
status_finished: "✅ 批量任务完成",
alert_no_item: "未找到文献,请确保在搜索结果页",
alert_no_check: "请先勾选需要下载的文献",
alert_history_clear: "确定要清除所有下载历史记录吗?\n这将导致脚本不再跳过同名文件。",
alert_history_done: "历史记录已清除。",
report_success: "成功",
report_exists: "已存在",
report_pay: "需付费",
report_nopdf: "无PDF",
report_fail: "失败",
report_msg_check: "请检查以下失败项目:",
report_msg_none: "🎉 没有下载失败的项目",
main_btn: "CNKI批量导出",
err_captcha: "触发验证码",
err_no_auth: "无权限/收费",
err_download_fail: "下载失败",
cool_down: "冷却"
},
en: {
title: "📚 CNKI Batch Downloader",
version: "v1.0",
close: "Close",
guide_title: "Please check the following configurations before use:",
guide_browser: "Browser: Disable 'Ask where to save each file before downloading' (Settings -> Downloads).",
guide_tamper: "Tampermonkey: Allow 'Manage Downloads' permission (Extension Management -> Details).",
guide_overwrite: "Overwrite: If a file with the same name exists locally, it will be overwritten.",
mask_title: "Task Paused: Manual Verification Required",
mask_desc: "CNKI CAPTCHA detected.
A verification window has been opened. Please manually click download and solve the slider in the new window.
After success, close that window and click the button below.",
btn_resume: "✅ I've Solved it, Continue",
btn_stop_task: "⏹ Stop Task",
report_title: "📊 Download Result Report",
report_retry: "🔄 Retry Failed Items",
report_close: "Close Report",
label_folder: "📂 Folder:",
label_vpn: "Enable WebVPN Mode",
btn_scan: "🔍 1. Scan Page",
btn_start: "▶ 2. Start Download",
btn_verify: "🛠️ Open Verify Page",
btn_clear: "🗑 Clear List",
btn_reset_history: "🧹 Reset History",
tip_shift: "💡 Tip: Hold Shift to select multiple items. Filenames are clean and will overwrite duplicates.",
th_check: "chk",
th_no: "No.",
th_title: "Title",
th_author: "Author/Source",
th_status: "Status",
status_wait: "Waiting",
status_done: "✔ Done",
status_error: "✘ Failed",
status_pay: "💰 Pay Req",
status_nopdf: "⚪ No PDF",
status_exists: "🔁 Exists",
status_running: "⟳ Parsing...",
status_downloading: "⬇ Downloading...",
status_skip: "⚠ Skipped",
status_ready: "Ready...",
status_stopped: "🚫 Task Stopped",
status_scanned: "Scanned, added {new}, total {total}.",
status_total: "Total {total} items",
status_finished: "✅ Batch Task Completed",
alert_no_item: "No papers found. Please use on search result page.",
alert_no_check: "Please select items first.",
alert_history_clear: "Are you sure to clear download history?\nThis will cause re-downloading of existing files.",
alert_history_done: "History cleared.",
report_success: "Success",
report_exists: "Exists",
report_pay: "Pay Req",
report_nopdf: "No PDF",
report_fail: "Failed",
report_msg_check: "Please check failed items:",
report_msg_none: "🎉 No failed items",
main_btn: "CNKI Export",
err_captcha: "Captcha Triggered",
err_no_auth: "No Auth/Paid",
err_download_fail: "Download Failed",
cool_down: "Cooldown"
}
};
function t(key) {
return i18n[currentLang][key] || key;
}
function toggleLang() {
currentLang = currentLang === 'zh' ? 'en' : 'zh';
GM_setValue('cnki_lang', currentLang);
// 移除旧界面重新渲染
const overlay = document.getElementById('cnki-overlay');
if (overlay) overlay.remove();
openDashboard();
}
// --- CSS ---
function injectStyle() {
if (document.getElementById('cnki-style')) return;
const style = document.createElement('style');
style.id = 'cnki-style';
style.textContent = `
.cnki-ui-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); backdrop-filter: blur(3px); z-index: 99999; display: flex; justify-content: center; align-items: center; }
.cnki-ui-modal { background: #fff; width: 950px; height: 90vh; border-radius: 12px; box-shadow: 0 15px 40px rgba(0,0,0,0.25); display: flex; flex-direction: column; overflow: hidden; font-family: "Microsoft YaHei", sans-serif; animation: fadeIn 0.3s ease; position: relative;}
@keyframes fadeIn { from { opacity: 0; transform: scale(0.95); } to { opacity: 1; transform: scale(1); } }
.cnki-ui-header { padding: 15px 25px; border-bottom: 1px solid #eee; display: flex; justify-content: space-between; align-items: center; background: #f8f9fa; }
.cnki-ui-title { font-size: 18px; font-weight: bold; color: #333; display: flex; align-items: center; gap: 8px; }
.cnki-ui-close { cursor: pointer; border: none; background: none; font-size: 24px; color: #999; transition: color 0.2s; }
.cnki-ui-close:hover { color: #333; }
.cnki-lang-btn { font-size: 12px; background: #e0f2fe; color: #0284c7; border: 1px solid #bae6fd; padding: 2px 8px; border-radius: 4px; cursor: pointer; margin-right: 10px; }
.cnki-config-guide { background: #fff1f2; border-bottom: 1px solid #fecdd3; padding: 12px 25px; font-size: 13px; color: #881337; line-height: 1.6; display: flex; gap: 10px; align-items: flex-start; }
.cnki-guide-icon { font-size: 18px; }
.cnki-guide-list { margin: 0; padding-left: 20px; }
.cnki-ui-toolbar { padding: 15px 25px; border-bottom: 1px solid #eee; background: #fff; display: flex; flex-direction: column; gap: 12px; }
.cnki-row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.cnki-ui-btn { padding: 8px 16px; border-radius: 6px; border: 1px solid #ddd; background: #fff; cursor: pointer; font-size: 13px; font-weight: 500; transition: all 0.2s; display: flex; align-items: center; gap: 5px; }
.cnki-ui-btn:hover { background: #f3f4f6; transform: translateY(-1px); }
.cnki-ui-btn:active { transform: translateY(0); }
.cnki-btn-primary { background: #3b82f6; color: #fff; border-color: #3b82f6; box-shadow: 0 2px 5px rgba(59,130,246,0.3); }
.cnki-btn-primary:hover { background: #2563eb; }
.cnki-btn-warn { background: #f59e0b; color: #fff; border-color: #f59e0b; }
.cnki-btn-warn:hover { background: #d97706; }
.cnki-btn-danger { background: #ef4444; color: #fff; border-color: #ef4444; }
.cnki-btn-danger:hover { background: #dc2626; }
.cnki-btn-info { background: #0ea5e9; color: #fff; border-color: #0ea5e9; }
.cnki-btn-info:hover { background: #0284c7; }
.cnki-input-group { display: flex; align-items: center; gap: 8px; font-size: 13px; color: #555; background: #f9fafb; padding: 5px 10px; border-radius: 6px; border: 1px solid #e5e7eb; }
.cnki-input { padding: 4px 8px; border: 1px solid #ccc; border-radius: 4px; width: 160px; font-size: 13px; }
.cnki-input:focus { border-color: #3b82f6; outline: none; }
.cnki-table-wrap { flex: 1; overflow-y: auto; padding: 0; background: #fdfdfd; }
.cnki-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.cnki-table th { position: sticky; top: 0; background: #f1f5f9; padding: 12px 15px; text-align: left; color: #475569; font-weight: 600; border-bottom: 1px solid #e2e8f0; z-index: 10; }
.cnki-table td { padding: 10px 15px; border-bottom: 1px solid #f1f5f9; color: #334155; vertical-align: middle; }
.cnki-table tr:hover { background: #f8fafc; }
.cnki-row-selected { background: #eff6ff !important; }
.cnki-footer { padding: 10px 25px; border-top: 1px solid #eee; background: #f8f9fa; font-size: 12px; color: #666; display: flex; justify-content: space-between; align-items: center; }
.cnki-orcid-link { color: #a3d014; text-decoration: none; display: flex; align-items: center; gap: 5px; font-weight: bold; }
.cnki-orcid-link:hover { text-decoration: underline; }
.cnki-pause-mask { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: rgba(255,255,255,0.95); z-index: 20; display: none; flex-direction: column; justify-content: center; align-items: center; gap: 20px; }
.cnki-pause-box { background: white; padding: 30px; border-radius: 12px; box-shadow: 0 10px 25px rgba(0,0,0,0.1); border: 1px solid #eee; text-align: center; max-width: 450px; }
/* 结果报告遮罩 */
.cnki-report-mask { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.6); z-index: 30; display: none; justify-content: center; align-items: center; }
.cnki-report-box { background: white; width: 650px; max-height: 85%; border-radius: 12px; display: flex; flex-direction: column; overflow: hidden; box-shadow: 0 20px 50px rgba(0,0,0,0.3); animation: fadeIn 0.2s ease; }
.cnki-report-header { padding: 20px; background: #f0fdf4; border-bottom: 1px solid #dcfce7; }
.cnki-report-header.has-error { background: #fef2f2; border-bottom: 1px solid #fee2e2; }
.cnki-report-list { flex: 1; overflow-y: auto; padding: 20px; }
.cnki-report-item { padding: 10px; border-bottom: 1px solid #eee; display: flex; justify-content: space-between; font-size: 13px; align-items: center; }
.cnki-report-item:last-child { border-bottom: none; }
.cnki-report-status-fail { color: #dc2626; font-weight: bold; background: #fef2f2; padding: 2px 6px; border-radius: 4px; font-size: 12px;}
.cnki-report-status-pay { color: #b45309; font-weight: bold; background: #fffbeb; padding: 2px 6px; border-radius: 4px; font-size: 12px;}
.cnki-report-status-nopdf { color: #6b7280; font-weight: bold; background: #f3f4f6; padding: 2px 6px; border-radius: 4px; font-size: 12px;}
.cnki-report-btn { padding: 15px; text-align: right; border-top: 1px solid #eee; background: #fff; display: flex; justify-content: flex-end; gap: 10px;}
/* 主按钮 */
.cnki-main-btn { position: fixed; bottom: 60px; right: 40px; padding: 12px 20px; border-radius: 50px; background: #3b82f6; color: white; border: none; box-shadow: 0 4px 15px rgba(59,130,246,0.4); cursor: pointer; z-index: 2147483647 !important; display: flex; align-items: center; gap: 8px; font-size: 14px; font-weight: bold; transition: all 0.2s; }
.cnki-main-btn:hover { transform: translateY(-2px); background: #2563eb; }
.cnki-status-wait { color: #94a3b8; }
.cnki-status-run { color: #3b82f6; font-weight: bold; }
.cnki-status-ok { color: #16a34a; font-weight: bold; }
.cnki-status-err { color: #ef4444; font-weight: bold; }
.cnki-status-pay { color: #f59e0b; font-weight: bold; }
.cnki-status-nopdf { color: #6b7280; font-weight: bold; }
.cnki-status-exists { color: #9ca3af; font-weight: bold; }
`;
document.head.appendChild(style);
}
// --- 核心入口:智能驻守 (轮询检测) ---
function tryCreateButton() {
if (document.getElementById('cnki-main-btn')) return;
// 只要是知网搜索页就显示
const currentURL = window.location.href;
if (currentURL.includes('defaultresult') || currentURL.includes('advsearch') || currentURL.includes('search') || currentURL.includes('kns8s')) {
const btn = document.createElement('button');
btn.id = 'cnki-main-btn';
btn.className = 'cnki-main-btn';
btn.innerHTML = ` ${t('main_btn')}`;
btn.title = t('title');
btn.onclick = openDashboard;
document.body.appendChild(btn);
}
}
// 使用 MutationObserver 监听 DOM 变化
const observer = new MutationObserver(() => {
tryCreateButton();
});
function startObserver() {
const targetNode = document.body;
if(targetNode) {
observer.observe(targetNode, { childList: true, subtree: true });
} else {
setTimeout(startObserver, 500);
}
}
const url = window.location.href.toLowerCase();
function openDashboard() {
if (document.getElementById('cnki-overlay')) return;
const overlay = document.createElement('div');
overlay.id = 'cnki-overlay';
overlay.className = 'cnki-ui-overlay';
overlay.innerHTML = `
${t('mask_desc')}
| ${t('th_no')} | ${t('th_title')} | ${t('th_author')} | ${t('th_status')} |
|---|