怠惰小说下载器

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

目前为 2024-07-01 提交的版本。查看 最新版本

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