怠惰小说下载器

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

安装此脚本
作者推荐脚本

您可能也喜欢东方永页机

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