// ==UserScript==
// @name 文库下载器
// @version 0.3.2
// @namespace https://waahah.gitee.io
// @description 百度文库解析下载功能 文档下载器 界面简洁,脚本仅限学习,请大家支持正版。
// @author waahah
// @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/*
// @match *://www.doc88.com/p-*
// @match *://www.docin.com/p-*
// @license Apache-2.0
// @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';
$(function(){
let webUrl = window.location.href;
let webUrl2;
const path = window.location.pathname.split("/")[1];
const host = location.host;
const tfpath = location.href.split("/");
const InterfaceList = [
{"host":"wkdownload","url":"http://www.html22.com/d/?url="},
{"host":"wenku.baidu.com","func":"doc()","el":"bdwk_ele"},
{"host":"www.doc88.com","func":"doc()","el":"doc_ele"},
{"host":"www.docin.com","func":"doc()","el":"docin_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';
}
const css = css => {
const myStyle = document.createElement('style');
myStyle.textContent = css;
const doc = document.head || document.documentElement;
doc.appendChild(myStyle);
}
css(`#zuihuitao {cursor:pointer; position:fixed; top:100px; left:0px; width:0px; z-index:2147483647; font-size:12px; text-align:left;}
#zuihuitao .logo { position: absolute;right: 0; width: 1.375rem;padding: 10px 2px;text-align: center;color: #fff;cursor: auto;user-select: none;border-radius: 0 4px 4px 0;transform: translate3d(100%, 5%, 0);background: deepskyblue;}
#zuihuitao .die {display:none; position:absolute; left:28px; top:0; text-align:center;background-color:#04B4AE; border:1px solid gray;}
#zuihuitao .die li{font-size:12px; color:#fff; text-align:center; width:60px; line-height:21px; float:left; border:1px solid gray;border-radius: 6px 6px 6px 6px; padding:0 4px; margin:4px 2px;list-style-type: none;}
#zuihuitao .die li:hover{color:#fff;background:#FE2E64;}
@media print {body {display: block !important;}}
.add{background-color:#FE2E64;}`);
const html = $(`<div id='zuihuitao'>
<div class='item_text'>
<div class="logo"><a id="m">文库下载</a></div>
<div class='die' >
<div style='display:flex;'>
<div style='width:128px; padding:0px 0;'>
<br>
<div style='font-size:16px; text-align:center; color:#fff; line-height:21px;'>bdwk解析</div>
<ul style='margin:0 24px;'>
<li id="li0">下载</li>
<div style='clear:both;'></div>
</ul>
<br>
<div style='font-size:16px; text-align:center; color:#fff; line-height:21px;'>打印PDF</div>
<ul style='margin:0 25px;'>
<li id="li2">打印</li>
<div style='clear:both;'></div>
</ul>
<br>
</div>`);
webUrl2 = after(webUrl);
$("body").append(html);
$(".item_text").on("mouseover", () => {
$(".die").show();
});
$(".item_text").on("mouseout", () => {
$(".die").hide();
});
console.log(document.lastModified);
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;
//console.log(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;
//console.log(pageData);
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 share = () => {
//console.log(webUrl);
let ph = document.body.scrollHeight;
let FPS = 1000;
let retime = setInterval(function () {
$(window).scrollTop( FPS,{ behavior: 'smooth'});
//ph = document.body.scrollHeight;
FPS = FPS + 1000;
if (FPS > ph) {
clearInterval(retime);
setTimeout(function () {
window.print();
$(".logo").show();
},500);
}
}, 300)
}
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'
]
const doc_ele = ['#continueButton','#header','#toolbar','#box1','#boxright','#readEndDiv','.commonbox1',
'#commentDiv','.clearfix','#footer','.dk-bg','.toplayer-shop','.activelist'
]
const docin_ele = ['.head_wrapper','.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 doc = () => {
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);
}
}
}
}
const main = () => {
for(const even in InterfaceList){
if(host ==InterfaceList[even].host){
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('请确保每一页都加载完再打印,是否打印?')){
$(".logo").hide();
$(".die").hide();
main();
share();
}
});
});
})();