// ==UserScript== // @name 文库下载器融合加强版 // @version 2.0.1 // @namespace https://waahah.gitee.io // @description 百度文库、淘豆网、豆丁网、道客巴巴、原创力文档解析下载功能 解除百度文库限制【保持原格式】界面简洁,脚本仅限学习。 // @author waahah // @antifeature membership 百度文库需要提交链接等待3-5分钟才能下载,淘豆网及豆丁网和道客巴巴等可直接导出PDF // @require https://cdn.bootcss.com/jquery/3.5.1/jquery.min.js // @require https://cdn.staticfile.org/jspdf/2.5.1/jspdf.umd.min.js // @require https://cdn.staticfile.org/html2canvas/1.4.1/html2canvas.min.js // @match *://wenku.baidu.com/view/* // @match *://wenku.baidu.com/tfview/* // @match *://wenku.baidu.com/link?url* // @match *://wenku.baidu.com/share/* // @match *://www.doc88.com/p-* // @match *://www.docin.com/p-* // @match *://jz.docin.com/p-* // @match *://www.taodocs.com/p-* // @match *://file.taodocs.com/p-* // @match *://max.book118.com/html/* // @license Apache-2.0 // @icon data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' width='24' height='24'%3E%3Cpath fill='%233498db' d='M21 5c-1.11-.35-2.33-.5-3.5-.5-1.95 0-4.05.4-5.5 1.5-1.45-1.1-3.55-1.5-5.5-1.5S2.45 4.9 1 6v14.65c0 .25.25.5.5.5.1 0 .15-.05.25-.05C3.1 20.45 5.05 20 6.5 20c1.95 0 4.05.4 5.5 1.5 1.35-.85 3.8-1.5 5.5-1.5 1.65 0 3.35.3 4.75 1.05.1.05.15.05.25.05.25 0 .5-.25.5-.5V6c-.6-.45-1.25-.75-2-1zm0 13.5c-1.1-.35-2.3-.5-3.5-.5-1.7 0-4.15.65-5.5 1.5V8c1.35-.85 3.8-1.5 5.5-1.5 1.2 0 2.4.15 3.5.5v11.5z'/%3E%3Cpath fill='%232980b9' d='M17.5 10.5c.88 0 1.73.09 2.5.26V9.24c-.79-.15-1.64-.24-2.5-.24-1.7 0-3.24.29-4.5.83v1.66c1.13-.64 2.7-.99 4.5-.99zM13 12.49v1.66c1.13-.64 2.7-.99 4.5-.99.88 0 1.73.09 2.5.26V11.9c-.79-.15-1.64-.24-2.5-.24-1.7 0-3.24.3-4.5.83zm4.5 1.84c-1.7 0-3.24.29-4.5.83v1.66c1.13-.64 2.7-.99 4.5-.99.88 0 1.73.09 2.5.26v-1.52c-.79-.16-1.64-.24-2.5-.24z'/%3E%3C/svg%3E // @grant unsafeWindow // @grant GM_openInTab // @grant GM.openInTab // @grant GM_getValue // @grant GM.getValue // @grant GM_setValue // @grant GM.setValue // @grant GM_xmlhttpRequest // @grant GM.xmlHttpRequest // @grant GM_registerMenuCommand // ==/UserScript== (function () { 'use strict'; // 等待页面加载完成 if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); } else { init(); } function init() { // 延迟执行以确保页面完全加载 setTimeout(mainFunction, 1000); } function mainFunction() { let webUrl = window.location.href; let webUrl2; const path = window.location.pathname.split("/")[1]; const host = location.host; const tfpath = location.href.split("/"); const title = document.title.split(' - ')[0]; const InterfaceList = [ { "host": "wkdownload", "url": "https://www.ziliaoku.xyz/?url=" }, { "host": "wenku.baidu.com", "func": "bdwk()", "el": "bdwk_ele" }, { "host": "www.doc88.com", "func": "doc()", "el": "doc_ele" }, { "host": "www.docin.com", "func": "docin()", "el": "docin_ele" }, { "host": "jz.docin.com", "func": "docin()", "el": "docin_ele" }, { "host": "www.taodocs.com", "func": "taodou()", "el": "taodou_ele" }, { "host": "file.taodocs.com", "func": "taodou()", "el": "taodou_ele" }, { "host": "max.book118.com", "func": "book118()", "el": "book118_ele" } ] function GMxmlhttpRequest(obj) { if (typeof GM_xmlhttpRequest === "function") { GM_xmlhttpRequest(obj); } else { GM.xmlhttpRequest(obj); } } function GMopenInTab(url, open_in_background) { if (typeof GM_openInTab === "function") { GM_openInTab(url, open_in_background); } else { GM.openInTab(url, open_in_background); } } const after = obj => { return obj.replace('/view/', '/share/').replace('.html', '') + '?share_api=1&width=800'; } // 新的CSS样式 - 左侧悬浮窗设计,默认展开 const css = css => { const myStyle = document.createElement('style'); myStyle.textContent = css; const doc = document.head || document.documentElement; doc.appendChild(myStyle); } css(` #zuihuitao { position: fixed; top: 120px; left: 20px; z-index: 2147483647; font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif; font-size: 14px; transition: all 0.3s ease; width: 280px; min-height: 200px; background: rgba(255, 255, 255, 0.95); backdrop-filter: blur(10px); border-radius: 16px; box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12); border: 1px solid rgba(255, 255, 255, 0.2); overflow: hidden; } #zuihuitao.compact { width: 50px; height: 50px; min-height: 50px; } .widget-header { background: linear-gradient(135deg, #3498db, #2980b9); color: white; padding: 12px 16px; display: flex; justify-content: space-between; align-items: center; cursor: move; user-select: none; } .widget-title { font-weight: 600; font-size: 14px; display: flex; align-items: center; gap: 8px; } .widget-title i { font-size: 16px; } .widget-controls { display: flex; gap: 6px; } .control-btn { width: 20px; height: 20px; border-radius: 50%; display: flex; align-items: center; justify-content: center; background: rgba(255, 255, 255, 0.2); cursor: pointer; transition: all 0.2s ease; font-size: 12px; font-weight: bold; } .control-btn:hover { background: rgba(255, 255, 255, 0.3); transform: scale(1.1); } .widget-body { padding: 16px; } #zuihuitao.compact .widget-body { display: none; } #zuihuitao.compact .widget-header { display: none; } .section { margin-bottom: 20px; } .section-title { font-size: 13px; color: #666; margin-bottom: 10px; font-weight: 500; padding-left: 4px; border-left: 3px solid #3498db; } .btn-group { display: flex; flex-direction: column; gap: 8px; } .action-btn { padding: 10px 12px; border: none; border-radius: 8px; font-size: 13px; font-weight: 500; cursor: pointer; transition: all 0.2s ease; display: flex; align-items: center; gap: 8px; background: #f8f9fa; color: #333; border: 1px solid #e9ecef; } .action-btn:hover { background: #3498db; color: white; transform: translateY(-1px); box-shadow: 0 4px 12px rgba(52, 152, 219, 0.3); } .action-btn i { font-size: 14px; } .action-btn.primary { background: #3498db; color: white; } .action-btn.primary:hover { background: #2980b9; } .status-indicator { display: flex; align-items: center; gap: 8px; font-size: 12px; color: #666; padding: 8px 12px; background: #f8f9fa; border-radius: 8px; margin-top: 12px; } .status-dot { width: 8px; height: 8px; border-radius: 50%; background: #2ecc71; animation: pulse 2s infinite; } @keyframes pulse { 0% { opacity: 1; } 50% { opacity: 0.5; } 100% { opacity: 1; } } .compact-mode { width: 50px; height: 50px; background: linear-gradient(135deg, #3498db, #2980b9); border-radius: 50%; display: flex; align-items: center; justify-content: center; color: white; cursor: pointer; box-shadow: 0 4px 16px rgba(52, 152, 219, 0.4); transition: all 0.3s ease; font-size: 20px; display: none; } #zuihuitao.compact .compact-mode { display: flex; } .compact-mode:hover { transform: scale(1.1); box-shadow: 0 6px 20px rgba(52, 152, 219, 0.6); } /* 保持原有的复制按钮样式 */ .btn-success { position: fixed; font-weight: 400; color: #fff; background-color: #28a745; border-color: #28a745; text-align: center; vertical-align: middle; border: 1px solid transparent; padding: .375rem .75rem; font-size: 1rem; line-height: 1.5; border-radius: .25rem; z-index: 2147483647; cursor: pointer; box-shadow: 0 2px 8px rgba(40, 167, 69, 0.3); } @media (max-width: 768px) { #zuihuitao { width: 260px; left: 10px; } } /* 打印样式保持不变 */ @media print { body { display: block !important; } #zuihuitao { display: none !important; } } /* 文本选择样式保持不变 */ * { -webkit-user-select: text; -moz-user-select: text; -ms-user-select: text; user-select: text; } /* 确保在百度文库中可见 */ .reader-container #zuihuitao, .reader-wrap #zuihuitao, .reader-holder #zuihuitao { z-index: 2147483647 !important; } `); const html = `
📚
📚 文库下载器
×
百度文库解析
其他文库工具
脚本已就绪
`; // 移除可能已存在的悬浮窗 if ($("#zuihuitao").length) { $("#zuihuitao").remove(); } // 添加到页面 $("body").append(html); // 悬浮窗交互逻辑 const widget = $("#zuihuitao"); let isDragging = false; let dragOffset = { x: 0, y: 0 }; // 最小化功能 $(".minimize").on("click", function(e) { e.stopPropagation(); widget.addClass("compact"); }); // 点击圆形图标展开 $(".compact-mode").on("click", function() { widget.removeClass("compact"); }); $(".close").on("click", function(e) { e.stopPropagation(); widget.hide(); }); // 拖拽功能 $(".widget-header").on("mousedown", function(e) { isDragging = true; dragOffset.x = e.clientX - widget.offset().left; dragOffset.y = e.clientY - widget.offset().top; widget.css("cursor", "grabbing"); }); $(document).on("mousemove", function(e) { if (!isDragging) return; const x = e.clientX - dragOffset.x; const y = e.clientY - dragOffset.y; // 限制在窗口范围内 const maxX = $(window).width() - widget.outerWidth(); const maxY = $(window).height() - widget.outerHeight(); widget.css({ left: Math.max(0, Math.min(x, maxX)) + "px", top: Math.max(0, Math.min(y, maxY)) + "px", right: "auto" }); }); $(document).on("mouseup", function() { isDragging = false; widget.css("cursor", ""); }); webUrl2 = after(webUrl); console.log("文库下载器已加载,当前页面:", document.title); if (path == "link") { const base_url = "https://wenku.baidu.com/user/interface/layerpop?act=get&platform=pc&layer_id=8"; const xhttp = new XMLHttpRequest(); xhttp.open("GET", base_url); xhttp.send(null); xhttp.onreadystatechange = function () { if (this.readyState == 4 && this.status == 200) { console.log('success'); let datas = xhttp.responseText; datas = JSON.parse(datas); console.log(datas); const have = datas.data[8].data.pageInfo; if (have !== undefined) { const url = have.referer; console.log(url); webUrl = url; webUrl2 = after(url); } else { console.log('用户已登录账号 url为空,使用方案二'); const pageData = document.querySelector("body > script:nth-child(5)").innerText; const DocId = pageData.indexOf('showDocId') + 12; const StoreId = pageData.indexOf('showStoreId') - 3; const showDocId = pageData.slice(DocId, StoreId); const url = tfpath[0] + "//" + tfpath[2] + '/view/' + showDocId + ".html"; console.log(url); webUrl = url; webUrl2 = after(url); } } } } if (path == "tfview") { const tfurl = tfpath[0] + "//" + tfpath[2] + "/view/" + tfpath[4]; const tfurl2 = after(tfurl); console.log(tfurl); webUrl = tfurl; webUrl2 = tfurl2; } const sleep = ms => { return new Promise(resolve => setTimeout(resolve, ms)); } const taodou_zhankai = () => { if (host == InterfaceList[5].host) { if (typeof inpmv != undefined) { let btn = document.querySelector('.moreBtn.goBtn span'); if (btn) { btn.click(); } } } } const share = () => { let FPS = 1000; let retime = setInterval(function () { let ph = document.body.scrollHeight; $(window).scrollTop(FPS, { behavior: 'smooth' }); FPS = FPS + 1000; taodou_zhankai(); if (FPS > ph) { clearInterval(retime); } }, 500) } const downloadPDF = (canvas, pdfName) => { window.scrollTo(0, 0); let num = 0; let err_num = 0; let len_cas = canvas.length; if (len_cas === 0) { alert('未找到可导出的内容,请确保页面已完全加载'); return; } var contentWidth = canvas[0].width; var contentHeight = canvas[0].height; console.log(`canvas数据:宽: ${contentWidth}px,高: ${contentHeight}px`); const orientation = contentWidth > contentHeight ? 'l' : 'p'; var pdf = new jspdf.jsPDF(orientation, 'px', [contentHeight, contentWidth]); for (const cas of canvas) { let pageData = cas; num += 1; try{ pdf.addImage(pageData, 'JPEG', 0, 0, contentWidth, contentHeight); }catch(err){ console.log(err); err_num += 1; } if (num < len_cas) { pdf.addPage(); } } pdf.save(`${pdfName}.pdf`); if(err_num){ alert(`有 ${err_num} 页未加载!`); } } const clipboardCopy = text => { if (navigator.clipboard) { navigator.clipboard.writeText(text); } else { const textarea = document.createElement('textarea'); document.body.appendChild(textarea); textarea.style.position = 'fixed'; textarea.style.clip = 'rect(0 0 0 0)'; textarea.style.top = '10px'; textarea.value = text; textarea.select(); document.execCommand('copy', true); document.body.removeChild(textarea); } } const replica = (selectText = true) => { try { if (host == InterfaceList[1].host) { let text = $('div.link')[0].outerText.split("”的文档")[0].split("查看全部包含“")[1]; clipboardCopy(text); $('.dialog-mask').remove(); $('.copy-limit-dialog-v2').remove(); } else if (host == InterfaceList[5].host) { $('.sel_tips').remove(); let selectedText = window.getSelection().toString(); if (selectedText == '') { clipboardCopy(selectText); } else { clipboardCopy(selectedText); } } } catch (error) { console.log(error.stack) text = "出错啦,请通知作者修复"; throw new Error(`Error: ${error}`); } } // 复制功能 (() => { let oldtext = ""; let count = 0; if (host == InterfaceList[1].host || host == InterfaceList[5].host) { $(document).unbind('keydown').bind('keydown', e => { if (e.ctrlKey && e.keyCode == 67) { replica(); $(".btn-success").text("复制成功").fadeOut(1000); e.preventDefault(); count += 1; } }) document.onmouseup = ev => { let nowtext; $(".btn-success").remove(); if (host == InterfaceList[1].host) { nowtext = $('div.link')[0].outerText.split("”的文档")[0].split("查看全部包含“")[1]; } else if (host == InterfaceList[5].host) { let selectedText = window.getSelection().toString(); if (selectedText) { nowtext = selectedText; } } if (nowtext != oldtext) { const oEvent = ev || event; const elbtn = $(`
复制
`); $("body").append(elbtn); oldtext = nowtext; $("#reader-helper").hide(); } $(".btn-success").on("click", e => { replica(nowtext); count += 1; console.info(`第${count}次为您复制,内容为:${nowtext}`); }).on("mouseup", e => { $(".btn-success").text("复制成功"); $(".btn-success").fadeOut(1000); e.stopPropagation(); e.preventDefault(); }) } } })(); const del = el => { $(el).remove(); } const bdwk_ele = ['.read-all', '.header-wrapper', '.no-full-screen', '.no-full-screen', '.lazy-load', '.reader-topbar', '.content-wrapper+div', '.hx-warp', '.try-end-fold-page', '#page-footer', '#reader-container+div', '#passport-login-pop' ] const doc_ele = ['#continueButton', '#header', '#toolbar', '#box1', '#boxright', '#readEndDiv', '.commonbox1', '#commentDiv', '.clearfix', '#footer', '.dk-bg', '.toplayer-shop', '.activelist' ] const docin_ele = ['.model-fold-show', '.page_crubms clear', '.doc_header_mod', '.aside', '#j_isend', '#docinShareSlider', '.backToTop', '.reader_tools_bar_wrap.tools_bar_small.clear', '.page_crubms.clear', '.adBox', '#jControlDiv' ] const taodou_ele = ['span.fc2e', '.sel_tips'] const book118_ele = ['#btn_preview_remain'] const bdwk = () => { widget.hide(); share(); setTimeout(() => { window.print(); widget.show(); }, 1000); } const doc = () => { const ele = document.querySelectorAll('canvas.inner_page'); try { downloadPDF(ele, title); } catch (error) { console.log(error.stack); } } const docin = () => { const ele = document.querySelectorAll('canvas'); try { downloadPDF(ele, title); } catch (error) { console.log(error.message); } } const taodou = () => { let ele = document.querySelectorAll('.textLayer_div canvas'); if (ele.length == 0) { ele = document.querySelectorAll('img.errorimg'); if (ele.length == 0) { ele = document.querySelectorAll('img'); } } try { downloadPDF(ele, title); } catch (error) { console.log(error.message); } } const book118 = () => { const ele = document.querySelectorAll('.webpreview-item img'); try { downloadPDF(ele, title); } catch (error) { console.log(error); } } const main = () => { for (const even in InterfaceList) { if (host == InterfaceList[even].host) { let ee = InterfaceList[even].el; $(eval(ee)[0]).click(); for (const el of eval(ee)) { del(el); } eval(InterfaceList[even].func); break; } } } // 按钮事件绑定 $("#li0").bind("click", e => { if (host == InterfaceList[1].host) { window.open(InterfaceList[0].url + webUrl); } else { e.preventDefault(); alert('百度文库解析,其它文库请使用导出PDF'); } }); $("#li2").bind("click", () => { if (confirm('请确保每一页都加载完再打印,是否打印?')) { main(); } }); $("#li3").on("click", () => { for (const even in InterfaceList) { if (host == InterfaceList[even].host) { let ee = InterfaceList[even].el; $(eval(ee)[0]).click(); break; } } widget.addClass("compact"); share(); }); // 百度文库特定处理 if (host === "wenku.baidu.com") { console.log("检测到百度文库页面,应用特定优化"); // 确保悬浮窗在阅读器上方显示 const observer = new MutationObserver(function(mutations) { mutations.forEach(function(mutation) { if (mutation.type === 'childList') { // 如果发现有阅读器相关元素被添加,重新确保悬浮窗在最上层 widget.css('z-index', '2147483647'); } }); }); // 开始观察 observer.observe(document.body, { childList: true, subtree: true }); // 添加百度文库特定样式 css(` .reader-container #zuihuitao, .reader-wrap #zuihuitao { z-index: 2147483647 !important; } `); } } })();