// ==UserScript== // @name 文库下载器百度文库专版 // @version 2.0.1 // @namespace https://waahah.gitee.io // @description 百度文库解析下载功能 解除百度文库限制【保持原格式】界面简洁,脚本仅限学习。 // @author waahah // @antifeature membership 百度文库需要提交链接等待3-5分钟才能下载 // @require https://cdn.bootcss.com/jquery/3.5.1/jquery.min.js // @match *://wenku.baidu.com/view/* // @match *://wenku.baidu.com/tfview/* // @match *://wenku.baidu.com/link?url* // @match *://wenku.baidu.com/share/* // @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 // ==/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 after = obj => { return obj.replace('/view/', '/share/').replace('.html', '') + '?share_api=1&width=800'; } 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-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.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; } .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 share = () => { let FPS = 1000; let retime = setInterval(function () { let ph = document.body.scrollHeight; $(window).scrollTop(FPS, { behavior: 'smooth' }); FPS = FPS + 1000; if (FPS > ph) { clearInterval(retime); } }, 500) } 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 { let text = $('div.link')[0].outerText.split("”的文档")[0].split("查看全部包含“")[1]; clipboardCopy(text); $('.dialog-mask').remove(); $('.copy-limit-dialog-v2').remove(); } catch (error) { console.log(error.stack) text = "出错啦,请通知作者修复"; throw new Error(`Error: ${error}`); } } // 复制功能 (() => { let oldtext = ""; let count = 0; $(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(); nowtext = $('div.link')[0].outerText.split("”的文档")[0].split("查看全部包含“")[1]; 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 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 bdwk = () => { widget.hide(); share(); setTimeout(() => { window.print(); widget.show(); }, 1000); } const main = () => { for (const el of bdwk_ele) { $(el).remove(); } bdwk(); } // 按钮事件绑定 $("#li0").bind("click", e => { window.open("https://www.ziliaoku.xyz/?url=" + webUrl); }); $("#li2").bind("click", () => { if (confirm('请确保每一页都加载完再打印,是否打印?')) { main(); } }); $("#li3").on("click", () => { for (const el of bdwk_ele) { $(el).remove(); } 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; } `); } } })();