// ==UserScript== // @name PKU-Thesis-Download 北大论文平台下载工具 // @namespace https://greasyfork.org/zh-CN/scripts/442310-pku-thesis-download // @supportURL https://github.com/xiaotianxt/PKU-Thesis-Download // @homepageURL https://github.com/xiaotianxt/PKU-Thesis-Download // @version 1.2.5 // @description 北大论文平台下载工具,请勿传播下载的文件,否则后果自负。 // @author xiaotianxt // @match http://162.105.134.201/pdfindex* // @match https://drm.lib.pku.edu.cn/pdfindex* // @match https://drm-lib-pku-edu-cn-443.webvpn.bjmu.edu.cn/pdfindex* // @icon https://www.google.com/s2/favicons?sz=64&domain=pku.edu.cn // @require https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js // @require https://cdnjs.cloudflare.com/ajax/libs/notify/0.4.2/notify.min.js // @license GNU GPLv3 // @grant GM_addStyle // @history 1.2.2 修复了横屏图片的加载样式和pdf渲染样式 // @history 1.2.3 支持北医 Web VPN 系统 // @history 1.2.4 适配限流问题 // @history 1.2.5 适当降低请求频率,避免触发过多限流 // @downloadURL none // ==/UserScript== (function () { "use strict"; class ParallelRateLimiter { constructor(maxParallel) { this.queue = []; this.running = 0; this.maxParallel = maxParallel; } add(fn) { return new Promise((resolve, reject) => { const wrappedFn = async () => { this.running++; try { const result = await fn(); resolve(result); } catch (error) { reject(error); } finally { this.running--; this.runNext(); } }; if (this.running < this.maxParallel) { wrappedFn(); } else { this.queue.push(wrappedFn); } }); } runNext() { if (this.queue.length > 0 && this.running < this.maxParallel) { const nextFn = this.queue.shift(); if (nextFn) { nextFn(); } } } } async function retry(fn, retries = 3, initialDelay = 1000) { let attempt = 0; while (true) { try { return await fn(); } catch (error) { attempt++; if (attempt >= retries) { console.error(`Failed after ${retries} attempts`); throw error; } // 计算指数退避延迟时间: initialDelay * 2^attempt + 随机抖动 const delay = initialDelay * Math.pow(2, attempt) + Math.random() * 1000; console.warn(`Attempt ${attempt} failed. Retrying in ${Math.round(delay)}ms...`); await new Promise(resolve => setTimeout(resolve, delay)); } } } const limiter = new ParallelRateLimiter(3); const print = (...args) => console.log("[PKU-Thesis-Download]", ...args); const OPTIMIZATION = "pku_thesis_download.optimization"; const fid = $("#fid").val(); const totalPage = parseInt($("#totalPages").html().replace(/ \/ /, "")); const baseUrl = `/jumpServlet?fid=${fid}`; const msgBox = initUI(); if (localStorage.getItem(OPTIMIZATION) === "true" || !localStorage.getItem(OPTIMIZATION)) { optimizeImgLoading(); } function initUI() { // 下载按钮 const downloadButton = document.querySelector("#thumbtab").cloneNode(true); downloadButton.innerHTML = `
下载 `; document.querySelector("#btnList").appendChild(downloadButton); downloadButton.addEventListener("click", download); // 论文加载优化 const optimizeImg = document.querySelector("#thumbtab").cloneNode(true); optimizeImg.innerHTML = ` `; optimizeImg.querySelector("input").checked = localStorage.getItem(OPTIMIZATION) === "true" || localStorage.getItem(OPTIMIZATION) === null; optimizeImg.addEventListener("click", (e) => { const checked = e.target.checked; localStorage.setItem(OPTIMIZATION, checked); if (checked) { optimizeImg(); } }); document.querySelector("#btnList").appendChild(optimizeImg); GM_addStyle(` .loadingBg { display: flex; justify-content: center; align-items: center; } `); const observer = new MutationObserver((mutationsList) => { for (let mutation of mutationsList) { if (mutation.type === 'childList') { mutation.addedNodes.forEach((node) => { if (node.nodeName === 'IMG' && node.parentElement.classList.contains('loadingBg')) { node.addEventListener('load', function () { const img = node; if (img.naturalWidth > img.naturalHeight) { // 横向图片 img.style.height = 'min(100%, 90vw / 1.414)'; img.style.width = 'auto'; } }); } }); } } }); observer.observe(document.querySelector(".jspPane-scroll"), { childList: true, subtree: true }); // msgBox const msgBox = downloadButton.querySelector("span"); return msgBox; } async function download(e) { e.preventDefault(); e.target.disabled = true; await solveSrc().then(solveImg).then(solvePDF); e.target.disabled = false; } /** * 解析pdf图片链接 */ async function solveSrc() { async function downloadSrcInfo(url) { const res = await fetch(url); if (!res.ok) { throw new Error(`HTTP error! status: ${res.status}`); } const json = await res.json(); finished += 2; msgBox.innerHTML = `${finished}/${totalPage}`; return json.list; } let finished = 0; const tasks = []; for (let page = 0; page < totalPage; page += 2) { const url = `${baseUrl}&page=${page}`; tasks.push(() => retry(async () => downloadSrcInfo(url))); } const results = await Promise.all(tasks.map(task => limiter.add(task))); return results.flat(); // 假设我们想要一个扁平化的结果数组 } /** * 下载图片 */ async function solveImg(urls) { let numFinished = 0; async function downloadPdf(url) { return fetch(url) .then((res) => res.blob()) .then((blob) => { const reader = new FileReader(); reader.readAsDataURL(blob); return new Promise((resolve, reject) => { reader.onloadend = () => { const base64 = reader.result; const img = new Image(); img.src = base64; img.onload = () => { const orientation = img.width > img.height ? "landscape" : "portrait"; resolve({ base64, orientation }); numFinished++; msgBox.innerHTML = numFinished + "/" + numTotal; }; img.onerror = () => { console.error("Failed to load image", url); reject(null); } }; }); }); } // remove duplicated const map = new Map(urls.flat().map((item) => [item.id, item.src])); // assert that all pages are loaded if (map.size !== totalPage) { const missing = Array.from({ length: totalPage }, (_, i) => i + 1).filter((i) => !map.has(`${i}`)); alert(`部分页面没有加载出来,请联系开发者。\n缺少:${missing.join(",")}`); } // sort and clear index const sortedUrls = [...map.entries()] .sort((a, b) => a[0] - b[0]) .map((item) => item[1]); // download images const tasks = []; const numTotal = sortedUrls.length; sortedUrls.forEach((url) => { tasks.push(async () => await retry(async () => await downloadPdf(url))); }); return Promise.all(tasks.map(task => limiter.add(task))); } /** * 拼接为pdf * @param {*} base64s */ async function solvePDF(base64s) { msgBox.innerHTML = "拼接中"; const doc = new jspdf.jsPDF({ format: 'a4', orientation: 'portrait' }); for (let i = 0; i < base64s.length; i++) { const { base64, orientation } = base64s[i]; if (orientation === "landscape") { doc.addImage(base64, "JPEG", 0, 0, 297, 210); } else { doc.addImage(base64, "JPEG", 0, 0, 210, 297); } i + 1 == base64s.length || doc.addPage("a4", base64s[i + 1].orientation); } msgBox.innerHTML = "保存中"; doc.save(document.title + ".pdf"); msgBox.innerHTML = "完成!"; } /** * 优化加载 */ async function optimizeImgLoading() { function loadImgForPage(element, observer) { const index = Array.from(document.getElementsByClassName('fwr_page_box')).indexOf(element) + 1; observer.unobserve(element); if (index % 3 !== 1) return; print(`load page ${index + 3} - ${index + 5}.`); omg(index + 3); // 提前加载 3 页 } // 创建 IntersectionObserver 实例 const observer = new IntersectionObserver((entries, observer) => { entries.forEach(entry => { if (entry.isIntersecting) { loadImgForPage(entry.target, observer); } }); }, { root: document.querySelector('#jspPane'), // 使用 jspPane 作为滚动容器 rootMargin: '0px', threshold: 0 // 当 10% 的内容进入视口时触发 }); // 为每个 fwr_page_box 元素设置观察器 const pages = document.querySelectorAll('.fwr_page_box:nth-child(3n+1)'); pages.forEach(page => observer.observe(page)); } })();