怠惰小说下载器

通用网站内容抓取工具,可批量抓取任意站点的小说、论坛内容等并保存为TXT文档

目前为 2022-08-26 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name DownloadAllContent
  3. // @name:zh-CN 怠惰小说下载器
  4. // @name:zh-TW 怠惰小説下載器
  5. // @name:ja 怠惰者小説ダウンロードツール
  6. // @namespace hoothin
  7. // @version 2.7.3.10
  8. // @description Fetch and download main content on current page, provide special support for chinese novel
  9. // @description:zh-CN 通用网站内容抓取工具,可批量抓取任意站点的小说、论坛内容等并保存为TXT文档
  10. // @description:zh-TW 通用網站內容抓取工具,可批量抓取任意站點的小說、論壇內容等並保存為TXT文檔
  11. // @description:ja ユニバーサルサイトコンテンツクロールツール、クロール、フォーラム内容など
  12. // @author hoothin
  13. // @match http://*/*
  14. // @match https://*/*
  15. // @match ftp://*/*
  16. // @grant GM_xmlhttpRequest
  17. // @grant GM_registerMenuCommand
  18. // @grant GM_setValue
  19. // @grant GM_getValue
  20. // @grant unsafeWindow
  21. // @license MIT License
  22. // @compatible chrome
  23. // @compatible firefox
  24. // @compatible opera 未测试
  25. // @compatible safari 未测试
  26. // @contributionURL https://buymeacoffee.com/hoothin
  27. // @contributionAmount 1
  28. // ==/UserScript==
  29. (function (global, factory) {
  30. if (typeof define === "function" && define.amd) {
  31. define([], factory);
  32. } else if (typeof exports !== "undefined") {
  33. factory();
  34. } else {
  35. var mod = {
  36. exports: {}
  37. };
  38. factory();
  39. global.FileSaver = mod.exports;
  40. }
  41. })(this, function () {
  42. "use strict";
  43.  
  44. /*
  45. * FileSaver.js
  46. * A saveAs() FileSaver implementation.
  47. *
  48. * By Eli Grey, http://eligrey.com
  49. *
  50. * License : https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md (MIT)
  51. * source : http://purl.eligrey.com/github/FileSaver.js
  52. */
  53. var _global = typeof window === 'object' && window.window === window ? window : typeof self === 'object' && self.self === self ? self : typeof global === 'object' && global.global === global ? global : void 0;
  54.  
  55. function bom(blob, opts) {
  56. if (typeof opts === 'undefined') opts = {
  57. autoBom: false
  58. };else if (typeof opts !== 'object') {
  59. console.warn('Deprecated: Expected third argument to be a object');
  60. opts = {
  61. autoBom: !opts
  62. };
  63. }
  64.  
  65. if (opts.autoBom && /^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
  66. return new Blob([String.fromCharCode(0xFEFF), blob], {
  67. type: blob.type
  68. });
  69. }
  70.  
  71. return blob;
  72. }
  73.  
  74. function download(url, name, opts) {
  75. var xhr = new XMLHttpRequest();
  76. xhr.open('GET', url);
  77. xhr.responseType = 'blob';
  78.  
  79. xhr.onload = function () {
  80. saveAs(xhr.response, name, opts);
  81. };
  82.  
  83. xhr.onerror = function () {
  84. console.error('could not download file');
  85. };
  86.  
  87. xhr.send();
  88. }
  89.  
  90. function corsEnabled(url) {
  91. var xhr = new XMLHttpRequest();
  92.  
  93. xhr.open('HEAD', url, false);
  94.  
  95. try {
  96. xhr.send();
  97. } catch (e) {}
  98.  
  99. return xhr.status >= 200 && xhr.status <= 299;
  100. }
  101.  
  102.  
  103. function click(node) {
  104. try {
  105. node.dispatchEvent(new MouseEvent('click'));
  106. } catch (e) {
  107. var evt = document.createEvent('MouseEvents');
  108. evt.initMouseEvent('click', true, true, window, 0, 0, 0, 80, 20, false, false, false, false, 0, null);
  109. node.dispatchEvent(evt);
  110. }
  111. }
  112.  
  113.  
  114. var isMacOSWebView = _global.navigator && /Macintosh/.test(navigator.userAgent) && /AppleWebKit/.test(navigator.userAgent) && !/Safari/.test(navigator.userAgent);
  115. var saveAs = _global.saveAs || (
  116. typeof window !== 'object' || window !== _global ? function saveAs() {}
  117.  
  118. : 'download' in HTMLAnchorElement.prototype && !isMacOSWebView ? function saveAs(blob, name, opts) {
  119. var URL = _global.URL || _global.webkitURL;
  120. var a = document.createElement('a');
  121. name = name || blob.name || 'download';
  122. a.download = name;
  123. a.rel = 'noopener';
  124.  
  125. if (typeof blob === 'string') {
  126. a.href = blob;
  127.  
  128. if (a.origin !== location.origin) {
  129. corsEnabled(a.href) ? download(blob, name, opts) : click(a, a.target = '_blank');
  130. } else {
  131. click(a);
  132. }
  133. } else {
  134. a.href = URL.createObjectURL(blob);
  135. setTimeout(function () {
  136. URL.revokeObjectURL(a.href);
  137. }, 4E4);
  138.  
  139. setTimeout(function () {
  140. click(a);
  141. }, 0);
  142. }
  143. }
  144. : 'msSaveOrOpenBlob' in navigator ? function saveAs(blob, name, opts) {
  145. name = name || blob.name || 'download';
  146.  
  147. if (typeof blob === 'string') {
  148. if (corsEnabled(blob)) {
  149. download(blob, name, opts);
  150. } else {
  151. var a = document.createElement('a');
  152. a.href = blob;
  153. a.target = '_blank';
  154. setTimeout(function () {
  155. click(a);
  156. });
  157. }
  158. } else {
  159. navigator.msSaveOrOpenBlob(bom(blob, opts), name);
  160. }
  161. }
  162. : function saveAs(blob, name, opts, popup) {
  163. popup = popup || open('', '_blank');
  164.  
  165. if (popup) {
  166. popup.document.title = popup.document.body.innerText = 'downloading...';
  167. }
  168.  
  169. if (typeof blob === 'string') return download(blob, name, opts);
  170. var force = blob.type === 'application/octet-stream';
  171.  
  172. var isSafari = /constructor/i.test(_global.HTMLElement) || _global.safari;
  173.  
  174. var isChromeIOS = /CriOS\/[\d]+/.test(navigator.userAgent);
  175.  
  176. if ((isChromeIOS || force && isSafari || isMacOSWebView) && typeof FileReader !== 'undefined') {
  177. var reader = new FileReader();
  178.  
  179. reader.onloadend = function () {
  180. var url = reader.result;
  181. url = isChromeIOS ? url : url.replace(/^data:[^;]*;/, 'data:attachment/file;');
  182. if (popup) popup.location.href = url;else location = url;
  183. popup = null;
  184. };
  185.  
  186. reader.readAsDataURL(blob);
  187. } else {
  188. var URL = _global.URL || _global.webkitURL;
  189. var url = URL.createObjectURL(blob);
  190. if (popup) popup.location = url;else location.href = url;
  191. popup = null;
  192.  
  193. setTimeout(function () {
  194. URL.revokeObjectURL(url);
  195. }, 4E4);
  196. }
  197. });
  198. _global.saveAs = saveAs.saveAs = saveAs;
  199.  
  200. if (typeof module !== 'undefined') {
  201. module.exports = saveAs;
  202. }
  203. });
  204.  
  205. (function() {
  206. 'use strict';
  207. var indexReg=/PART\b|^Prologue|Chapter\s*[\-_]?\d+|分卷|^序$|^序\s*言|^序\s*章|^前\s*言|^附\s*[录錄]|^引\s*[言子]|^摘\s*要|^[楔契]\s*子|^后\s*记|^後\s*記|^附\s*言|^结\s*语|^結\s*語|^尾\s*[声聲]|^最終話|^最终话|^番\s*外|^\d+\s*\D*[^\d#\.]$|^[第(]?[\d〇零一二三四五六七八九十百千万萬-]+\s*[、)章节節回卷折篇幕集话話]/i;
  208. var innerNextPage=/下一[页頁张張]|next\s*page|次のページ/i;
  209. var lang = navigator.appName=="Netscape"?navigator.language:navigator.userLanguage;
  210. var i18n={};
  211. var rCats=[];
  212. var processFunc;
  213. switch (lang){
  214. case "zh-CN":
  215. case "zh-SG":
  216. i18n={
  217. fetch:"开始下载小说【Ctrl+F9】",
  218. info:"本文是使用怠惰小说下载器(DownloadAllContent)下载的",
  219. error:"该段内容获取失败",
  220. downloading:"已下载完成 %s 段,剩余 %s 段<br>正在下载 %s",
  221. complete:"已全部下载完成,共 %s 段",
  222. del:"设置文本干扰码的CSS选择器",
  223. custom:"自定义下载",
  224. customInfo:"输入网址或者章节CSS选择器",
  225. reSort:"按标题名重新排序",
  226. reSortUrl:"按网址重新排序",
  227. setting:"选项设置",
  228. abort:"跳过此章",
  229. save:"临时保存",
  230. downThreadNum:"设置同时下载的线程数"
  231. };
  232. break;
  233. case "zh-TW":
  234. case "zh-HK":
  235. i18n={
  236. fetch:"開始下載小說【Ctrl+F9】",
  237. info:"本文是使用怠惰小說下載器(DownloadAllContent)下載的",
  238. error:"該段內容獲取失敗",
  239. downloading:"已下載完成 %s 段,剩餘 %s 段<br>正在下載 %s",
  240. complete:"已全部下載完成,共 %s 段",
  241. del:"設置文本干擾碼的CSS選擇器",
  242. custom:"自定義下載",
  243. customInfo:"輸入網址或者章節CSS選擇器",
  244. reSort:"按標題名重新排序",
  245. reSortUrl:"按網址重新排序",
  246. setting:"選項設置",
  247. abort:"跳過此章",
  248. save:"保存當前",
  249. downThreadNum:"設置同時下載的綫程數"
  250. };
  251. break;
  252. default:
  253. i18n={
  254. fetch:"Download [Ctrl+F9]",
  255. info:"The TXT is downloaded by 'DownloadAllContent'",
  256. error:"Failed in downloading current chapter",
  257. downloading:"%s pages are downloaded, there are still %s pages left<br>Downloading %s ......",
  258. complete:"Completed! Get %s pages in total",
  259. del:"Set css selectors for ignore",
  260. custom:"Custom to download",
  261. customInfo:"Input urls OR sss selectors for chapter links",
  262. reSort:"ReSort by title",
  263. reSortUrl:"Resort by URLs",
  264. setting:"Open Setting",
  265. abort:"Abort",
  266. save:"Save",
  267. downThreadNum:"Set threadNum for download"
  268. };
  269. break;
  270. }
  271. var firefox=navigator.userAgent.toLowerCase().indexOf('firefox')!=-1,curRequests=[];
  272. var rocketContent,txtDownContent,txtDownWords,txtDownQuit,txtDownDivInited=false;
  273.  
  274. function initTxtDownDiv(){
  275. if(txtDownDivInited)return;
  276. txtDownDivInited=true;
  277. rocketContent=document.createElement("div");
  278. document.body.appendChild(rocketContent);
  279. rocketContent.outerHTML=`
  280. <div id="txtDownContent">
  281. <div style="font-size:16px;color:#333333;width:360px;height:110px;position:fixed;left:50%;top:50%;margin-top:-25px;margin-left:-150px;z-index:100000;background-color:#ffffff;border:1px solid #afb3b6;border-radius:10px;opacity:0.95;filter:alpha(opacity=95);box-shadow:5px 5px 20px 0px #000;">
  282. <div id="txtDownWords" style="position:absolute;width:275px;height: 90px;max-height: 90%;border: 1px solid #f3f1f1;padding: 8px;border-radius: 10px;overflow: auto;">
  283. Downloading......
  284. </div>
  285. <div id="txtDownQuit" style="width: 30px;height: 30px;border-radius: 30px;position:absolute;right:2px;top:2px;cursor: pointer;background-color:#ff5a5a;">
  286. <span style="height: 30px;line-height: 30px;display:block;color:#FFF;text-align:center;font-size: 12px;font-weight: bold;">╳</span>
  287. </div>
  288. <div style="position:absolute;right:0px;bottom:2px;cursor: pointer;max-width:85px">
  289. <button id="abortRequest" style="background: #008aff;border: 0;padding: 5px;border-radius: 6px;color: white;float: right;margin: 1px;height: 25px;display:none;line-height: 16px;">${getI18n('abort')}</button>
  290. <button id="tempSaveTxt" style="background: #008aff;border: 0;padding: 5px;border-radius: 6px;color: white;float: right;margin: 1px;height: 25px;line-height: 16px;">${getI18n('save')}</button>
  291. </div>
  292. </div>
  293. </div>`;
  294. txtDownContent=document.querySelector("#txtDownContent");
  295. txtDownWords=document.querySelector("#txtDownWords");
  296. txtDownQuit=document.querySelector("#txtDownQuit");
  297. txtDownQuit.onclick=function(){
  298. txtDownContent.style.display="none";
  299. txtDownContent.parentNode.removeChild(txtDownContent);
  300. };
  301. initTempSave();
  302. }
  303.  
  304. function initTempSave(){
  305. var tempSavebtn = document.getElementById('tempSaveTxt');
  306. var abortbtn = document.getElementById('abortRequest');
  307. tempSavebtn.onclick = function(){
  308. var blob = new Blob([i18n.info+"\r\n\r\n"+document.title+"\r\n\r\n"+rCats.join("\r\n\r\n")], {type: "text/plain;charset=utf-8"});
  309. saveAs(blob, document.title+".txt");
  310. console.log(curRequests);
  311. }
  312. abortbtn.onclick = function(){
  313. let curRequest = curRequests.pop();
  314. if(curRequest)curRequest[1].abort();
  315. }
  316. }
  317.  
  318. function indexDownload(aEles){
  319. if(aEles.length<1)return;
  320. initTxtDownDiv();
  321. if(GM_getValue("contentSort")){
  322. aEles.sort(function(a,b){
  323. return parseInt(a.innerText.replace(/[^0-9]/ig,"")) - parseInt(b.innerText.replace(/[^0-9]/ig,""));
  324. });
  325. }
  326. if(GM_getValue("contentSortUrl")){
  327. aEles.sort(function(a,b){
  328. return parseInt(a.href.replace(/[^0-9]/ig,"")) - parseInt(b.href.replace(/[^0-9]/ig,""));
  329. });
  330. }
  331. rCats=[];
  332. var insertSigns=[];
  333. // var j=0,rCats=[];
  334. var downIndex=0,downNum=0,downOnce=function(){
  335. if(downNum>=aEles.length)return;
  336. let curIndex=downIndex;
  337. let aTag=aEles[curIndex];
  338. let request=(aTag, curIndex)=>{
  339. let tryTimes=0;
  340. let requestBody={
  341. method: 'GET',
  342. url: aTag.href,
  343. headers:{
  344. referer:aTag.href,
  345. "Content-Type":"text/html;charset="+document.charset,
  346. },
  347. timeout:15000,
  348. overrideMimeType:"text/html;charset="+document.charset,
  349. onload: function(result) {
  350. downIndex++;
  351. downNum++;
  352. var doc = getDocEle(result.responseText);
  353. let nextPage=checkNextPage(doc);
  354. if(nextPage){
  355. var inArr=false;
  356. for(var ai=0;ai<aEles.length;ai++){
  357. if(aEles[ai].href==nextPage.href){
  358. inArr=true;
  359. break;
  360. }
  361. }
  362. if(!inArr){
  363. nextPage.innerText=aTag.innerText+"\t>>";
  364. aEles.push(nextPage);
  365. let targetIndex = curIndex;
  366. for(let a=0;a<insertSigns.length;a++){
  367. let signs=insertSigns[a],breakSign=false;
  368. if(signs){
  369. for(let b=0;b<signs.length;b++){
  370. let sign=signs[b];
  371. if(sign==curIndex){
  372. targetIndex=a;
  373. breakSign=true;
  374. break;
  375. }
  376. }
  377. }
  378. if(breakSign)break;
  379. }
  380. let insertSign = insertSigns[targetIndex];
  381. if(!insertSign)insertSigns[targetIndex] = [];
  382. insertSigns[targetIndex].push(aEles.length-1);
  383. }
  384. }
  385. processDoc(curIndex, aTag, doc);
  386. let request=downOnce();
  387. if(request)curRequests.push(request);
  388. },
  389. onerror: function(e) {
  390. console.warn("error:");
  391. console.log(e);
  392. downIndex++;
  393. downNum++;
  394. processDoc(curIndex, aTag, null, ' : NETWORK ERROR '+(e.response||e.responseText));
  395. let request=downOnce();
  396. if(request)curRequests.push(request);
  397. },
  398. ontimeout: function(e) {
  399. console.warn("timeout: times="+tryTimes+" url="+aTag.href);
  400. //console.log(e);
  401. if(++tryTimes<3){
  402. return GM_xmlhttpRequest(requestBody);
  403. }
  404. downIndex++;
  405. downNum++;
  406. processDoc(curIndex, aTag, null, ' : TIMEOUT '+aTag.href);
  407. let request=downOnce();
  408. if(request)curRequests.push(request);
  409. }
  410. };
  411. return [curIndex,GM_xmlhttpRequest(requestBody),aTag.href];
  412. }
  413. if(!aTag){
  414. let waitAtagReadyInterval=setInterval(function(){
  415. if(downNum>=aEles.length)clearInterval(waitAtagReadyInterval);
  416. aTag=aEles[curIndex];
  417. if(aTag){
  418. clearInterval(waitAtagReadyInterval);
  419. request(aTag, curIndex);
  420. }
  421. },1000);
  422. return null;
  423. }
  424. return request(aTag, curIndex);
  425. };
  426. function getDocEle(str){
  427. var doc = null;
  428. try {
  429. doc = document.implementation.createHTMLDocument('');
  430. doc.documentElement.innerHTML = str;
  431. }
  432. catch (e) {
  433. console.log('parse error');
  434. }
  435. return doc;
  436. }
  437. function sortInnerPage(){
  438. var pageArrs=[],maxIndex=0,i,j;
  439. for(i=0;i<insertSigns.length;i++){
  440. var signs=insertSigns[i];
  441. if(signs){
  442. for(j=0;j<signs.length;j++){
  443. var sign=signs[j];
  444. var cat=rCats[sign];
  445. rCats[sign]=null;
  446. if(!pageArrs[i])pageArrs[i]=[];
  447. pageArrs[i].push(cat);
  448. }
  449. }
  450. }
  451. for(i=pageArrs.length-1;i>=0;i--){
  452. let pageArr=pageArrs[i];
  453. if(pageArr){
  454. for(j=pageArr.length-1;j>=0;j--){
  455. rCats.splice(i+1, 0, pageArr[j]);
  456. }
  457. }
  458. }
  459. rCats = rCats.filter(function(e){return e!=null});
  460. }
  461. var waitForComplete;
  462. function processDoc(i, aTag, doc, cause){
  463. let cbFunc=content=>{
  464. rCats[i]=(aTag.innerText.trim() + "\r\n" + content + (cause || ''));
  465. curRequests = curRequests.filter(function(e){return e[0]!=i});
  466. txtDownContent.style.display="block";
  467. txtDownWords.innerHTML=getI18n("downloading",[downNum,(aEles.length-downNum),aTag.innerText]);
  468. if(downNum==aEles.length){
  469. if(waitForComplete) clearTimeout(waitForComplete);
  470. waitForComplete=setTimeout(()=>{
  471. if(downNum==aEles.length){
  472. txtDownWords.innerHTML=getI18n("complete",[downNum]);
  473. sortInnerPage();
  474. var blob = new Blob([i18n.info+"\r\n\r\n"+document.title+"\r\n\r\n"+rCats.join("\r\n\r\n")], {type: "text/plain;charset=utf-8"});
  475. saveAs(blob, document.title+".txt");
  476. }
  477. },3000);
  478. }
  479. };
  480. let contentResult=getPageContent(doc, content=>{
  481. cbFunc(content);
  482. });
  483. if(contentResult!==false){
  484. cbFunc(contentResult);
  485. }
  486. }
  487. var downThreadNum = parseInt(GM_getValue("downThreadNum"));
  488. downThreadNum=downThreadNum>0?downThreadNum:20;
  489. for(var i=0;i<downThreadNum;i++){
  490. let request=downOnce();
  491. if(request)curRequests.push(request);
  492. if(downIndex>=aEles.length-1 || downIndex>=downThreadNum-1)break;
  493. else downIndex++;
  494. }
  495.  
  496. /*for(let i=0;i<aEles.length;i++){
  497. let aTag=aEles[i];
  498. GM_xmlhttpRequest({
  499. method: 'GET',
  500. url: aTag.href,
  501. overrideMimeType:"text/html;charset="+document.charset,
  502. onload: function(result) {
  503. var doc = getDocEle(result.responseText);
  504. processDoc(i, aTag, doc);
  505. }
  506. });
  507. }*/
  508. }
  509.  
  510. function checkNextPage(doc){
  511. let aTags=doc.querySelectorAll("a"),nextPage=null;
  512. for(var i=0;i<aTags.length;i++){
  513. let aTag=aTags[i];
  514. if(innerNextPage.test(aTag.innerText) && aTag.href.indexOf("javascript")==-1){
  515. nextPage=aTag;
  516. break;
  517. }
  518. }
  519. return nextPage;
  520. }
  521.  
  522. function getPageContent(doc, cb){
  523. if(!doc)return i18n.error;
  524. if(processFunc){
  525. return processFunc(doc, cb);
  526. }
  527. if(doc.defaultView){
  528. [].forEach.call(doc.querySelectorAll("span,div,ul"),function(item){
  529. var thisStyle=doc.defaultView.getComputedStyle(item);
  530. if(thisStyle && (thisStyle.display=="none" || (item.tagName=="SPAN" && thisStyle.fontSize=="0px")))
  531. item.innerHTML="";
  532. });
  533. }
  534. var i,j,k,rStr="",pageData=(doc.body?doc.body:doc).cloneNode(true),delList=[];
  535. pageData.innerHTML=pageData.innerHTML.replace(/\<\!\-\-((.|[\n|\r|\r\n])*?)\-\-\>/g,"");
  536. [].forEach.call(pageData.querySelectorAll("font.jammer"),function(item){
  537. item.innerHTML="";
  538. });
  539. var selectors=GM_getValue("selectors");
  540. if(selectors){
  541. [].forEach.call(pageData.querySelectorAll(selectors),function(item){
  542. item.innerHTML="";
  543. });
  544. }
  545. [].forEach.call(pageData.querySelectorAll("script,style,link,img,noscript,iframe"),function(item){delList.push(item);});
  546. [].forEach.call(delList,function(item){item.innerHTML="";});
  547. var largestContent,contents=pageData.querySelectorAll("span,div,article,p,td"),largestNum=0;
  548. for(i=0;i<contents.length;i++){
  549. let content=contents[i],hasText=false,allSingle=true,item,curNum=0;
  550. for(j=content.childNodes.length-1;j>=0;j--){
  551. item=content.childNodes[j];
  552. if(item.nodeType==3){
  553. if(/^\s*$/.test(item.data))
  554. item.innerHTML="";
  555. else hasText=true;
  556. }else if(/^(I|A|STRONG|B|FONT|P|DL|DD|H\d)$/.test(item.tagName))hasText=true;
  557. }
  558. for(j=content.childNodes.length-1;j>=0;j--){
  559. item=content.childNodes[j];
  560. if(item.nodeType==1 && !/^(I|A|STRONG|B|FONT|BR)$/.test(item.tagName) && /^[\s\-\_\?\>\|]*$/.test(item.innerHTML))
  561. item.innerHTML="";
  562. }
  563. if(content.childNodes.length>1){
  564. for(j=0;j<content.childNodes.length;j++){
  565. item=content.childNodes[j];
  566. if(item.nodeType==1){
  567. for(k=0;k<item.childNodes.length;k++){
  568. var childNode=item.childNodes[k];
  569. if(childNode.nodeType!=3 && !/^(I|A|STRONG|B|FONT|BR)$/.test(childNode.tagName)){
  570. allSingle=false;
  571. break;
  572. }
  573. }
  574. if(!allSingle)break;
  575. }
  576. }
  577. }else{
  578. allSingle=false;
  579. }
  580. if(allSingle){
  581. curNum=(firefox?content.textContent.length:content.innerText.length);
  582. }else {
  583. if(!hasText)continue;
  584. if(pageData==document && content.offsetWidth<=0 && content.offsetHeight<=0)
  585. continue;
  586. [].forEach.call(content.childNodes,function(item){
  587. if(item.nodeType==3)curNum+=item.data.length;
  588. else if(/^(I|A|STRONG|B|FONT|P|DL|DD|H\d)$/.test(item.tagName))curNum+=(firefox?item.textContent.length:item.innerText.length);
  589. });
  590. }
  591. if(curNum>largestNum){
  592. largestNum=curNum;
  593. largestContent=content;
  594. }
  595. }
  596. if(!largestContent)return i18n.error+" : NO TEXT CONTENT";
  597. var childlist=pageData.querySelectorAll(largestContent.tagName);//+(largestContent.className?"."+largestContent.className.replace(/(^\s*)|(\s*$)/g, '').replace(/\s+/g, '.'):""));
  598. function getRightStr(ele, noTextEnable){
  599. let childNodes=ele.childNodes,cStr="\r\n",hasText=false;
  600. for(let j=0;j<childNodes.length;j++){
  601. let childNode=childNodes[j];
  602. if(childNode.nodeType==3 && childNode.data && !/^[\s\-\_\?\>\|]*$/.test(childNode.data))hasText=true;
  603. if(childNode.innerHTML){
  604. childNode.innerHTML=childNode.innerHTML.replace(/\<\s*br\s*\>/gi,"\r\n").replace(/\n+/gi,"\n").replace(/\r+/gi,"\r");
  605. }
  606. if(childNode.textContent){
  607. cStr+=childNode.textContent.replace(/ +/g," ").replace(/([^\r]|^)\n([^\r]|$)/gi,"$1\r\n$2");
  608. }
  609. if(childNode.nodeType!=3 && !/^(I|A|STRONG|B|FONT)$/.test(childNode.tagName))cStr+="\r\n";
  610. }
  611. if(hasText || noTextEnable || ele==largestContent)rStr+=cStr+"\r\n";
  612. }
  613. for(i=0;i<childlist.length;i++){
  614. var child=childlist[i];
  615. if(getDepth(child)==getDepth(largestContent)){
  616. if((!largestContent.className && child.className) || (largestContent.className && !child.className) || (largestContent.className && child.className && largestContent.className != child.className))continue;
  617. if((largestContent.className && largestContent.className==child.className)||largestContent.parentNode ==child.parentNode){
  618. getRightStr(child, true);
  619. }else {
  620. getRightStr(child, false);
  621. }
  622. }
  623. }
  624. return rStr.replace(/[\n\r]+/g,"\n\r");
  625. }
  626.  
  627. function getI18n(key, args){
  628. var resultStr=i18n[key];
  629. if(args && args.length>0){
  630. args.forEach(function(item){
  631. resultStr=resultStr.replace(/%s/,item);
  632. });
  633. }
  634. return resultStr;
  635. }
  636.  
  637. function getDepth(dom){
  638. var pa=dom,i=0;
  639. while(pa.parentNode){
  640. pa=pa.parentNode;
  641. i++;
  642. }
  643. return i;
  644. }
  645.  
  646. function fetch(forceSingle){
  647. forceSingle=forceSingle===true;
  648. processFunc=null;
  649. var aEles=document.querySelectorAll("a:not(#search-jumper a)"),list=[];
  650. for(var i=0;i<aEles.length;i++){
  651. var aEle=aEles[i],has=false;
  652. if((!aEle.href || aEle.href.indexOf("javascript")!=-1) && aEle.dataset.href){
  653. aEle.href=aEle.dataset.href;
  654. }
  655. for(var j=0;j<list.length;j++){
  656. if(list[j].href==aEle.href){
  657. aEle=list[j];
  658. list.splice(j,1);
  659. list.push(aEle);
  660. has=true;
  661. break;
  662. }
  663. }
  664. if(!has && aEle.href && /^http/i.test(aEle.href) && ((aEle.innerText.trim()!="" && indexReg.test(aEle.innerText.trim())) || /chapter[\-_]?\d/.test(aEle.href))){
  665. list.push(aEle);
  666. }
  667. }
  668. if(list.length>2 && !forceSingle){
  669. indexDownload(list);
  670. }else{
  671. var blob = new Blob([i18n.info+"\r\n\r\n"+document.title+"\r\n\r\n"+getPageContent(document)], {type: "text/plain;charset=utf-8"});
  672. saveAs(blob, document.title+".txt");
  673. }
  674. }
  675.  
  676. document.addEventListener("keydown", function(e) {
  677. if(e.keyCode == 120 && e.ctrlKey) {
  678. fetch(e.shiftKey);
  679. }
  680. });
  681. function setDel(){
  682. var selValue=GM_getValue("selectors");
  683. var selectors=prompt(i18n.del,selValue?selValue:"");
  684. GM_setValue("selectors",selectors);
  685. selValue=GM_getValue("downThreadNum");
  686. var downThreadNum=prompt(i18n.downThreadNum,selValue?selValue:"20");
  687. GM_setValue("downThreadNum",downThreadNum);
  688. var sortByUrl=window.confirm(i18n.reSortUrl);
  689. GM_setValue("contentSortUrl",sortByUrl);
  690. if(!sortByUrl)GM_setValue("contentSort",window.confirm(i18n.reSort));
  691. }
  692. function customDown(){
  693. processFunc=null;
  694. var customRules=GM_getValue("DACrules_"+document.domain);
  695. var urls=window.prompt(i18n.customInfo,customRules?customRules:"https://xxx.xxx/book-[20-99].html, https://xxx.xxx/book-[01-10].html");
  696. if(urls){
  697. urls=decodeURIComponent(urls.replace(/%/g,'%25'));
  698. GM_setValue("DACrules_"+document.domain, urls);
  699. var processEles=[];
  700. let urlsArr=urls.split("@@"),eles=[];
  701. if(/^http|^ftp/.test(urlsArr[0])){
  702. [].forEach.call(urlsArr[0].split(","),function(i){
  703. var curEle;
  704. var varNum=/\[\d+\-\d+\]/.exec(i);
  705. if(varNum){
  706. varNum=varNum[0].trim();
  707. }else{
  708. curEle=document.createElement("a");
  709. curEle.href=i;
  710. processEles.push(curEle);
  711. return;
  712. }
  713. var num1=/\[(\d+)/.exec(varNum)[1].trim();
  714. var num2=/(\d+)\]/.exec(varNum)[1].trim();
  715. var num1Int=parseInt(num1);
  716. var num2Int=parseInt(num2);
  717. var numLen=num1.length;
  718. var needAdd=num1.charAt(0)=="0";
  719. if(num1Int>=num2Int)return;
  720. for(var j=num1Int;j<=num2Int;j++){
  721. var urlIndex=j.toString();
  722. if(needAdd){
  723. while(urlIndex.length<numLen)urlIndex="0"+urlIndex;
  724. }
  725. var curUrl=i.replace(/\[\d+\-\d+\]/,urlIndex).trim();
  726. curEle=document.createElement("a");
  727. curEle.href=curUrl;
  728. curEle.innerText=processEles.length.toString();
  729. processEles.push(curEle);
  730. }
  731. });
  732. }else{
  733. let urlSel=urlsArr[0].split(">>");
  734. try{
  735. eles=document.querySelectorAll(urlSel[0]);
  736. eles=[].filter.call(eles, ele=>{
  737. return ele.tagName=='BODY'||(!!ele.offsetParent&&getComputedStyle(ele).display!=='none');
  738. })
  739. }catch(e){}
  740. if(eles.length==0){
  741. eles=[];
  742. var eleTxts=urlsArr[0].split(/(?<=[^\\])[,,]/),exmpEles=[],excludeTxts={};
  743. [].forEach.call(document.querySelectorAll("a"),function(item){
  744. eleTxts.forEach(txt=>{
  745. var txtArr=txt.split("!");
  746. if(item.innerText.indexOf(txtArr[0])!=-1){
  747. exmpEles.push(item);
  748. excludeTxts[item]=txtArr.splice(1);
  749. }
  750. });
  751. })
  752. exmpEles.forEach(e=>{
  753. var cssSelStr="a",pa=e.parentNode,excludeTxt=excludeTxts[e];
  754. if(e.className)cssSelStr+="."+CSS.escape(e.className);
  755. while(pa && pa.nodeName!="BODY"){
  756. cssSelStr=pa.nodeName+">"+cssSelStr;
  757. pa=pa.parentNode;
  758. }
  759. [].forEach.call(document.querySelectorAll(cssSelStr),function(item){
  760. var isExclude=false;
  761. for(var t in excludeTxt){
  762. if(item.innerText.indexOf(excludeTxt[t])!=-1){
  763. isExclude=true;
  764. break;
  765. }
  766. }
  767. if(!isExclude && eles.indexOf(item)==-1){
  768. eles.push(item);
  769. }
  770. });
  771. });
  772. }
  773. function addItem(item) {
  774. let has=false;
  775. for(var j=0;j<processEles.length;j++){
  776. if(processEles[j].href==item.href){
  777. processEles.splice(j,1);
  778. processEles.push(item);
  779. has=true;
  780. break;
  781. }
  782. }
  783. if((!item.href || item.href.indexOf("javascript")!=-1) && item.dataset.href){
  784. item.href=item.dataset.href;
  785. }
  786. if(!has && item.href && /^http/i.test(item.href)){
  787. processEles.push(item.cloneNode(1));
  788. }
  789. }
  790. [].forEach.call(eles,function(item){
  791. if(urlSel[1]){
  792. item=Function("item",urlSel[1])(item);
  793. let items;
  794. if (Array.isArray(item)) {
  795. items = item;
  796. } else items = [item];
  797. items.forEach(item => {
  798. if(!item || !item.href)return;
  799. if(!item.tagName || item.tagName!="A"){
  800. let href=item.href;
  801. let innerText=item.innerText;
  802. item=document.createElement("a");
  803. item.href=href;
  804. item.innerText=innerText;
  805. }
  806. addItem(item);
  807. });
  808. } else {
  809. addItem(item);
  810. }
  811. });
  812. }
  813. if(urlsArr[1]){
  814. processEles.forEach(ele=>{
  815. ele.href=ele.href.replace(new RegExp(urlsArr[1]), urlsArr[2]);
  816. });
  817. }
  818. if(urlsArr[3]){
  819. processFunc=(data, cb)=>{
  820. if(urlsArr[3].indexOf("return ")==-1){
  821. return eval(urlsArr[3])
  822. }else{
  823. return Function("data","cb",urlsArr[3])(data, cb);
  824. }
  825. };
  826. }else{
  827. var win=(typeof unsafeWindow=='undefined'? window : unsafeWindow);
  828. if(win.dacProcess){
  829. processFunc=win.dacProcess;
  830. }
  831. }
  832. indexDownload(processEles);
  833. }
  834. }
  835. GM_registerMenuCommand(i18n.fetch, fetch);
  836. GM_registerMenuCommand(i18n.custom, customDown);
  837. GM_registerMenuCommand(i18n.setting, setDel);
  838. })();