怠惰小说下载器

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

目前为 2024-05-08 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name DownloadAllContent
  3. // @name:zh-CN 怠惰小说下载器
  4. // @name:zh-TW 怠惰小説下載器
  5. // @name:ja 怠惰者小説ダウンロードツール
  6. // @namespace hoothin
  7. // @version 2.8.3.5
  8. // @description Lightweight web scraping script. Fetch and download main textual content from the current page, provide special support for novels
  9. // @description:zh-CN 通用网站内容爬虫抓取工具,可批量抓取任意站点的小说、论坛内容等并保存为TXT文档
  10. // @description:zh-TW 通用網站內容爬蟲抓取工具,可批量抓取任意站點的小說、論壇內容等並保存為TXT文檔
  11. // @description:ja 軽量なWebスクレイピングスクリプト。ユニバーサルサイトコンテンツクロールツール、クロール、フォーラム内容など
  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 GM_openInTab
  21. // @grant GM_setClipboard
  22. // @grant GM_addStyle
  23. // @grant unsafeWindow
  24. // @license MIT License
  25. // @compatible chrome
  26. // @compatible firefox
  27. // @compatible opera 未测试
  28. // @compatible safari 未测试
  29. // @contributionURL https://ko-fi.com/hoothin
  30. // @contributionAmount 1
  31. // ==/UserScript==
  32.  
  33. if (window.top != window.self) {
  34. try {
  35. if (window.self.innerWidth < 250 || window.self.innerHeight < 250) {
  36. return;
  37. }
  38. } catch(e) {
  39. return;
  40. }
  41. }
  42.  
  43. (function (global, factory) {
  44. if (typeof define === "function" && define.amd) {
  45. define([], factory);
  46. } else if (typeof exports !== "undefined") {
  47. factory();
  48. } else {
  49. var mod = {
  50. exports: {}
  51. };
  52. factory();
  53. global.FileSaver = mod.exports;
  54. }
  55. })(this, function () {
  56. "use strict";
  57.  
  58. /*
  59. * FileSaver.js
  60. * A saveAs() FileSaver implementation.
  61. *
  62. * By Eli Grey, http://eligrey.com
  63. *
  64. * License : https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md (MIT)
  65. * source : http://purl.eligrey.com/github/FileSaver.js
  66. */
  67. 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;
  68.  
  69. function bom(blob, opts) {
  70. if (typeof opts === 'undefined') opts = {
  71. autoBom: false
  72. };else if (typeof opts !== 'object') {
  73. console.warn('Deprecated: Expected third argument to be a object');
  74. opts = {
  75. autoBom: !opts
  76. };
  77. }
  78.  
  79. if (opts.autoBom && /^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
  80. return new Blob([String.fromCharCode(0xFEFF), blob], {
  81. type: blob.type
  82. });
  83. }
  84.  
  85. return blob;
  86. }
  87.  
  88. function download(url, name, opts) {
  89. var xhr = new XMLHttpRequest();
  90. xhr.open('GET', url);
  91. xhr.responseType = 'blob';
  92.  
  93. xhr.onload = function () {
  94. saveAs(xhr.response, name, opts);
  95. };
  96.  
  97. xhr.onerror = function () {
  98. console.error('could not download file');
  99. };
  100.  
  101. xhr.send();
  102. }
  103.  
  104. function corsEnabled(url) {
  105. var xhr = new XMLHttpRequest();
  106.  
  107. xhr.open('HEAD', url, false);
  108.  
  109. try {
  110. xhr.send();
  111. } catch (e) {}
  112.  
  113. return xhr.status >= 200 && xhr.status <= 299;
  114. }
  115.  
  116.  
  117. function click(node) {
  118. try {
  119. node.dispatchEvent(new MouseEvent('click'));
  120. } catch (e) {
  121. var evt = document.createEvent('MouseEvents');
  122. evt.initMouseEvent('click', true, true, window, 0, 0, 0, 80, 20, false, false, false, false, 0, null);
  123. node.dispatchEvent(evt);
  124. }
  125. }
  126.  
  127.  
  128. var isMacOSWebView = _global.navigator && /Macintosh/.test(navigator.userAgent) && /AppleWebKit/.test(navigator.userAgent) && !/Safari/.test(navigator.userAgent);
  129. var saveAs = _global.saveAs || (
  130. typeof window !== 'object' || window !== _global ? function saveAs() {}
  131.  
  132. : 'download' in HTMLAnchorElement.prototype && !isMacOSWebView ? function saveAs(blob, name, opts) {
  133. var URL = _global.URL || _global.webkitURL;
  134. var a = document.createElement('a');
  135. name = name || blob.name || 'download';
  136. a.download = name;
  137. a.rel = 'noopener';
  138.  
  139. if (typeof blob === 'string') {
  140. a.href = blob;
  141.  
  142. if (a.origin !== location.origin) {
  143. corsEnabled(a.href) ? download(blob, name, opts) : click(a, a.target = '_blank');
  144. } else {
  145. click(a);
  146. }
  147. } else {
  148. a.href = URL.createObjectURL(blob);
  149. setTimeout(function () {
  150. URL.revokeObjectURL(a.href);
  151. }, 4E4);
  152.  
  153. setTimeout(function () {
  154. click(a);
  155. }, 0);
  156. }
  157. }
  158. : 'msSaveOrOpenBlob' in navigator ? function saveAs(blob, name, opts) {
  159. name = name || blob.name || 'download';
  160.  
  161. if (typeof blob === 'string') {
  162. if (corsEnabled(blob)) {
  163. download(blob, name, opts);
  164. } else {
  165. var a = document.createElement('a');
  166. a.href = blob;
  167. a.target = '_blank';
  168. setTimeout(function () {
  169. click(a);
  170. });
  171. }
  172. } else {
  173. navigator.msSaveOrOpenBlob(bom(blob, opts), name);
  174. }
  175. }
  176. : function saveAs(blob, name, opts, popup) {
  177. popup = popup || open('', '_blank');
  178.  
  179. if (popup) {
  180. popup.document.title = popup.document.body.innerText = 'downloading...';
  181. }
  182.  
  183. if (typeof blob === 'string') return download(blob, name, opts);
  184. var force = blob.type === 'application/octet-stream';
  185.  
  186. var isSafari = /constructor/i.test(_global.HTMLElement) || _global.safari;
  187.  
  188. var isChromeIOS = /CriOS\/[\d]+/.test(navigator.userAgent);
  189.  
  190. if ((isChromeIOS || force && isSafari || isMacOSWebView) && typeof FileReader !== 'undefined') {
  191. var reader = new FileReader();
  192.  
  193. reader.onloadend = function () {
  194. var url = reader.result;
  195. url = isChromeIOS ? url : url.replace(/^data:[^;]*;/, 'data:attachment/file;');
  196. if (popup) popup.location.href = url;else location = url;
  197. popup = null;
  198. };
  199.  
  200. reader.readAsDataURL(blob);
  201. } else {
  202. var URL = _global.URL || _global.webkitURL;
  203. var url = URL.createObjectURL(blob);
  204. if (popup) popup.location = url;else location.href = url;
  205. popup = null;
  206.  
  207. setTimeout(function () {
  208. URL.revokeObjectURL(url);
  209. }, 4E4);
  210. }
  211. });
  212. _global.saveAs = saveAs.saveAs = saveAs;
  213.  
  214. if (typeof module !== 'undefined') {
  215. module.exports = saveAs;
  216. }
  217. });
  218.  
  219. (function() {
  220. 'use strict';
  221. var indexReg=/^(\w.*)?PART\b|^Prologue|^(\w.*)?Chapter\s*[\-_]?\d+|分卷|^序$|^序\s*[·言章]|^前\s*言|^附\s*[录錄]|^引\s*[言子]|^摘\s*要|^[楔契]\s*子|^后\s*记|^後\s*記|^附\s*言|^结\s*语|^結\s*語|^尾\s*[声聲]|^最終話|^最终话|^番\s*外|^\d+[\s\.、,,)\-_::][^\d#\.]|^(\d|\s|\.)*[第(]?\s*[\d〇零一二两三四五六七八九十百千万萬-]+\s*[、)章节節回卷折篇幕集话話]/i;
  222. var innerNextPage=/^\s*(下一[页頁张張]|next\s*page|次のページ)/i;
  223. var lang = navigator.appName=="Netscape"?navigator.language:navigator.userLanguage;
  224. var i18n={};
  225. var rCats=[];
  226. var processFunc, nextPageFunc;
  227. const AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;
  228. var win=(typeof unsafeWindow=='undefined'? window : unsafeWindow);
  229. switch (lang){
  230. case "zh-CN":
  231. case "zh-SG":
  232. i18n={
  233. fetch:"开始下载小说",
  234. info:"来源:#t#\n本文是使用怠惰小说下载器(DownloadAllContent)下载的",
  235. error:"该段内容获取失败",
  236. downloading:"已下载完成 %s 段,剩余 %s 段<br>正在下载 %s",
  237. complete:"已全部下载完成,共 %s 段",
  238. del:"设置文本干扰码的CSS选择器",
  239. custom:"自定规则下载",
  240. customInfo:"输入网址或者章节CSS选择器",
  241. reSort:"按标题名重新排序章节",
  242. reSortUrl:"按网址重新排序章节",
  243. setting:"选项参数设置",
  244. searchRule:"搜索网站规则",
  245. abort:"跳过此章",
  246. save:"保存当前",
  247. saveAsMd:"存为 Markdown",
  248. downThreadNum:"设置同时下载的线程数",
  249. customTitle:"自定义章节标题,输入内页文字对应选择器",
  250. reSortDefault:"默认按页面中位置排序章节",
  251. reverseOrder:"反转章节排序",
  252. saveBtn:"保存设置",
  253. saveOk:"保存成功",
  254. nextPage:"嗅探章节内分页",
  255. nextPageReg:"自定义分页正则",
  256. retainImage:"保留正文中图片的网址",
  257. minTxtLength:"当检测到的正文字数小于此数,则尝试重新抓取",
  258. showFilterList:"下载前显示章节筛选排序窗口",
  259. ok:"确定",
  260. close:"关闭",
  261. dacSortByPos:"按页内位置排序",
  262. dacSortByUrl:"按网址排序",
  263. dacSortByName:"按章节名排序",
  264. reverse:"反选",
  265. dacUseIframe:"使用 iframe 后台加载内容(慢速)",
  266. dacSaveAsZip:"下载为 zip",
  267. dacSetCustomRule:"修改规则",
  268. dacAddUrl:"添加章节",
  269. dacStartDownload:"下载选中",
  270. downloadShortcut:"下载章节",
  271. downloadSingleShortcut:"下载单页",
  272. downloadCustomShortcut:"自定义下载"
  273. };
  274. break;
  275. case "zh-TW":
  276. case "zh-HK":
  277. i18n={
  278. fetch:"開始下載小說",
  279. info:"來源:#t#\n本文是使用怠惰小說下載器(DownloadAllContent)下載的",
  280. error:"該段內容獲取失敗",
  281. downloading:"已下載完成 %s 段,剩餘 %s 段<br>正在下載 %s",
  282. complete:"已全部下載完成,共 %s 段",
  283. del:"設置文本干擾碼的CSS選擇器",
  284. custom:"自訂規則下載",
  285. customInfo:"輸入網址或者章節CSS選擇器",
  286. reSort:"按標題名重新排序章節",
  287. reSortUrl:"按網址重新排序章節",
  288. setting:"選項參數設定",
  289. searchRule:"搜尋網站規則",
  290. abort:"跳過此章",
  291. save:"保存當前",
  292. saveAsMd:"存爲 Markdown",
  293. downThreadNum:"設置同時下載的綫程數",
  294. customTitle:"自訂章節標題,輸入內頁文字對應選擇器",
  295. reSortDefault:"預設依頁面中位置排序章節",
  296. reverseOrder:"反轉章節排序",
  297. saveBtn:"儲存設定",
  298. saveOk:"儲存成功",
  299. nextPage:"嗅探章節內分頁",
  300. nextPageReg:"自訂分頁正規",
  301. retainImage:"保留內文圖片的網址",
  302. minTxtLength:"當偵測到的正文字數小於此數,則嘗試重新抓取",
  303. showFilterList:"下載前顯示章節篩選排序視窗",
  304. ok:"確定",
  305. close:"關閉",
  306. dacSortByPos:"依頁內位置排序",
  307. dacSortByUrl:"依網址排序",
  308. dacSortByName:"依章節名排序",
  309. reverse:"反選",
  310. dacUseIframe:"使用 iframe 背景載入內容(慢速)",
  311. dacSaveAsZip:"下載為 zip",
  312. dacSetCustomRule:"修改規則",
  313. dacAddUrl:"新增章節",
  314. dacStartDownload:"下載選取",
  315. downloadShortcut:"下載章節",
  316. downloadSingleShortcut:"下載單頁",
  317. downloadCustomShortcut:"自設下載"
  318. };
  319. break;
  320. default:
  321. i18n={
  322. fetch:"Download",
  323. info:"Source: #t#\nThe TXT is downloaded by 'DownloadAllContent'",
  324. error:"Failed in downloading current chapter",
  325. downloading:"%s pages are downloaded, there are still %s pages left<br>Downloading %s ......",
  326. complete:"Completed! Get %s pages in total",
  327. del:"Set css selectors for ignore",
  328. custom:"Custom to download",
  329. customInfo:"Input urls OR sss selectors for chapter links",
  330. reSort:"ReSort by title",
  331. reSortUrl:"Resort by URLs",
  332. setting:"Open Setting",
  333. searchRule:"Search rule",
  334. abort:"Abort",
  335. save:"Save",
  336. saveAsMd:"Save as Markdown",
  337. downThreadNum:"Set threadNum for download",
  338. customTitle: "Customize the chapter title, enter the selector on inner page",
  339. reSortDefault: "Default sort by position in the page",
  340. reverseOrder:"Reverse chapter ordering",
  341. saveBtn:"Save Setting",
  342. saveOk:"Save Over",
  343. nextPage:"Check next page in chapter",
  344. nextPageReg:"Custom RegExp of next page",
  345. retainImage:"Keep the URL of image if there are images in the text",
  346. minTxtLength:"Try to crawl again when the length of content is less than this",
  347. showFilterList: "Show chapter filtering and sorting window before downloading",
  348. ok:"OK",
  349. close:"Close",
  350. dacSortByPos:"Sort by position",
  351. dacSortByUrl:"Sort by URL",
  352. dacSortByName:"Sort by name",
  353. reverse:"Reverse selection",
  354. dacUseIframe: "Use iframe to load content (slow)",
  355. dacSaveAsZip: "Save as zip",
  356. dacSetCustomRule:"Modify rules",
  357. dacAddUrl:"Add Chapter",
  358. dacStartDownload:"Download selected",
  359. downloadShortcut:"Download chapter",
  360. downloadSingleShortcut:"Download single page",
  361. downloadCustomShortcut:"Custom download"
  362. };
  363. break;
  364. }
  365. var firefox=navigator.userAgent.toLowerCase().indexOf('firefox')!=-1,curRequests=[],useIframe=false,iframeSandbox=false,iframeInit=false;
  366. var filterListContainer,txtDownContent,txtDownWords,txtDownQuit,dacLinksCon,dacUseIframe,shadowContainer;
  367.  
  368. const escapeHTMLPolicy = (win.trustedTypes && win.trustedTypes.createPolicy) ? win.trustedTypes.createPolicy('dac_default', {
  369. createHTML: (string, sink) => string
  370. }) : null;
  371.  
  372. function createHTML(html) {
  373. return escapeHTMLPolicy ? escapeHTMLPolicy.createHTML(html) : html;
  374. }
  375.  
  376. function str2Num(str) {
  377. str = str.replace(/^番\s*外/, "99999+").replace(/[一①Ⅰ壹]/g, "1").replace(/[二②Ⅱ贰]/g, "2").replace(/[三③Ⅲ叁]/g, "3").replace(/[四④Ⅳ肆]/g, "4").replace(/[五⑤Ⅴ伍]/g, "5").replace(/[六⑥Ⅵ陆]/g, "6").replace(/[七⑦Ⅶ柒]/g, "7").replace(/[八⑧Ⅷ捌]/g, "8").replace(/[九⑨Ⅸ玖]/g, "9").replace(/[十⑩Ⅹ拾]/g, "*10+").replace(/[百佰]/g, "*100+").replace(/[千仟]/g, "*1000+").replace(/[万萬]/g, "*10000+").replace(/\s/g, "").match(/[\d\*\+]+/);
  378. if (!str) return 0;
  379. str = str[0];
  380. let mul = str.match(/(\d*)\*(\d+)/);
  381. while(mul) {
  382. let result = parseInt(mul[1] || 1) * parseInt(mul[2]);
  383. str = str.replace(mul[0], result);
  384. mul = str.match(/(\d+)\*(\d+)/);
  385. }
  386. let plus = str.match(/(\d+)\+(\d+)/);
  387. while(plus) {
  388. let result = parseInt(plus[1]) + parseInt(plus[2]);
  389. str = str.replace(plus[0], result);
  390. plus = str.match(/(\d+)\+(\d+)/);
  391. }
  392. return parseInt(str);
  393. }
  394.  
  395. var dragOverItem, dragFrom, linkDict;
  396. function createLinkItem(aEle) {
  397. let item = document.createElement("div");
  398. item.innerHTML = createHTML(`
  399. <input type="checkbox" checked>
  400. <a class="dacLink" draggable="false" target="_blank" href="${aEle.href}">${aEle.innerText || "📄"}</a>
  401. <span>🖱️</span>
  402. `);
  403. item.title = aEle.innerText;
  404. item.setAttribute("draggable", "true");
  405. item.addEventListener("dragover", e => {
  406. e.preventDefault();
  407. });
  408. item.addEventListener("dragenter", e => {
  409. if (dragOverItem) dragOverItem.style.opacity = "";
  410. item.style.opacity = 0.3;
  411. dragOverItem = item;
  412. });
  413. item.addEventListener('dragstart', e => {
  414. dragFrom = item;
  415. });
  416. item.addEventListener('drop', e => {
  417. if (!dragFrom) return;
  418. if (e.clientX < item.getBoundingClientRect().left + 142) {
  419. dacLinksCon.insertBefore(dragFrom, item);
  420. } else {
  421. if (item.nextElementSibling) {
  422. dacLinksCon.insertBefore(dragFrom, item.nextElementSibling);
  423. } else {
  424. dacLinksCon.appendChild(dragFrom);
  425. }
  426. }
  427. e.preventDefault();
  428. });
  429. linkDict[aEle.href] = item;
  430. dacLinksCon.appendChild(item);
  431. }
  432.  
  433. var saveAsZip = true;
  434. function filterList(list) {
  435. if (!GM_getValue("showFilterList")) {
  436. indexDownload(list);
  437. return;
  438. }
  439. if (txtDownContent) {
  440. txtDownContent.style.display = "none";
  441. }
  442. if (filterListContainer) {
  443. filterListContainer.style.display = "";
  444. filterListContainer.classList.remove("customRule");
  445. dacLinksCon.innerHTML = createHTML("");
  446. } else {
  447. document.addEventListener('dragend', e => {
  448. if (dragOverItem) dragOverItem.style.opacity = "";
  449. }, true);
  450. filterListContainer = document.createElement("div");
  451. filterListContainer.id = "filterListContainer";
  452. filterListContainer.innerHTML = createHTML(`
  453. <div id="dacFilterBg" style="height: 100%; width: 100%; position: fixed; top: 0; z-index: 99998; opacity: 0.3; filter: alpha(opacity=30); background-color: #000;"></div>
  454. <div id="filterListBody">
  455. <div class="dacCustomRule">
  456. ${i18n.custom}
  457. <textarea id="dacCustomInput"></textarea>
  458. <div class="fun">
  459. <input id="dacConfirmRule" value="${i18n.ok}" type="button"/>
  460. <input id="dacCustomClose" value="${i18n.close}" type="button"/>
  461. </div>
  462. </div>
  463. <div class="sort">
  464. <input id="dacSortByPos" value="${i18n.dacSortByPos}" type="button"/>
  465. <input id="dacSortByUrl" value="${i18n.dacSortByUrl}" type="button"/>
  466. <input id="dacSortByName" value="${i18n.dacSortByName}" type="button"/>
  467. <input id="reverse" value="${i18n.reverse}" type="button"/>
  468. </div>
  469. <div id="dacLinksCon" style="max-height: calc(80vh - 100px); min-height: 100px; display: grid; grid-template-columns: auto auto; width: 100%; overflow: auto; white-space: nowrap;"></div>
  470. <p style="margin: 5px; text-align: center; font-size: 14px; height: 20px;"><span><input id="dacUseIframe" type="checkbox"/><label for="dacUseIframe"> ${i18n.dacUseIframe}</label></span> <span style="display:${win.downloadAllContentSaveAsZip ? "inline" : "none"}"><input id="dacSaveAsZip" type="checkbox" checked="checked"/><label for="dacSaveAsZip"> ${i18n.dacSaveAsZip}</label></span></p>
  471. <div class="fun">
  472. <input id="dacSetCustomRule" value="${i18n.dacSetCustomRule}" type="button"/>
  473. <input id="dacAddUrl" value="${i18n.dacAddUrl}" type="button"/>
  474. <input id="dacStartDownload" value="${i18n.dacStartDownload}" type="button"/>
  475. <input id="dacLinksClose" value="${i18n.close}" type="button"/>
  476. </div>
  477. </div>`);
  478. let dacSortByPos = filterListContainer.querySelector("#dacSortByPos");
  479. let dacSortByUrl = filterListContainer.querySelector("#dacSortByUrl");
  480. let dacSortByName = filterListContainer.querySelector("#dacSortByName");
  481. let reverse = filterListContainer.querySelector("#reverse");
  482. let dacSetCustomRule = filterListContainer.querySelector("#dacSetCustomRule");
  483. let dacCustomInput = filterListContainer.querySelector("#dacCustomInput");
  484. let dacConfirmRule = filterListContainer.querySelector("#dacConfirmRule");
  485. let dacCustomClose = filterListContainer.querySelector("#dacCustomClose");
  486. let dacAddUrl = filterListContainer.querySelector("#dacAddUrl");
  487. let dacStartDownload = filterListContainer.querySelector("#dacStartDownload");
  488. let dacLinksClose = filterListContainer.querySelector("#dacLinksClose");
  489. let dacFilterBg = filterListContainer.querySelector("#dacFilterBg");
  490. let dacSaveAsZip = filterListContainer.querySelector("#dacSaveAsZip");
  491. dacUseIframe = filterListContainer.querySelector("#dacUseIframe");
  492. dacSaveAsZip.onchange = e => {
  493. saveAsZip = dacSaveAsZip.checked;
  494. };
  495. dacSortByPos.onclick = e => {
  496. let linkList = [].slice.call(dacLinksCon.children);
  497. if (linkList[0].children[1].href != list[0].href) {
  498. list.reverse().forEach(a => {
  499. let link = linkDict[a.href];
  500. if (!link) return;
  501. dacLinksCon.insertBefore(link, dacLinksCon.children[0]);
  502. });
  503. } else {
  504. list.forEach(a => {
  505. let link = linkDict[a.href];
  506. if (!link) return;
  507. dacLinksCon.insertBefore(link, dacLinksCon.children[0]);
  508. });
  509. }
  510. };
  511. dacSortByUrl.onclick = e => {
  512. let linkList = [].slice.call(dacLinksCon.children);
  513. linkList.sort((a, b) => {
  514. const nameA = a.children[1].href.toUpperCase();
  515. const nameB = b.children[1].href.toUpperCase();
  516. if (nameA < nameB) {
  517. return -1;
  518. }
  519. if (nameA > nameB) {
  520. return 1;
  521. }
  522. return 0;
  523. });
  524. if (linkList[0] == dacLinksCon.children[0]) {
  525. linkList = linkList.reverse();
  526. }
  527. linkList.forEach(link => {
  528. dacLinksCon.appendChild(link);
  529. });
  530. };
  531. dacSortByName.onclick = e => {
  532. let linkList = [].slice.call(dacLinksCon.children);
  533. linkList.sort((a, b) => {
  534. return str2Num(a.innerText) - str2Num(b.innerText);
  535. });
  536. if (linkList[0] == dacLinksCon.children[0]) {
  537. linkList = linkList.reverse();
  538. }
  539. linkList.forEach(link => {
  540. dacLinksCon.appendChild(link);
  541. });
  542. };
  543. reverse.onclick = e => {
  544. let linkList = [].slice.call(dacLinksCon.children);
  545. linkList.forEach(link => {
  546. link.children[0].checked=!link.children[0].checked;
  547. });
  548. };
  549. dacSetCustomRule.onclick = e => {
  550. filterListContainer.classList.add("customRule");
  551. dacCustomInput.value = GM_getValue("DACrules_" + document.domain) || "";
  552. };
  553. dacConfirmRule.onclick = e => {
  554. if (dacCustomInput.value) {
  555. customDown(dacCustomInput.value);
  556. }
  557. };
  558. dacCustomClose.onclick = e => {
  559. filterListContainer.classList.remove("customRule");
  560. };
  561. dacAddUrl.onclick = e => {
  562. let addUrls = window.prompt(i18n.customInfo, "https://xxx.xxx/book-[20-99].html, https://xxx.xxx/book-[01-10].html");
  563. if (!addUrls || !/^http|^ftp/.test(addUrls)) return;
  564. let index = 1;
  565. [].forEach.call(addUrls.split(","), function(i) {
  566. var curEle;
  567. var varNum = /\[\d+\-\d+\]/.exec(i);
  568. if (varNum) {
  569. varNum = varNum[0].trim();
  570. } else {
  571. curEle = document.createElement("a");
  572. curEle.href = i;
  573. curEle.innerText = "Added Url";
  574. createLinkItem(curEle);
  575. return;
  576. }
  577. var num1 = /\[(\d+)/.exec(varNum)[1].trim();
  578. var num2 = /(\d+)\]/.exec(varNum)[1].trim();
  579. var num1Int = parseInt(num1);
  580. var num2Int = parseInt(num2);
  581. var numLen = num1.length;
  582. var needAdd = num1.charAt(0) == "0";
  583. if (num1Int >= num2Int) return;
  584. for (var j = num1Int; j <= num2Int; j++) {
  585. var urlIndex = j.toString();
  586. if (needAdd) {
  587. while(urlIndex.length < numLen) urlIndex = "0" + urlIndex;
  588. }
  589. var curUrl = i.replace(/\[\d+\-\d+\]/, urlIndex).trim();
  590. curEle = document.createElement("a");
  591. curEle.href = curUrl;
  592. curEle.innerText = "Added Url " + index++;
  593. createLinkItem(curEle);
  594. }
  595. });
  596. };
  597. dacStartDownload.onclick = e => {
  598. let linkList = [].slice.call(dacLinksCon.querySelectorAll("input:checked+.dacLink"));
  599. useIframe = !!dacUseIframe.checked;
  600. indexDownload(linkList, true);
  601. };
  602. dacLinksClose.onclick = e => {
  603. filterListContainer.style.display = "none";
  604. };
  605. dacFilterBg.onclick = e => {
  606. filterListContainer.style.display = "none";
  607. };
  608. let listStyle = GM_addStyle(`
  609. #filterListContainer * {
  610. font-size: 13px;
  611. float: initial;
  612. background-image: initial;
  613. height: fit-content;
  614. color: black;
  615. }
  616. #filterListContainer.customRule .dacCustomRule {
  617. display: flex;
  618. }
  619. #filterListContainer .dacCustomRule>textarea {
  620. height: 300px;
  621. width: 100%;
  622. border: 1px #DADADA solid;
  623. background: #ededed70;
  624. margin: 5px;
  625. }
  626. #filterListContainer.customRule .dacCustomRule~* {
  627. display: none!important;
  628. }
  629. #dacLinksCon>div {
  630. padding: 5px 0;
  631. display: flex;
  632. }
  633. #dacLinksCon>div>a {
  634. max-width: 245px;
  635. display: inline-block;
  636. text-overflow: ellipsis;
  637. overflow: hidden;
  638. }
  639. #dacLinksCon>div>input {
  640. margin-right: 5px;
  641. }
  642. #filterListContainer .dacCustomRule {
  643. border-radius: 8px;
  644. font-weight: bold;
  645. font-size: 16px;
  646. outline: none;
  647. align-items: center;
  648. flex-wrap: nowrap;
  649. white-space: nowrap;
  650. flex-direction: column;
  651. display: none;
  652. }
  653. #filterListContainer input {
  654. border-width: 2px;
  655. border-style: outset;
  656. border-color: buttonface;
  657. border-image: initial;
  658. border: 1px #DADADA solid;
  659. padding: 5px;
  660. border-radius: 8px;
  661. font-weight: bold;
  662. font-size: 9pt;
  663. outline: none;
  664. cursor: pointer;
  665. line-height: initial;
  666. width: initial;
  667. min-width: initial;
  668. max-width: initial;
  669. height: initial;
  670. min-height: initial;
  671. max-height: initial;
  672. }
  673. #dacLinksCon>div:nth-of-type(4n),
  674. #dacLinksCon>div:nth-of-type(4n+1) {
  675. background: #ffffff;
  676. }
  677. #dacLinksCon>div:nth-of-type(4n+2),
  678. #dacLinksCon>div:nth-of-type(4n+3) {
  679. background: #f5f5f5;
  680. }
  681. #filterListContainer .fun,#filterListContainer .sort {
  682. display: flex;
  683. justify-content: space-around;
  684. flex-wrap: nowrap;
  685. width: 100%;
  686. height: 28px;
  687. }
  688. #filterListContainer input[type=button]:hover {
  689. border: 1px #C6C6C6 solid;
  690. box-shadow: 1px 1px 1px #EAEAEA;
  691. color: #333333;
  692. background: #F7F7F7;
  693. }
  694. #filterListContainer input[type=button]:active {
  695. box-shadow: inset 1px 1px 1px #DFDFDF;
  696. }
  697. #filterListBody {
  698. padding: 5px;
  699. box-sizing: border-box;
  700. overflow: hidden;
  701. width: 600px;
  702. height: auto;
  703. max-height: 80vh;
  704. min-height: 200px;
  705. position: fixed;
  706. left: 50%;
  707. top: 10%;
  708. margin-left: -300px;
  709. z-index: 99998;
  710. background-color: #ffffff;
  711. border: 1px solid #afb3b6;
  712. border-radius: 10px;
  713. opacity: 0.95;
  714. filter: alpha(opacity=95);
  715. box-shadow: 5px 5px 20px 0px #000;
  716. }
  717. @media screen and (max-width: 800px) {
  718. #filterListBody {
  719. width: 90%;
  720. margin-left: -45%;
  721. }
  722. }
  723. `);
  724. dacLinksCon = filterListContainer.querySelector("#dacLinksCon");
  725. shadowContainer = document.createElement("div");
  726. document.body.appendChild(shadowContainer);
  727. let shadow = shadowContainer.attachShadow({ mode: "open" });
  728. shadow.appendChild(listStyle);
  729. shadow.appendChild(filterListContainer);
  730. }
  731. if (shadowContainer.parentNode) shadowContainer.parentNode.removeChild(shadowContainer);
  732. linkDict = {};
  733. list.forEach(a => {
  734. createLinkItem(a);
  735. });
  736. dacUseIframe.checked = useIframe;
  737. document.body.appendChild(shadowContainer);
  738. }
  739.  
  740. function initTxtDownDiv() {
  741. if (txtDownContent) {
  742. txtDownContent.style.display = "";
  743. return;
  744. }
  745. txtDownContent = document.createElement("div");
  746. txtDownContent.id = "txtDownContent";
  747. let shadowContainer = document.createElement("div");
  748. document.body.appendChild(shadowContainer);
  749. let shadow = shadowContainer.attachShadow({ mode: "open" });
  750. shadow.appendChild(txtDownContent);
  751. txtDownContent.innerHTML=createHTML(`
  752. <style>
  753. #txtDownContent>div{
  754. font-size:16px;
  755. color:#333333;
  756. width:362px;
  757. height:110px;
  758. position:fixed;
  759. left:50%;
  760. top:50%;
  761. margin-top:-25px;
  762. margin-left:-191px;
  763. z-index:100000;
  764. background-color:#ffffff;
  765. border:1px solid #afb3b6;
  766. border-radius:10px;
  767. opacity:0.95;
  768. filter:alpha(opacity=95);
  769. box-shadow:5px 5px 20px 0px #000;
  770. }
  771. #txtDownWords{
  772. position:absolute;
  773. width:275px;
  774. height: 90px;
  775. max-height: 90%;
  776. border: 1px solid #f3f1f1;
  777. padding: 8px;
  778. border-radius: 10px;
  779. overflow: auto;
  780. }
  781. #txtDownQuit{
  782. width: 30px;height: 30px;border-radius: 30px;position:absolute;right:2px;top:2px;cursor: pointer;background-color:#ff5a5a;
  783. }
  784. #txtDownQuit>span{
  785. height: 30px;line-height: 30px;display:block;color:#FFF;text-align:center;font-size: 12px;font-weight: bold;font-family: arial;background: initial; float: initial;
  786. }
  787. #txtDownQuit+div{
  788. position:absolute;right:0px;bottom:2px;cursor: pointer;max-width:85px;
  789. }
  790. #txtDownQuit+div>button{
  791. background: #008aff;border: 0;padding: 5px;border-radius: 6px;color: white;float: right;margin: 1px;height: 25px;line-height: 16px;cursor: pointer;overflow: hidden;
  792. }
  793. </style>
  794. <div>
  795. <div id="txtDownWords">
  796. Analysing......
  797. </div>
  798. <div id="txtDownQuit">
  799. <span>╳</span>
  800. </div>
  801. <div>
  802. <button id="abortRequest" style="display:none;">${getI18n('abort')}</button>
  803. <button id="tempSaveTxt">${getI18n('save')}</button>
  804. <button id="saveAsMd" title="${getI18n('saveAsMd')}">Markdown</button>
  805. </div>
  806. </div>`);
  807. txtDownWords=txtDownContent.querySelector("#txtDownWords");
  808. txtDownQuit=txtDownContent.querySelector("#txtDownQuit");
  809. txtDownQuit.onclick=function(){
  810. txtDownContent.style.display="none";
  811. };
  812. initTempSave(txtDownContent);
  813. win.txtDownWords = txtDownWords;
  814. }
  815.  
  816. function saveContent() {
  817. if (win.downloadAllContentSaveAsZip && saveAsZip) {
  818. win.downloadAllContentSaveAsZip(rCats, i18n.info.replace("#t#", location.href), content => {
  819. saveAs(content, document.title + ".zip");
  820. });
  821. } else {
  822. var blob = new Blob([i18n.info.replace("#t#", location.href) + "\r\n\r\n" + document.title + "\r\n\r\n" + rCats.join("\r\n\r\n")], {type: "text/plain;charset=utf-8"});
  823. saveAs(blob, document.title + ".txt");
  824. }
  825. }
  826.  
  827. function initTempSave(txtDownContent){
  828. var tempSavebtn = txtDownContent.querySelector('#tempSaveTxt');
  829. var abortbtn = txtDownContent.querySelector('#abortRequest');
  830. var saveAsMd = txtDownContent.querySelector('#saveAsMd');
  831. tempSavebtn.onclick = function(){
  832. saveContent();
  833. console.log(curRequests);
  834. }
  835. abortbtn.onclick = function(){
  836. let curRequest = curRequests.pop();
  837. if(curRequest)curRequest[1].abort();
  838. }
  839. saveAsMd.onclick = function(){
  840. let txt = i18n.info.replace("#t#", location.href)+"\n\n---\n"+document.title+"\n===\n";
  841. rCats.forEach(cat => {
  842. cat = cat.replace("\r\n", "\n---").replace(/(\r\n|\n\r)+/g, "\n\n").replace(/[\n\r]\t+/g, "\n");
  843. txt += '\n\n'+cat;
  844. });
  845. var blob = new Blob([txt], {type: "text/plain;charset=utf-8"});
  846. saveAs(blob, document.title+".md");
  847. }
  848. }
  849.  
  850. let charset = (document.characterSet || document.charset || document.inputEncoding);
  851. let equiv = document.querySelector('[http-equiv="Content-Type"]'), charsetValid = true;
  852. if (equiv && equiv.content) {
  853. let innerCharSet = equiv.content.match(/charset\=([^;]+)/);
  854. if (!innerCharSet) {
  855. charsetValid = false;
  856. } else if (innerCharSet[1].replace("-", "").toLowerCase() != charset.replace("-", "").toLowerCase()) {
  857. charsetValid = false;
  858. }
  859. } else charsetValid = false;
  860. function indexDownload(aEles, noSort){
  861. if(aEles.length<1)return;
  862. initTxtDownDiv();
  863. if(!noSort) {
  864. if(GM_getValue("contentSort")){
  865. aEles.sort((a, b) => {
  866. return str2Num(a.innerText) - str2Num(b.innerText);
  867. });
  868. }
  869. if(GM_getValue("contentSortUrl")){
  870. aEles.sort((a, b) => {
  871. const nameA = a.href.toUpperCase();
  872. const nameB = b.href.toUpperCase();
  873. if (nameA < nameB) {
  874. return -1;
  875. }
  876. if (nameA > nameB) {
  877. return 1;
  878. }
  879. return 0;
  880. });
  881. }
  882. if(GM_getValue("reverse")){
  883. aEles=aEles.reverse();
  884. }
  885. }
  886. rCats=[];
  887. var minTxtLength=GM_getValue("minTxtLength") || 100;
  888. var customTitle=GM_getValue("customTitle");
  889. var disableNextPage=!!GM_getValue("disableNextPage");
  890. var customNextPageReg=GM_getValue("nextPageReg");
  891. if (customNextPageReg) {
  892. try {
  893. innerNextPage = new RegExp(customNextPageReg);
  894. } catch(e) {
  895. console.warn(e);
  896. }
  897. }
  898. var insertSigns=[];
  899. // var j=0,rCats=[];
  900. var downIndex=0,downNum=0,downOnce=function(wait){
  901. if(downNum>=aEles.length)return;
  902. let curIndex=downIndex;
  903. let aTag=aEles[curIndex];
  904. let request=(aTag, curIndex)=>{
  905. let tryTimes=0;
  906. let validTimes=0;
  907. function requestDoc(_charset) {
  908. if (!_charset) _charset = charset;
  909. return GM_xmlhttpRequest({
  910. method: 'GET',
  911. url: aTag.href,
  912. headers:{
  913. referer:aTag.href,
  914. "Content-Type":"text/html;charset="+_charset
  915. },
  916. timeout:10000,
  917. overrideMimeType:"text/html;charset="+_charset,
  918. onload: async function(result) {
  919. let doc = getDocEle(result.responseText);
  920. if (charsetValid) {
  921. let equiv = doc.querySelector('[http-equiv="Content-Type"]');
  922. if (equiv && equiv.content) {
  923. let innerCharSet = equiv.content.match(/charset\=([^;]+)/);
  924. if (innerCharSet && innerCharSet[1].replace("-", "").toLowerCase() != _charset.replace("-", "").toLowerCase()) {
  925. charset = innerCharSet[1];
  926. return requestDoc(charset);
  927. }
  928. }
  929. }
  930. downIndex++;
  931. downNum++;
  932. if (/^{/.test(result.responseText)) {
  933. doc.json = () => {
  934. try {
  935. return JSON.parse(result.responseText);
  936. } catch(e) {}
  937. return {};
  938. }
  939. }
  940. let base = doc.querySelector("base");
  941. let nextPages = !disableNextPage && !processFunc && await checkNextPage(doc, base ? base.href : aTag.href);
  942. if (nextPages) {
  943. if (!nextPages.length) nextPages = [nextPages];
  944. nextPages.forEach(nextPage => {
  945. var inArr=false;
  946. for(var ai=0;ai<aEles.length;ai++){
  947. if(aEles[ai].href==nextPage.href){
  948. inArr=true;
  949. break;
  950. }
  951. }
  952. if(!inArr){
  953. nextPage.innerText=aTag.innerText+"\t>>";
  954. aEles.push(nextPage);
  955. let targetIndex = curIndex;
  956. for(let a=0;a<insertSigns.length;a++){
  957. let signs=insertSigns[a],breakSign=false;
  958. if(signs){
  959. for(let b=0;b<signs.length;b++){
  960. let sign=signs[b];
  961. if(sign==curIndex){
  962. targetIndex=a;
  963. breakSign=true;
  964. break;
  965. }
  966. }
  967. }
  968. if(breakSign)break;
  969. }
  970. let insertSign = insertSigns[targetIndex];
  971. if(!insertSign)insertSigns[targetIndex] = [];
  972. insertSigns[targetIndex].push(aEles.length-1);
  973. }
  974. });
  975. }
  976. if (result.status >= 400) {
  977. console.warn("error:", `status: ${result.status} from: ${aTag.href}`);
  978. } else {
  979. console.log(result.status);
  980. }
  981. if (customTitle) {
  982. try {
  983. let title = doc.querySelector(customTitle);
  984. if (title && title.innerText) {
  985. aTag.innerText = title.innerText;
  986. }
  987. } catch(e) {
  988. console.warn(e);
  989. }
  990. }
  991. let validData = processDoc(curIndex, aTag, doc, (result.status>=400?` status: ${result.status} from: ${aTag.href} `:""), validTimes < 5);
  992. if (!validData && validTimes++ < 5) {
  993. downIndex--;
  994. downNum--;
  995. setTimeout(() => {
  996. requestDoc();
  997. }, Math.random() * 500 + validTimes * 1000);
  998. return;
  999. }
  1000. if (wait) {
  1001. setTimeout(() => {
  1002. downOnce(wait);
  1003. }, wait);
  1004. } else downOnce();
  1005. },
  1006. onerror: function(e) {
  1007. console.warn("error:", e, aTag.href);
  1008. if(tryTimes++ < 5){
  1009. setTimeout(() => {
  1010. requestDoc();
  1011. }, Math.random() * 500 + tryTimes * 1000);
  1012. return;
  1013. }
  1014. downIndex++;
  1015. downNum++;
  1016. processDoc(curIndex, aTag, null, ` NETWORK ERROR: ${(e.response||e.responseText)} from: ${aTag.href} `);
  1017. if (wait) {
  1018. setTimeout(() => {
  1019. downOnce(wait);
  1020. }, wait);
  1021. } else downOnce();
  1022. },
  1023. ontimeout: function(e) {
  1024. console.warn("timeout: times="+(tryTimes+1)+" url="+aTag.href);
  1025. //console.log(e);
  1026. if(tryTimes++ < 5){
  1027. setTimeout(() => {
  1028. requestDoc();
  1029. }, Math.random() * 500 + tryTimes * 1000);
  1030. return;
  1031. }
  1032. downIndex++;
  1033. downNum++;
  1034. processDoc(curIndex, aTag, null, ` TIMEOUT: ${aTag.href} `);
  1035. if (wait) {
  1036. setTimeout(() => {
  1037. downOnce(wait);
  1038. }, wait);
  1039. } else downOnce();
  1040. }
  1041. });
  1042. };
  1043. if (useIframe) {
  1044. let iframe = document.createElement('iframe'), inited = false, failedTimes = 0;
  1045. iframe.name = 'pagetual-iframe';
  1046. iframe.width = '100%';
  1047. iframe.height = '1000';
  1048. iframe.frameBorder = '0';
  1049. iframe.sandbox = iframeSandbox || "allow-same-origin allow-scripts allow-popups allow-forms";
  1050. iframe.style.cssText = 'margin:0!important;padding:0!important;visibility:hidden!important;flex:0;opacity:0!important;pointer-events:none!important;position:fixed;top:0px;left:0px;z-index:-2147483647;';
  1051. iframe.addEventListener('load', e => {
  1052. if (e.data != 'pagetual-iframe:DOMLoaded' && e.type != 'load') return;
  1053. if (inited) return;
  1054. inited = true;
  1055. async function checkIframe() {
  1056. try {
  1057. let doc = iframe.contentDocument || iframe.contentWindow.document;
  1058. if (!doc || !doc.body) {
  1059. setTimeout(() => {
  1060. checkIframe();
  1061. }, 1000);
  1062. return;
  1063. }
  1064. doc.body.scrollTop = 9999999;
  1065. doc.documentElement.scrollTop = 9999999;
  1066. if (!processFunc && validTimes++ > 5 && failedTimes++ < 2) {
  1067. iframe.src = iframe.src;
  1068. validTimes = 0;
  1069. inited = false;
  1070. return;
  1071. }
  1072. let base = doc.querySelector("base");
  1073. let nextPages = !disableNextPage && !processFunc && await checkNextPage(doc, base ? base.href : aTag.href);
  1074. if (nextPages) {
  1075. if (!nextPages.length) nextPages = [nextPages];
  1076. nextPages.forEach(nextPage => {
  1077. var inArr=false;
  1078. for(var ai=0;ai<aEles.length;ai++){
  1079. if(aEles[ai].href==nextPage.href){
  1080. inArr=true;
  1081. break;
  1082. }
  1083. }
  1084. if(!inArr){
  1085. nextPage.innerText=aTag.innerText+"\t>>";
  1086. aEles.push(nextPage);
  1087. let targetIndex = curIndex;
  1088. for(let a=0;a<insertSigns.length;a++){
  1089. let signs=insertSigns[a],breakSign=false;
  1090. if(signs){
  1091. for(let b=0;b<signs.length;b++){
  1092. let sign=signs[b];
  1093. if(sign==curIndex){
  1094. targetIndex=a;
  1095. breakSign=true;
  1096. break;
  1097. }
  1098. }
  1099. }
  1100. if(breakSign)break;
  1101. }
  1102. let insertSign = insertSigns[targetIndex];
  1103. if(!insertSign)insertSigns[targetIndex] = [];
  1104. insertSigns[targetIndex].push(aEles.length-1);
  1105. }
  1106. });
  1107. }
  1108. if (customTitle) {
  1109. try {
  1110. let title = doc.querySelector(customTitle);
  1111. if (title && title.innerText) {
  1112. aTag.innerText = title.innerText;
  1113. }
  1114. } catch(e) {
  1115. console.warn(e);
  1116. }
  1117. }
  1118. downIndex++;
  1119. downNum++;
  1120. let validData = processDoc(curIndex, aTag, doc, "", failedTimes < 2);
  1121. if (!validData) {
  1122. downIndex--;
  1123. downNum--;
  1124. setTimeout(() => {
  1125. checkIframe();
  1126. }, 1000);
  1127. return;
  1128. }
  1129. if (wait) {
  1130. setTimeout(() => {
  1131. downOnce(wait);
  1132. }, wait);
  1133. } else downOnce();
  1134. } catch(e) {
  1135. console.debug("Stop as cors");
  1136. }
  1137. if (iframe && iframe.parentNode) iframe.parentNode.removeChild(iframe);
  1138. }
  1139. setTimeout(() => {
  1140. checkIframe();
  1141. }, 500);
  1142. }, false);
  1143. let checkReady = setInterval(() => {
  1144. let doc;
  1145. try {
  1146. doc = iframe.contentDocument || (iframe.contentWindow && iframe.contentWindow.document);
  1147. } catch(e) {
  1148. clearInterval(checkReady);
  1149. return;
  1150. }
  1151. if (doc) {
  1152. try {
  1153. Function('win', 'iframe', '"use strict";' + (iframeInit || "win.self=win.top;"))(iframe.contentWindow, iframe);
  1154. clearInterval(checkReady);
  1155. } catch(e) {
  1156. console.debug(e);
  1157. }
  1158. }
  1159. }, 50);
  1160. iframe.src = aTag.href;
  1161. document.body.appendChild(iframe);
  1162. return [curIndex, null, aTag.href];
  1163. } else {
  1164. return [curIndex, requestDoc(), aTag.href];
  1165. }
  1166. }
  1167. if(!aTag){
  1168. let waitAtagReadyInterval=setInterval(function(){
  1169. if(downNum>=aEles.length)clearInterval(waitAtagReadyInterval);
  1170. aTag=aEles[curIndex];
  1171. if(aTag){
  1172. clearInterval(waitAtagReadyInterval);
  1173. request(aTag, curIndex);
  1174. }
  1175. },1000);
  1176. return null;
  1177. }
  1178. let result = request(aTag, curIndex);
  1179. if (result) curRequests.push(result);
  1180. return result;
  1181. };
  1182. function getDocEle(str){
  1183. var doc = null;
  1184. try {
  1185. doc = document.implementation.createHTMLDocument('');
  1186. doc.documentElement.innerHTML = str;
  1187. }
  1188. catch (e) {
  1189. console.log('parse error');
  1190. }
  1191. return doc;
  1192. }
  1193. function sortInnerPage(){
  1194. var pageArrs=[],maxIndex=0,i,j;
  1195. for(i=0;i<insertSigns.length;i++){
  1196. var signs=insertSigns[i];
  1197. if(signs){
  1198. for(j=0;j<signs.length;j++){
  1199. var sign=signs[j];
  1200. var cat=rCats[sign];
  1201. rCats[sign]=null;
  1202. if(!pageArrs[i])pageArrs[i]=[];
  1203. pageArrs[i].push(cat);
  1204. }
  1205. }
  1206. }
  1207. for(i=pageArrs.length-1;i>=0;i--){
  1208. let pageArr=pageArrs[i];
  1209. if(pageArr){
  1210. for(j=pageArr.length-1;j>=0;j--){
  1211. rCats.splice(i+1, 0, pageArr[j]);
  1212. }
  1213. }
  1214. }
  1215. rCats = rCats.filter(function(e){return e!=null});
  1216. }
  1217. var waitForComplete;
  1218. function processDoc(i, aTag, doc, cause, check){
  1219. let cbFunc=content=>{
  1220. rCats[i]=(aTag.innerText.replace(/[\r\n\t]/g, "") + "\r\n" + (cause || '') + content.replace(/\s*$/, ""));
  1221. curRequests = curRequests.filter(function(e){return e[0]!=i});
  1222. txtDownContent.style.display="block";
  1223. txtDownWords.innerHTML=getI18n("downloading",[downNum,(aEles.length-downNum),aTag.innerText]);
  1224. if(downNum==aEles.length){
  1225. if(waitForComplete) clearTimeout(waitForComplete);
  1226. waitForComplete=setTimeout(()=>{
  1227. if(downNum==aEles.length){
  1228. txtDownWords.innerHTML=getI18n("complete",[downNum]);
  1229. sortInnerPage();
  1230. saveContent();
  1231. }
  1232. },3000);
  1233. }
  1234. };
  1235. let contentResult=getPageContent(doc, content=>{
  1236. cbFunc(content);
  1237. }, aTag.href);
  1238. if(contentResult!==false){
  1239. if(check && contentResult && contentResult.replace(/\s/g, "").length<minTxtLength){
  1240. return false;
  1241. }
  1242. cbFunc(contentResult);
  1243. }
  1244. return true;
  1245. }
  1246. var downThreadNum = parseInt(GM_getValue("downThreadNum"));
  1247. downThreadNum = downThreadNum || 20;
  1248. if (useIframe && downThreadNum > 5) {
  1249. downThreadNum = 5;
  1250. }
  1251. if (downThreadNum > 0) {
  1252. for (var i = 0; i < downThreadNum; i++) {
  1253. downOnce();
  1254. if (downIndex >= aEles.length - 1 || downIndex >= downThreadNum - 1) break;
  1255. else downIndex++;
  1256. }
  1257. } else {
  1258. downOnce(-downThreadNum * 1000);
  1259. if (downIndex < aEles.length - 1 && downIndex < downThreadNum - 1) downIndex++;
  1260. }
  1261.  
  1262. /*for(let i=0;i<aEles.length;i++){
  1263. let aTag=aEles[i];
  1264. GM_xmlhttpRequest({
  1265. method: 'GET',
  1266. url: aTag.href,
  1267. overrideMimeType:"text/html;charset="+document.charset,
  1268. onload: function(result) {
  1269. var doc = getDocEle(result.responseText);
  1270. processDoc(i, aTag, doc);
  1271. }
  1272. });
  1273. }*/
  1274. }
  1275.  
  1276. function canonicalUri(src, baseUrl) {
  1277. if (!src) {
  1278. return "";
  1279. }
  1280. if (src.charAt(0) == "#") return baseUrl + src;
  1281. if (src.charAt(0) == "?") return baseUrl.replace(/^([^\?#]+).*/, "$1" + src);
  1282. let origin = location.protocol + '//' + location.host;
  1283. let url = baseUrl || origin;
  1284. url = url.replace(/(\?|#).*/, "");
  1285. if (/https?:\/\/[^\/]+$/.test(url)) url = url + '/';
  1286. if (url.indexOf("http") !== 0) url = origin + url;
  1287. var root_page = /^[^\?#]*\//.exec(url)[0],
  1288. root_domain = /^\w+\:\/\/\/?[^\/]+/.exec(root_page)[0],
  1289. absolute_regex = /^\w+\:\/\//;
  1290. while (src.indexOf("../") === 0) {
  1291. src = src.substr(3);
  1292. root_page = root_page.replace(/\/[^\/]+\/$/, "/");
  1293. }
  1294. src = src.replace(/\.\//, "");
  1295. if (/^\/\/\/?/.test(src)) {
  1296. src = location.protocol + src;
  1297. }
  1298. return (absolute_regex.test(src) ? src : ((src.charAt(0) == "/" ? root_domain : root_page) + src));
  1299. }
  1300.  
  1301. async function checkNextPage(doc, baseUrl) {
  1302. let nextPage = null;
  1303. if (nextPageFunc) {
  1304. nextPage = await nextPageFunc(doc, baseUrl);
  1305. if (nextPage && nextPage.length === 0) nextPage = null;
  1306. } else {
  1307. let aTags = doc.querySelectorAll("a");
  1308. for (var i = 0; i < aTags.length; i++) {
  1309. let aTag = aTags[i];
  1310. if (innerNextPage.test(aTag.innerText) && aTag.href && !/javascript:|#/.test(aTag.href)) {
  1311. let nextPageHref = canonicalUri(aTag.getAttribute("href"), baseUrl || location.href);
  1312. if (nextPageHref != location.href) {
  1313. nextPage = aTag;
  1314. nextPage.href = nextPageHref;
  1315. break;
  1316. }
  1317. }
  1318. }
  1319. }
  1320. return nextPage;
  1321. }
  1322.  
  1323. function textNodesUnder(el){
  1324. var n, a=[], walk=document.createTreeWalker(el,NodeFilter.SHOW_TEXT,null,false);
  1325. while(n=walk.nextNode()) a.push(n);
  1326. return a;
  1327. }
  1328.  
  1329. function getPageContent(doc, cb, url){
  1330. if(!doc)return i18n.error;
  1331. if(doc.body && !doc.body.children.length)return doc.body.innerText;
  1332. if(processFunc){
  1333. return processFunc(doc, cb, url);
  1334. }
  1335. [].forEach.call(doc.querySelectorAll("span,div,ul"),function(item){
  1336. var thisStyle=doc.defaultView?doc.defaultView.getComputedStyle(item):item.style;
  1337. if(thisStyle && (thisStyle.display=="none" || (item.nodeName=="SPAN" && thisStyle.fontSize=="0px"))){
  1338. item.innerHTML="";
  1339. }
  1340. });
  1341. var i,j,k,rStr="",pageData=(doc.body?doc.body:doc).cloneNode(true);
  1342. pageData.innerHTML=pageData.innerHTML.replace(/\<\!\-\-((.|[\n|\r|\r\n])*?)\-\-\>/g,"");
  1343. [].forEach.call(pageData.querySelectorAll("font.jammer"),function(item){
  1344. item.innerHTML="";
  1345. });
  1346. var selectors=GM_getValue("selectors");
  1347. if(selectors){
  1348. [].forEach.call(pageData.querySelectorAll(selectors),function(item){
  1349. item.innerHTML="";
  1350. });
  1351. }
  1352. [].forEach.call(pageData.querySelectorAll("script,style,link,noscript,iframe"),function(item){
  1353. if (item && item.parentNode) {
  1354. item.parentNode.removeChild(item);
  1355. }
  1356. });
  1357. var endEle = ele => {
  1358. return /^(I|STRONG|B|FONT|P|DL|DD|H\d)$/.test(ele.nodeName) && ele.children.length <= 1;
  1359. };
  1360. var largestContent,contents=pageData.querySelectorAll("span,div,article,p,td,pre"),largestNum=0;
  1361. for(i=0;i<contents.length;i++){
  1362. let content=contents[i],hasText=false,allSingle=true,item,curNum=0;
  1363. if(/footer/.test(content.className))continue;
  1364. for(j=content.childNodes.length-1;j>=0;j--){
  1365. item=content.childNodes[j];
  1366. if(item.nodeType==3){
  1367. if(/^\s*$/.test(item.data)){
  1368. item.innerHTML="";
  1369. }else hasText=true;
  1370. }else if(/^(I|A|STRONG|B|FONT|P|DL|DD|H\d)$/.test(item.nodeName)){
  1371. hasText=true;
  1372. }else if(item.nodeType==1&&item.children.length==1&&/^(I|A|STRONG|B|FONT|P|DL|DD|H\d)$/.test(item.children[0].nodeName)){
  1373. hasText=true;
  1374. }
  1375. }
  1376. for(j=content.childNodes.length-1;j>=0;j--){
  1377. item=content.childNodes[j];
  1378. if(item.nodeType==1 && !/^(I|A|STRONG|B|FONT|BR)$/.test(item.nodeName) && /^[\s\-\_\?\>\|]*$/.test(item.innerHTML)){
  1379. item.innerHTML="";
  1380. }
  1381. }
  1382. if(content.childNodes.length>1){
  1383. let indexItem=0;
  1384. for(j=0;j<content.childNodes.length;j++){
  1385. item=content.childNodes[j];
  1386. if(item.nodeType==1){
  1387. if(item.innerText && item.innerText.length<50 && indexReg.test(item.innerText))indexItem++;
  1388. for(k=0;k<item.childNodes.length;k++){
  1389. var childNode=item.childNodes[k];
  1390. if(childNode.nodeType!=3 && !/^(I|A|STRONG|B|FONT|BR)$/.test(childNode.nodeName)){
  1391. allSingle=false;
  1392. break;
  1393. }
  1394. }
  1395. if(!allSingle)break;
  1396. }
  1397. }
  1398. if(indexItem>=5)continue;
  1399. }else{
  1400. allSingle=false;
  1401. }
  1402. if(!allSingle && !hasText){
  1403. continue;
  1404. }else {
  1405. if(pageData==document && content.offsetWidth<=0 && content.offsetHeight<=0){
  1406. continue;
  1407. }
  1408. [].forEach.call(content.childNodes,function(item){
  1409. if(item.nodeType==3)curNum+=item.data.trim().length;
  1410. else if(endEle(item) || (item.nodeType == 1 && item.children.length == 1 && endEle(item.children[0]))) curNum += (firefox ? item.textContent.trim().length : item.innerText.trim().length);
  1411. });
  1412. }
  1413. if(curNum>largestNum){
  1414. largestNum=curNum;
  1415. largestContent=content;
  1416. }
  1417. }
  1418. if(!largestContent)return i18n.error+" : NO TEXT CONTENT";
  1419. var retainImage=!!GM_getValue("retainImage");
  1420. function getContentByLargest() {
  1421. var childlist=pageData.querySelectorAll(largestContent.nodeName);//+(largestContent.className?"."+largestContent.className.replace(/(^\s*)|(\s*$)/g, '').replace(/\s+/g, '.'):""));
  1422. function getRightStr(ele, noTextEnable){
  1423. [].forEach.call(ele.querySelectorAll("a[href]"), a => {
  1424. a.parentNode && a.parentNode.removeChild(a);
  1425. });
  1426. if(retainImage){
  1427. [].forEach.call(ele.querySelectorAll("img[src]"), img => {
  1428. let imgTxtNode=document.createTextNode(`![img](${canonicalUri(img.getAttribute("src"), url || location.href)})`);
  1429. img.parentNode.replaceChild(imgTxtNode, img);
  1430. });
  1431. }
  1432. let childNodes=ele.childNodes,cStr="\r\n",hasText=false;
  1433. for(let j=0;j<childNodes.length;j++){
  1434. let childNode=childNodes[j];
  1435. if(childNode.nodeType==3 && childNode.data && !/^[\s\-\_\?\>\|]*$/.test(childNode.data))hasText=true;
  1436. if(childNode.innerHTML){
  1437. childNode.innerHTML=childNode.innerHTML.replace(/\<\s*br\s*\>/gi,"\r\n").replace(/\n+/gi,"\n").replace(/\r+/gi,"\r");
  1438. }
  1439. let content=childNode.textContent;
  1440. if(content){
  1441. if(!content.trim())continue;
  1442. cStr+=content.replace(/[\uFEFF\xA0 ]+/g," ").replace(/([^\r]|^)\n([^\r]|$)/gi,"$1\r\n$2");
  1443. }
  1444. if(childNode.nodeType!=3 && !/^(I|A|STRONG|B|FONT|IMG)$/.test(childNode.nodeName))cStr+="\r\n";
  1445. }
  1446. if(hasText || noTextEnable || ele==largestContent)rStr+=cStr+"\r\n";
  1447. }
  1448. var sameDepthChildren=[];
  1449. for(i=0;i<childlist.length;i++){
  1450. var child=childlist[i];
  1451. if(getDepth(child)==getDepth(largestContent)){
  1452. if(largestContent.className != child.className)continue;
  1453. sameDepthChildren.push(child);
  1454. }
  1455. }
  1456. var minLength = largestNum>>2;
  1457. var tooShort = sameDepthChildren.length <= 3;
  1458. sameDepthChildren.forEach(child => {
  1459. if(tooShort && child.innerText.length < minLength) return;
  1460. if((largestContent.className && largestContent.className == child.className) || largestContent.parentNode == child.parentNode){
  1461. getRightStr(child, true);
  1462. }else {
  1463. getRightStr(child, false);
  1464. }
  1465. });
  1466. rStr = rStr.replace(/[\n\r]+/g,"\n\r");
  1467. }
  1468. getContentByLargest();
  1469. if (rStr.length < 100) {
  1470. let articles = pageData.querySelectorAll("article");
  1471. if (articles && articles.length == 1) {
  1472. largestContent = articles[0];
  1473. largestNum = largestContent.innerText.length;
  1474. if (largestNum > 100) {
  1475. rStr = "";
  1476. getContentByLargest();
  1477. }
  1478. }
  1479. }
  1480. return rStr;
  1481. }
  1482.  
  1483. function getI18n(key, args){
  1484. var resultStr=i18n[key];
  1485. if(args && args.length>0){
  1486. args.forEach(function(item){
  1487. resultStr=resultStr.replace(/%s/,item);
  1488. });
  1489. }
  1490. return resultStr;
  1491. }
  1492.  
  1493. function getDepth(dom){
  1494. var pa=dom,i=0;
  1495. while(pa.parentNode){
  1496. pa=pa.parentNode;
  1497. i++;
  1498. }
  1499. return i;
  1500. }
  1501.  
  1502. async function sleep(time) {
  1503. await new Promise((resolve) => {
  1504. setTimeout(() => {
  1505. resolve();
  1506. }, time);
  1507. })
  1508. }
  1509.  
  1510. async function fetch(forceSingle){
  1511. forceSingle=forceSingle===true;
  1512. processFunc=null;
  1513. initTxtDownDiv();
  1514. var aEles=document.body.querySelectorAll("a"),list=[];
  1515. txtDownWords.innerHTML=`Analysing ( 1/${aEles.length} )......`;
  1516. txtDownContent.style.pointerEvents="none";
  1517. for(var i=0;i<aEles.length;i++){
  1518. if (i % 100 == 0) {
  1519. await sleep(1);
  1520. }
  1521. txtDownWords.innerHTML=`Analysing ( ${i + 1}/${aEles.length} )......`;
  1522. var aEle=aEles[i],has=false;
  1523. if(aEle.dataset.href && (!aEle.href || aEle.href.indexOf("javascript")!=-1)){
  1524. aEle.href=aEle.dataset.href;
  1525. }
  1526. if(aEle.href==location.href)continue;
  1527. for(var j=0;j<list.length;j++){
  1528. if(list[j].href==aEle.href){
  1529. aEle=list[j];
  1530. list.splice(j,1);
  1531. list.push(aEle);
  1532. has=true;
  1533. break;
  1534. }
  1535. }
  1536. if(!has && aEle.href && /^http/i.test(aEle.href) && ((aEle.innerText.trim()!="" && indexReg.test(aEle.innerText.trim())) || /chapter[\-_]?\d/.test(aEle.href))){
  1537. list.push(aEle);
  1538. }
  1539. }
  1540. txtDownContent.style.display="none";
  1541. txtDownContent.style.pointerEvents="";
  1542. txtDownWords.innerHTML="Analysing......";
  1543. if(list.length>2 && !forceSingle){
  1544. useIframe = false;
  1545. filterList(list);
  1546. }else{
  1547. var blob = new Blob([i18n.info.replace("#t#", location.href)+"\r\n\r\n"+document.title+"\r\n\r\n"+getPageContent(document)], {type: "text/plain;charset=utf-8"});
  1548. saveAs(blob, document.title+".txt");
  1549. }
  1550. }
  1551.  
  1552. function customDown(urls){
  1553. processFunc = null;
  1554. useIframe = false;
  1555. if(urls){
  1556. urls=decodeURIComponent(urls.replace(/%/g,'%25'));
  1557. GM_setValue("DACrules_"+document.domain, urls);
  1558. var processEles=[];
  1559. let urlsArr=urls.split("@@"),eles=[];
  1560. if(/^http|^ftp/.test(urlsArr[0])){
  1561. [].forEach.call(urlsArr[0].split(","),function(i){
  1562. var curEle;
  1563. var varNum=/\[\d+\-\d+\]/.exec(i);
  1564. if(varNum){
  1565. varNum=varNum[0].trim();
  1566. }else{
  1567. curEle=document.createElement("a");
  1568. curEle.href=i;
  1569. curEle.innerText="Added Url";
  1570. processEles.push(curEle);
  1571. return;
  1572. }
  1573. var num1=/\[(\d+)/.exec(varNum)[1].trim();
  1574. var num2=/(\d+)\]/.exec(varNum)[1].trim();
  1575. var num1Int=parseInt(num1);
  1576. var num2Int=parseInt(num2);
  1577. var numLen=num1.length;
  1578. var needAdd=num1.charAt(0)=="0";
  1579. if(num1Int>=num2Int)return;
  1580. for(var j=num1Int;j<=num2Int;j++){
  1581. var urlIndex=j.toString();
  1582. if(needAdd){
  1583. while(urlIndex.length<numLen)urlIndex="0"+urlIndex;
  1584. }
  1585. var curUrl=i.replace(/\[\d+\-\d+\]/,urlIndex).trim();
  1586. curEle=document.createElement("a");
  1587. curEle.href=curUrl;
  1588. curEle.innerText="Added Url " + processEles.length.toString();
  1589. processEles.push(curEle);
  1590. }
  1591. });
  1592. }else{
  1593. let urlSel=urlsArr[0].split(">>");
  1594. try{
  1595. eles=document.querySelectorAll(urlSel[0]);
  1596. eles=[].filter.call(eles, ele=>{
  1597. return ele.nodeName=='BODY'||(!!ele.offsetParent&&getComputedStyle(ele).display!=='none');
  1598. })
  1599. }catch(e){}
  1600. if(eles.length==0){
  1601. eles=[];
  1602. var eleTxts=urlsArr[0].split(/(?<=[^\\])[,,]/),exmpEles=[],excludeTxts={};
  1603. [].forEach.call(document.querySelectorAll("a"),function(item){
  1604. if(!item.offsetParent)return;
  1605. eleTxts.forEach(txt=>{
  1606. var txtArr=txt.split("!");
  1607. if(item.innerText.indexOf(txtArr[0])!=-1){
  1608. exmpEles.push(item);
  1609. excludeTxts[item]=txtArr.splice(1);
  1610. }
  1611. });
  1612. })
  1613. exmpEles.forEach(e=>{
  1614. var cssSelStr="a",pa=e.parentNode,excludeTxt=excludeTxts[e];
  1615. if(e.className)cssSelStr+="."+CSS.escape(e.className.replace(/\s+/g, ".")).replace(/\\\./g, '.');
  1616. while(pa && pa.nodeName!="BODY"){
  1617. cssSelStr=pa.nodeName+">"+cssSelStr;
  1618. pa=pa.parentNode;
  1619. }
  1620. cssSelStr="body>"+cssSelStr;;
  1621. [].forEach.call(document.querySelectorAll(cssSelStr),function(item){
  1622. if(!item.offsetParent)return;
  1623. var isExclude=false;
  1624. for(var t in excludeTxt){
  1625. if(item.innerText.indexOf(excludeTxt[t])!=-1){
  1626. isExclude=true;
  1627. break;
  1628. }
  1629. }
  1630. if(!isExclude && eles.indexOf(item)==-1){
  1631. eles.push(item);
  1632. }
  1633. });
  1634. });
  1635. }
  1636. function addItem(item) {
  1637. let has=false;
  1638. for(var j=0;j<processEles.length;j++){
  1639. if(processEles[j].href==item.href){
  1640. processEles.splice(j,1);
  1641. processEles.push(item);
  1642. has=true;
  1643. break;
  1644. }
  1645. }
  1646. if((!item.href || item.href.indexOf("javascript")!=-1) && item.dataset.href){
  1647. item.href=item.dataset.href;
  1648. }
  1649. if(!has && item.href && /^http/i.test(item.href)){
  1650. processEles.push(item.cloneNode(1));
  1651. }
  1652. }
  1653. [].forEach.call(eles,function(item){
  1654. if(urlSel[1]){
  1655. item=Function("item",urlSel[1])(item);
  1656. let items;
  1657. if (Array.isArray(item)) {
  1658. items = item;
  1659. } else items = [item];
  1660. items.forEach(item => {
  1661. if(!item || !item.href)return;
  1662. if(!item.nodeName || item.nodeName!="A"){
  1663. let href=item.href;
  1664. let innerText=item.innerText;
  1665. item=document.createElement("a");
  1666. item.href=href;
  1667. item.innerText=innerText;
  1668. }
  1669. addItem(item);
  1670. });
  1671. } else {
  1672. addItem(item);
  1673. }
  1674. });
  1675. }
  1676. if(urlsArr[1]){
  1677. processEles.forEach(ele=>{
  1678. ele.href=ele.href.replace(new RegExp(urlsArr[1]), urlsArr[2]);
  1679. });
  1680. }
  1681. var retainImage=!!GM_getValue("retainImage");
  1682. var evalCode = urlsArr[3];
  1683. if (evalCode) {
  1684. evalCode = evalCode.trim();
  1685. if (/^iframe:/.test(evalCode)) {
  1686. evalCode = evalCode.replace("iframe:", "");
  1687. useIframe = true;
  1688. iframeSandbox = false;
  1689. iframeInit = false;
  1690. while (/^(sandbox|init):/.test(evalCode)) {
  1691. iframeSandbox = evalCode.match(/^sandbox:\{(.*?)\}/);
  1692. if (iframeSandbox) {
  1693. evalCode = evalCode.replace(iframeSandbox[0], "");
  1694. iframeSandbox = iframeSandbox[1];
  1695. }
  1696. iframeInit = evalCode.match(/^init:\{(.*?)\}/);
  1697. if (iframeInit) {
  1698. evalCode = evalCode.replace(iframeInit[0], "");
  1699. iframeInit = iframeInit[1];
  1700. }
  1701. }
  1702. }
  1703. let charsetMatch = evalCode.match(/^charset:{(.+?)}/);
  1704. if (charsetMatch) {
  1705. charset = charsetMatch[1];
  1706. evalCode = evalCode.replace(charsetMatch[0], "");
  1707. }
  1708. let nextMatch = evalCode.match(/^next:(\{+)/);
  1709. if (nextMatch) {
  1710. let splitLen = nextMatch[1].length;
  1711. nextMatch = evalCode.match(new RegExp(`^next:\\{{${splitLen}}(.*?)\\}{${splitLen}}`));
  1712. if (nextMatch) {
  1713. let nextCode = nextMatch[1];
  1714. evalCode = evalCode.replace(nextMatch[0], "");
  1715. nextPageFunc = async (doc, url) => {
  1716. let result;
  1717. if (/\breturn\b/.test(nextCode)) {
  1718. result = await new AsyncFunction('doc', 'url', '"use strict";' + nextCode)(doc, url);
  1719. } else {
  1720. try {
  1721. result = doc.querySelectorAll(nextCode);
  1722. if (result && result.length) {
  1723. [].forEach.call(result, ele => {
  1724. ele.href = canonicalUri(ele.getAttribute("href"), url || location.href);
  1725. });
  1726. } else result = null;
  1727. } catch(e) {}
  1728. }
  1729. return result;
  1730. }
  1731. }
  1732. }
  1733. }
  1734. if(evalCode){
  1735. processFunc=(data, cb, url)=>{
  1736. let doc=data;
  1737. if(evalCode.indexOf("return ")==-1){
  1738. if(evalCode.indexOf("@")==0){
  1739. let content="";
  1740. if(retainImage){
  1741. [].forEach.call(data.querySelectorAll("img[src]"), img => {
  1742. let imgTxt=`![img](${canonicalUri(img.getAttribute("src"), location.href)})`;
  1743. let imgTxtNode=document.createTextNode(imgTxt);
  1744. img.parentNode.replaceChild(imgTxtNode, img);
  1745. });
  1746. }
  1747. [].forEach.call(data.querySelectorAll(evalCode.slice(1)), ele=>{
  1748. [].forEach.call(ele.childNodes, child=>{
  1749. if(child.innerHTML){
  1750. child.innerHTML=child.innerHTML.replace(/\<\s*br\s*\>/gi,"\r\n").replace(/\n+/gi,"\n").replace(/\r+/gi,"\r");
  1751. }
  1752. if(child.textContent){
  1753. content+=(child.textContent.replace(/ +/g," ").replace(/([^\r]|^)\n([^\r]|$)/gi,"$1\r\n$2")+"\r\n");
  1754. }
  1755. });
  1756. content+="\r\n";
  1757. });
  1758. return content;
  1759. }else return eval(evalCode);
  1760. }else{
  1761. return Function("data", "doc", "cb", "url", evalCode)(data, doc, cb, url);
  1762. }
  1763. };
  1764. }else{
  1765. if(win.dacProcess){
  1766. processFunc=win.dacProcess;
  1767. }
  1768. }
  1769. filterList(processEles);
  1770. }
  1771. }
  1772. const configPage = "https://hoothin.github.io/UserScripts/DownloadAllContent/";
  1773. const copySvg = '<svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" style="transition: all ease 0.5s;top: 5px;right: 5px;position: absolute;cursor: pointer;"><title>Copy</title><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg>';
  1774. function searchRule(){
  1775. GM_openInTab(configPage + "#@" + location.hostname, {active: true});
  1776. }
  1777. var downloadShortcut = GM_getValue("downloadShortcut") || {ctrlKey: true, shiftKey: false, altKey: false, metaKey: false, key: 'F9'};
  1778. var downloadSingleShortcut = GM_getValue("downloadSingleShortcut") || {ctrlKey: true, shiftKey: true, altKey: false, metaKey: false, key: 'F9'};
  1779. var downloadCustomShortcut = GM_getValue("downloadCustomShortcut") || {ctrlKey: true, shiftKey: false, altKey: true, metaKey: false, key: 'F9'};
  1780.  
  1781. if (location.origin + location.pathname == configPage) {
  1782. let exampleNode = document.getElementById("example");
  1783. if (!exampleNode) return;
  1784.  
  1785. exampleNode = exampleNode.parentNode;
  1786. let ruleList = exampleNode.nextElementSibling.nextElementSibling;
  1787. let searchInput = document.createElement("input");
  1788. let inputTimer;
  1789. function searchByInput() {
  1790. clearTimeout(inputTimer);
  1791. inputTimer = setTimeout(() => {
  1792. let curValue = searchInput.value;
  1793. let matchRules = [];
  1794. let dontMatchRules = [];
  1795. if (curValue) {
  1796. for (let i = 0; i < ruleList.children.length; i++) {
  1797. let curRule = ruleList.children[i];
  1798. let aHref = curRule.firstChild.href;
  1799. if (aHref.indexOf(curValue) == -1) {
  1800. dontMatchRules.push(curRule);
  1801. } else {
  1802. matchRules.push(curRule);
  1803. }
  1804. }
  1805. } else {
  1806. dontMatchRules = ruleList.children;
  1807. }
  1808. if (matchRules.length) {
  1809. for (let i = 0; i < dontMatchRules.length; i++) {
  1810. let curRule = dontMatchRules[i];
  1811. curRule.style.display = "none";
  1812. }
  1813. for (let i = 0; i < matchRules.length; i++) {
  1814. let curRule = matchRules[i];
  1815. curRule.style.display = "";
  1816. }
  1817. } else {
  1818. for (let i = 0; i < dontMatchRules.length; i++) {
  1819. let curRule = dontMatchRules[i];
  1820. curRule.style.display = "";
  1821. }
  1822. }
  1823. }, 500);
  1824. }
  1825. searchInput.style.margin = "10px";
  1826. searchInput.style.width = "100%";
  1827. searchInput.placeholder = i18n.searchRule;
  1828. searchInput.addEventListener("input", function(e) {
  1829. searchByInput();
  1830. });
  1831. if (location.hash) {
  1832. let hash = location.hash.slice(1);
  1833. if (hash.indexOf("@") == 0) {
  1834. setTimeout(() => {
  1835. exampleNode.scrollIntoView();
  1836. }, 500);
  1837. searchInput.value = hash.slice(1);
  1838. searchByInput();
  1839. }
  1840. }
  1841. [].forEach.call(ruleList.querySelectorAll("div.highlight"), highlight => {
  1842. highlight.style.position = "relative";
  1843. highlight.innerHTML = highlight.innerHTML + copySvg;
  1844. let svg = highlight.children[1];
  1845. svg.addEventListener("click", function(e) {
  1846. GM_setClipboard(highlight.children[0].innerText);
  1847. svg.style.opacity = 0;
  1848. setTimeout(() => {
  1849. svg.style.opacity = 1;
  1850. }, 1000);
  1851. });
  1852. });
  1853. exampleNode.parentNode.insertBefore(searchInput, ruleList);
  1854.  
  1855.  
  1856. let donateNode = document.querySelector("[alt='donate']");
  1857. if (!donateNode) return;
  1858. let insertPos = donateNode.parentNode.nextElementSibling;
  1859. let radioIndex = 0;
  1860. function createOption(_name, _value, _type) {
  1861. if (!_type) _type = "input";
  1862. let con = document.createElement("div");
  1863. let option = document.createElement("input");
  1864. let cap = document.createElement("b");
  1865. option.type = _type;
  1866. option.value = _value;
  1867. option.checked = _value;
  1868. cap.style.margin = "0px 10px 0px 0px";
  1869. if (_type == "radio") {
  1870. let label = document.createElement("label");
  1871. label.innerText = _name;
  1872. radioIndex++;
  1873. option.id = "radio" + radioIndex;
  1874. label.setAttribute("for", option.id);
  1875. cap.appendChild(label);
  1876. } else {
  1877. if (_type == "input") {
  1878. option.style.flexGrow = "1";
  1879. }
  1880. cap.innerText = _name;
  1881. }
  1882. con.style.margin = "10px 0";
  1883. con.style.display = "flex";
  1884. con.style.alignItems = "center";
  1885. con.appendChild(cap);
  1886. con.appendChild(option);
  1887. insertPos.parentNode.insertBefore(con, insertPos);
  1888. return option;
  1889. }
  1890. function formatShortcut(e) {
  1891. let result = [];
  1892. if (e.ctrlKey) {
  1893. result.push("Ctrl");
  1894. }
  1895. if (e.shiftKey) {
  1896. result.push("Shift");
  1897. }
  1898. if (e.altKey) {
  1899. result.push("Alt");
  1900. }
  1901. if (e.metaKey) {
  1902. result.push("Meta");
  1903. }
  1904. result.push(e.key);
  1905. return result.join(" + ");
  1906. }
  1907. function geneShortcutData(str) {
  1908. if (!str) return "";
  1909. let result = {ctrlKey: false, shiftKey: false, altKey: false, metaKey: false, key: ''};
  1910. str.split(" + ").forEach(item => {
  1911. switch(item) {
  1912. case "Ctrl":
  1913. result.ctrlKey = true;
  1914. break;
  1915. case "Shift":
  1916. result.shiftKey = true;
  1917. break;
  1918. case "Alt":
  1919. result.altKey = true;
  1920. break;
  1921. case "Meta":
  1922. result.metaKey = true;
  1923. break;
  1924. default:
  1925. result.key = item;
  1926. break;
  1927. }
  1928. });
  1929. return result;
  1930. }
  1931. let showFilterList = createOption(i18n.showFilterList, !!GM_getValue("showFilterList"), "checkbox");
  1932. let downloadShortcutInput = createOption(i18n.downloadShortcut, formatShortcut(downloadShortcut) || "");
  1933. let downloadSingleShortcutInput = createOption(i18n.downloadSingleShortcut, formatShortcut(downloadSingleShortcut) || "");
  1934. let downloadCustomShortcutInput = createOption(i18n.downloadCustomShortcut, formatShortcut(downloadCustomShortcut) || "");
  1935. downloadShortcutInput.setAttribute("readonly", "true");
  1936. downloadSingleShortcutInput.setAttribute("readonly", "true");
  1937. downloadCustomShortcutInput.setAttribute("readonly", "true");
  1938. let keydonwHandler = e => {
  1939. if (e.key) {
  1940. if (e.key == "Backspace") {
  1941. e.target.value = "";
  1942. } else if (e.key != "Control" && e.key != "Shift" && e.key != "Alt" && e.key != "Meta") {
  1943. e.target.value = formatShortcut(e);
  1944. }
  1945. }
  1946. e.preventDefault();
  1947. e.stopPropagation();
  1948. };
  1949. downloadShortcutInput.addEventListener("keydown", keydonwHandler);
  1950. downloadSingleShortcutInput.addEventListener("keydown", keydonwHandler);
  1951. downloadCustomShortcutInput.addEventListener("keydown", keydonwHandler);
  1952.  
  1953. let delSelector = createOption(i18n.del, GM_getValue("selectors") || "");
  1954. delSelector.setAttribute("placeHolder", ".mask,.ksam");
  1955. let downThreadNum = createOption(i18n.downThreadNum, GM_getValue("downThreadNum") || "20", "number");
  1956. let customTitle = createOption(i18n.customTitle, GM_getValue("customTitle") || "");
  1957. customTitle.setAttribute("placeHolder", "title");
  1958. let minTxtLength = createOption(i18n.minTxtLength, GM_getValue("minTxtLength") || "100", "number");
  1959. let contentSortUrlValue = GM_getValue("contentSortUrl") || false;
  1960. let contentSortValue = GM_getValue("contentSort") || false;
  1961. let reSortDefault = createOption(i18n.reSortDefault, !contentSortUrlValue && !contentSortValue, "radio");
  1962. let reSortUrl = createOption(i18n.reSortUrl, contentSortUrlValue || false, "radio");
  1963. let contentSort = createOption(i18n.reSort, contentSortValue || false, "radio");
  1964. reSortDefault.name = "sort";
  1965. reSortUrl.name = "sort";
  1966. contentSort.name = "sort";
  1967. let reverse = createOption(i18n.reverseOrder, !!GM_getValue("reverse"), "checkbox");
  1968. let retainImage = createOption(i18n.retainImage, !!GM_getValue("retainImage"), "checkbox");
  1969. let disableNextPage = !!GM_getValue("disableNextPage");
  1970. let nextPage = createOption(i18n.nextPage, !disableNextPage, "checkbox");
  1971. let nextPageReg = createOption(i18n.nextPageReg, GM_getValue("nextPageReg") || "");
  1972. nextPageReg.setAttribute("placeHolder", "^\\s*(下一[页頁张張]|next\\s*page|次のページ)");
  1973. if (disableNextPage) {
  1974. nextPageReg.parentNode.style.display = "none";
  1975. }
  1976. nextPage.onclick = e => {
  1977. nextPageReg.parentNode.style.display = nextPage.checked ? "flex" : "none";
  1978. }
  1979. let saveBtn = document.createElement("button");
  1980. saveBtn.innerText = i18n.saveBtn;
  1981. saveBtn.style.margin = "0 0 20px 0";
  1982. insertPos.parentNode.insertBefore(saveBtn, insertPos);
  1983. saveBtn.onclick = e => {
  1984. GM_setValue("selectors", delSelector.value || "");
  1985. GM_setValue("downThreadNum", downThreadNum.value || 20);
  1986. GM_setValue("minTxtLength", minTxtLength.value || 100);
  1987. GM_setValue("customTitle", customTitle.value || "");
  1988. if (reSortUrl.checked) {
  1989. GM_setValue("contentSortUrl", true);
  1990. GM_setValue("contentSort", false);
  1991. } else if (contentSort.checked) {
  1992. GM_setValue("contentSortUrl", false);
  1993. GM_setValue("contentSort", true);
  1994. } else {
  1995. GM_setValue("contentSortUrl", false);
  1996. GM_setValue("contentSort", false);
  1997. }
  1998. GM_setValue("reverse", reverse.checked);
  1999. GM_setValue("retainImage", retainImage.checked);
  2000. GM_setValue("showFilterList", showFilterList.checked);
  2001. GM_setValue("disableNextPage", !nextPage.checked);
  2002. GM_setValue("nextPageReg", nextPageReg.value || "");
  2003. GM_setValue("downloadShortcut", geneShortcutData(downloadShortcutInput.value) || "");
  2004. GM_setValue("downloadSingleShortcut", geneShortcutData(downloadSingleShortcutInput.value) || "");
  2005. GM_setValue("downloadCustomShortcut", geneShortcutData(downloadCustomShortcutInput.value) || "");
  2006. alert(i18n.saveOk);
  2007. };
  2008. return;
  2009. }
  2010.  
  2011. function setDel(){
  2012. GM_openInTab(configPage + "#操作說明", {active: true});
  2013. /*var selValue=GM_getValue("selectors");
  2014. var selectors=prompt(i18n.del,selValue?selValue:"");
  2015. GM_setValue("selectors",selectors);
  2016. selValue=GM_getValue("downThreadNum");
  2017. var downThreadNum=prompt(i18n.downThreadNum,selValue?selValue:"20");
  2018. GM_setValue("downThreadNum",downThreadNum);
  2019. var sortByUrl=window.confirm(i18n.reSortUrl);
  2020. GM_setValue("contentSortUrl",sortByUrl);
  2021. if(!sortByUrl)GM_setValue("contentSort",window.confirm(i18n.reSort));*/
  2022. }
  2023.  
  2024. function checkKey(shortcut1, shortcut2) {
  2025. return shortcut1.ctrlKey == shortcut2.ctrlKey && shortcut1.shiftKey == shortcut2.shiftKey && shortcut1.altKey == shortcut2.altKey && shortcut1.metaKey == shortcut2.metaKey && shortcut1.key == shortcut2.key;
  2026. }
  2027.  
  2028. function startCustom() {
  2029. var customRules = GM_getValue("DACrules_" + document.domain);
  2030. var urls = window.prompt(i18n.customInfo, customRules ? customRules : "https://xxx.xxx/book-[20-99].html, https://xxx.xxx/book-[01-10].html");
  2031. if (urls) {
  2032. customDown(urls);
  2033. }
  2034. }
  2035.  
  2036. document.addEventListener("keydown", function(e) {
  2037. if (checkKey(downloadCustomShortcut, e)) {
  2038. startCustom();
  2039. } else if (checkKey(downloadSingleShortcut, e)) {
  2040. fetch(true);
  2041. } else if (checkKey(downloadShortcut, e)) {
  2042. fetch(false);
  2043. }
  2044. });
  2045. GM_registerMenuCommand(i18n.custom, () => {
  2046. startCustom();
  2047. });
  2048. GM_registerMenuCommand(i18n.fetch, fetch);
  2049. GM_registerMenuCommand(i18n.setting, setDel);
  2050. GM_registerMenuCommand(i18n.searchRule, searchRule);
  2051. })();