怠惰小説下載器

通用網站內容爬蟲抓取工具,可批量抓取任意站點的小說、論壇內容等並保存為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.17
  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. function indexDownload(aEles, noSort){
  981. if(aEles.length<1)return;
  982. initTxtDownDiv();
  983. if(!noSort) {
  984. if(GM_getValue("contentSort")){
  985. aEles.sort((a, b) => {
  986. return str2Num(a.innerText) - str2Num(b.innerText);
  987. });
  988. }
  989. if(GM_getValue("contentSortUrl")){
  990. aEles.sort((a, b) => {
  991. const nameA = a.href.toUpperCase();
  992. const nameB = b.href.toUpperCase();
  993. if (nameA < nameB) {
  994. return -1;
  995. }
  996. if (nameA > nameB) {
  997. return 1;
  998. }
  999. return 0;
  1000. });
  1001. }
  1002. if(GM_getValue("reverse")){
  1003. aEles=aEles.reverse();
  1004. }
  1005. }
  1006. rCats=[];
  1007. const minute=60000;
  1008. var minTxtLength=GM_getValue("minTxtLength") || 100;
  1009. var customTitle=GM_getValue("customTitle");
  1010. var prefix=GM_getValue("prefix");
  1011. var disableNextPage=!!GM_getValue("disableNextPage");
  1012. var customNextPageReg=GM_getValue("nextPageReg");
  1013. var maxDlPerMin=GM_getValue("maxDlPerMin") || 0;
  1014. var dlCount=0;
  1015. if (customNextPageReg) {
  1016. try {
  1017. innerNextPage = new RegExp(customNextPageReg);
  1018. } catch(e) {
  1019. console.warn(e);
  1020. }
  1021. }
  1022. function packLink(doc, item, curIndex) {
  1023. if (customTitle) {
  1024. try {
  1025. let title = doc.querySelector(customTitle);
  1026. if (title && title.innerText) {
  1027. item.innerText = title.innerText;
  1028. }
  1029. } catch(e) {
  1030. console.warn(e);
  1031. }
  1032. }
  1033. if (prefix) {
  1034. item.innerText = prefix.replace(/\$i/g, ++curIndex) + item.innerText;
  1035. }
  1036. }
  1037. var insertSigns=[];
  1038. // var j=0,rCats=[];
  1039. var downIndex=0,downNum=0,downOnce=function(wait){
  1040. if(downNum>=aEles.length)return;
  1041. if(maxDlPerMin){
  1042. if(dlCount===-1){
  1043. setTimeout(() => {
  1044. downOnce(wait);
  1045. }, minute);
  1046. return;
  1047. }else if(dlCount>=maxDlPerMin){
  1048. dlCount=-1;
  1049. setTimeout(() => {
  1050. dlCount=0;
  1051. downOnce(wait);
  1052. }, minute);
  1053. return;
  1054. }else dlCount++;
  1055. }
  1056. let curIndex=downIndex;
  1057. let aTag=aEles[curIndex];
  1058. let request=(aTag, curIndex)=>{
  1059. if (aTag && aTag.cloneNode) {
  1060. aTag = aTag.cloneNode(true);
  1061. }
  1062. let tryTimes=0;
  1063. let validTimes=0;
  1064. function requestDoc(_charset) {
  1065. if (!_charset) _charset = charset;
  1066. return GM_xmlhttpRequest({
  1067. method: 'GET',
  1068. url: aTag.href,
  1069. headers:{
  1070. referer:aTag.href,
  1071. "Content-Type":"text/html;charset="+_charset
  1072. },
  1073. timeout:10000,
  1074. overrideMimeType:"text/html;charset="+_charset,
  1075. onload: async function(result) {
  1076. let doc = getDocEle(result.responseText);
  1077. if (charsetValid) {
  1078. let equiv = doc.querySelector('[http-equiv="Content-Type"]');
  1079. if (equiv && equiv.content) {
  1080. let innerCharSet = equiv.content.match(/charset\=([^;]+)/);
  1081. if (innerCharSet && innerCharSet[1].replace("-", "").toLowerCase() != _charset.replace("-", "").toLowerCase()) {
  1082. charset = innerCharSet[1];
  1083. return requestDoc(charset);
  1084. }
  1085. }
  1086. }
  1087. downIndex++;
  1088. downNum++;
  1089. if (/^{/.test(result.responseText)) {
  1090. doc.json = () => {
  1091. try {
  1092. return JSON.parse(result.responseText);
  1093. } catch(e) {}
  1094. return {};
  1095. }
  1096. }
  1097. let base = doc.querySelector("base");
  1098. let nextPages = !disableNextPage && !processFunc && await checkNextPage(doc, base ? base.href : aTag.href);
  1099. if (nextPages) {
  1100. if (!nextPages.length) nextPages = [nextPages];
  1101. nextPages.forEach(nextPage => {
  1102. var inArr=false;
  1103. for(var ai=0;ai<aEles.length;ai++){
  1104. if(aEles[ai].href==nextPage.href){
  1105. inArr=true;
  1106. break;
  1107. }
  1108. }
  1109. if(!inArr){
  1110. nextPage.innerText=aTag.innerText+"\t>>";
  1111. aEles.push(nextPage);
  1112. let targetIndex = curIndex;
  1113. for(let a=0;a<insertSigns.length;a++){
  1114. let signs=insertSigns[a],breakSign=false;
  1115. if(signs){
  1116. for(let b=0;b<signs.length;b++){
  1117. let sign=signs[b];
  1118. if(sign==curIndex){
  1119. targetIndex=a;
  1120. breakSign=true;
  1121. break;
  1122. }
  1123. }
  1124. }
  1125. if(breakSign)break;
  1126. }
  1127. let insertSign = insertSigns[targetIndex];
  1128. if(!insertSign)insertSigns[targetIndex] = [];
  1129. insertSigns[targetIndex].push(aEles.length-1);
  1130. }
  1131. });
  1132. }
  1133. if (result.status >= 400) {
  1134. console.warn("error:", `status: ${result.status} from: ${aTag.href}`);
  1135. } else {
  1136. console.log(result.status);
  1137. }
  1138. let validData = processDoc(curIndex, aTag, doc, (result.status>=400?` status: ${result.status} from: ${aTag.href} `:""), validTimes < 5);
  1139. if (!validData && validTimes++ < 5) {
  1140. downIndex--;
  1141. downNum--;
  1142. setTimeout(() => {
  1143. requestDoc();
  1144. }, Math.random() * 500 + validTimes * 1000);
  1145. return;
  1146. }
  1147. if (wait) {
  1148. setTimeout(() => {
  1149. downOnce(wait);
  1150. }, wait);
  1151. } else downOnce();
  1152. },
  1153. onerror: function(e) {
  1154. console.warn("error:", e, aTag.href);
  1155. if(tryTimes++ < 5){
  1156. setTimeout(() => {
  1157. requestDoc();
  1158. }, Math.random() * 500 + tryTimes * 1000);
  1159. return;
  1160. }
  1161. downIndex++;
  1162. downNum++;
  1163. processDoc(curIndex, aTag, null, ` NETWORK ERROR: ${(e.response||e.responseText)} from: ${aTag.href} `);
  1164. if (wait) {
  1165. setTimeout(() => {
  1166. downOnce(wait);
  1167. }, wait);
  1168. } else downOnce();
  1169. },
  1170. ontimeout: function(e) {
  1171. console.warn("timeout: times="+(tryTimes+1)+" url="+aTag.href);
  1172. //console.log(e);
  1173. if(tryTimes++ < 5){
  1174. setTimeout(() => {
  1175. requestDoc();
  1176. }, Math.random() * 500 + tryTimes * 1000);
  1177. return;
  1178. }
  1179. downIndex++;
  1180. downNum++;
  1181. processDoc(curIndex, aTag, null, ` TIMEOUT: ${aTag.href} `);
  1182. if (wait) {
  1183. setTimeout(() => {
  1184. downOnce(wait);
  1185. }, wait);
  1186. } else downOnce();
  1187. }
  1188. });
  1189. };
  1190. if (useIframe) {
  1191. let iframe = document.createElement('iframe'), inited = false, failedTimes = 0;
  1192. let loadtimeout;
  1193. let loadIframe = src => {
  1194. iframe.src = src;
  1195. clearTimeout(loadtimeout);
  1196. loadtimeout = setTimeout(() => {
  1197. iframe.src = src;
  1198. }, 20000);
  1199. };
  1200. iframe.name = 'pagetual-iframe';
  1201. iframe.width = '100%';
  1202. iframe.height = '1000';
  1203. iframe.frameBorder = '0';
  1204. iframe.sandbox = iframeSandbox || "allow-same-origin allow-scripts allow-popups allow-forms";
  1205. 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;';
  1206. iframe.addEventListener('load', e => {
  1207. if (e.data != 'pagetual-iframe:DOMLoaded' && e.type != 'load') return;
  1208. if (inited) return;
  1209. inited = true;
  1210. clearTimeout(loadtimeout);
  1211. async function checkIframe() {
  1212. try {
  1213. let doc = iframe.contentDocument || iframe.contentWindow.document;
  1214. if (!doc || !doc.body) {
  1215. setTimeout(() => {
  1216. checkIframe();
  1217. }, 1000);
  1218. return;
  1219. }
  1220. doc.body.scrollTop = 9999999;
  1221. doc.documentElement.scrollTop = 9999999;
  1222. if (!processFunc && validTimes++ > 5 && failedTimes++ < 2) {
  1223. loadIframe(iframe.src);
  1224. validTimes = 0;
  1225. inited = false;
  1226. return;
  1227. }
  1228. let base = doc.querySelector("base");
  1229. let nextPages = !disableNextPage && !processFunc && await checkNextPage(doc, base ? base.href : aTag.href);
  1230. if (nextPages) {
  1231. if (!nextPages.length) nextPages = [nextPages];
  1232. nextPages.forEach(nextPage => {
  1233. var inArr=false;
  1234. for(var ai=0;ai<aEles.length;ai++){
  1235. if(aEles[ai].href==nextPage.href){
  1236. inArr=true;
  1237. break;
  1238. }
  1239. }
  1240. if(!inArr){
  1241. nextPage.innerText=aTag.innerText+"\t>>";
  1242. aEles.push(nextPage);
  1243. let targetIndex = curIndex;
  1244. for(let a=0;a<insertSigns.length;a++){
  1245. let signs=insertSigns[a],breakSign=false;
  1246. if(signs){
  1247. for(let b=0;b<signs.length;b++){
  1248. let sign=signs[b];
  1249. if(sign==curIndex){
  1250. targetIndex=a;
  1251. breakSign=true;
  1252. break;
  1253. }
  1254. }
  1255. }
  1256. if(breakSign)break;
  1257. }
  1258. let insertSign = insertSigns[targetIndex];
  1259. if(!insertSign)insertSigns[targetIndex] = [];
  1260. insertSigns[targetIndex].push(aEles.length-1);
  1261. }
  1262. });
  1263. }
  1264. downIndex++;
  1265. downNum++;
  1266. let validData = processDoc(curIndex, aTag, doc, "", failedTimes < 2);
  1267. if (!validData) {
  1268. downIndex--;
  1269. downNum--;
  1270. setTimeout(() => {
  1271. checkIframe();
  1272. }, 1000);
  1273. return;
  1274. }
  1275. if (wait) {
  1276. setTimeout(() => {
  1277. downOnce(wait);
  1278. }, wait);
  1279. } else downOnce();
  1280. } catch(e) {
  1281. console.debug("Stop as cors");
  1282. }
  1283. if (iframe && iframe.parentNode) iframe.parentNode.removeChild(iframe);
  1284. }
  1285. setTimeout(() => {
  1286. checkIframe();
  1287. }, 500);
  1288. }, false);
  1289. let checkReady = setInterval(() => {
  1290. let doc;
  1291. try {
  1292. doc = iframe.contentDocument || (iframe.contentWindow && iframe.contentWindow.document);
  1293. } catch(e) {
  1294. clearInterval(checkReady);
  1295. return;
  1296. }
  1297. if (doc) {
  1298. try {
  1299. Function('win', 'iframe', '"use strict";' + (iframeInit || "win.self=win.top;"))(iframe.contentWindow, iframe);
  1300. clearInterval(checkReady);
  1301. } catch(e) {
  1302. console.debug(e);
  1303. }
  1304. }
  1305. }, 50);
  1306. loadIframe(aTag.href);
  1307. document.body.appendChild(iframe);
  1308. return [curIndex, null, aTag.href];
  1309. } else {
  1310. return [curIndex, requestDoc(), aTag.href];
  1311. }
  1312. }
  1313. if(!aTag){
  1314. let waitAtagReadyInterval=setInterval(function(){
  1315. if(downNum>=aEles.length)clearInterval(waitAtagReadyInterval);
  1316. aTag=aEles[curIndex];
  1317. if(aTag){
  1318. clearInterval(waitAtagReadyInterval);
  1319. request(aTag, curIndex);
  1320. }
  1321. },1000);
  1322. return null;
  1323. }
  1324. let result = request(aTag, curIndex);
  1325. if (result) curRequests.push(result);
  1326. return result;
  1327. };
  1328. function getDocEle(str){
  1329. var doc = null;
  1330. try {
  1331. doc = document.implementation.createHTMLDocument('');
  1332. doc.documentElement.innerHTML = str;
  1333. }
  1334. catch (e) {
  1335. console.log('parse error');
  1336. }
  1337. return doc;
  1338. }
  1339. function sortInnerPage(){
  1340. var pageArrs=[],maxIndex=0,i,j;
  1341. for(i=0;i<insertSigns.length;i++){
  1342. var signs=insertSigns[i];
  1343. if(signs){
  1344. for(j=0;j<signs.length;j++){
  1345. var sign=signs[j];
  1346. var cat=rCats[sign];
  1347. rCats[sign]=null;
  1348. if(!pageArrs[i])pageArrs[i]=[];
  1349. pageArrs[i].push(cat);
  1350. }
  1351. }
  1352. }
  1353. for(i=pageArrs.length-1;i>=0;i--){
  1354. let pageArr=pageArrs[i];
  1355. if(pageArr){
  1356. for(j=pageArr.length-1;j>=0;j--){
  1357. rCats.splice(i+1, 0, pageArr[j]);
  1358. }
  1359. }
  1360. }
  1361. rCats = rCats.filter(function(e){return e!=null});
  1362. }
  1363. var waitForComplete;
  1364. function processDoc(i, aTag, doc, cause, check){
  1365. let cbFunc=content=>{
  1366. packLink(doc, aTag, i);
  1367. let isHref = "";
  1368. let saveUrl = GM_getValue("saveUrl");
  1369. if (saveUrl){
  1370. isHref = aTag.href + '\r\n';
  1371. }
  1372. rCats[i]=(aTag.innerText.replace(/[\r\n\t]/g, "") + "\r\n" + isHref + (cause || '') + content.replace(/\s*$/, ""));
  1373. curRequests = curRequests.filter(function(e){return e[0]!=i});
  1374. txtDownContent.style.display="block";
  1375. txtDownWords.innerHTML=getI18n("downloading",[downNum,(aEles.length-downNum),aTag.innerText]);
  1376. if(downNum==aEles.length){
  1377. if(waitForComplete) clearTimeout(waitForComplete);
  1378. waitForComplete=setTimeout(()=>{
  1379. if(downNum==aEles.length){
  1380. txtDownWords.innerHTML=getI18n("complete",[downNum]);
  1381. sortInnerPage();
  1382. var disableAutoStartSave = GM_getValue("disableAutoStartSave");
  1383. if(!disableAutoStartSave){
  1384. saveContent();
  1385. }
  1386. }
  1387. },3000);
  1388. }
  1389. };
  1390. let contentResult=getPageContent(doc, content=>{
  1391. cbFunc(content);
  1392. }, aTag.href);
  1393. if(contentResult!==false){
  1394. if(check && contentResult && contentResult.replace(/\s/g, "").length<minTxtLength){
  1395. return false;
  1396. }
  1397. cbFunc(contentResult);
  1398. }
  1399. return true;
  1400. }
  1401. var downThreadNum = parseInt(GM_getValue("downThreadNum"));
  1402. downThreadNum = downThreadNum || 20;
  1403. if (useIframe && downThreadNum > 5) {
  1404. downThreadNum = 5;
  1405. }
  1406. if (downThreadNum > 0) {
  1407. for (var i = 0; i < downThreadNum; i++) {
  1408. downOnce();
  1409. if (downIndex >= aEles.length - 1 || downIndex >= downThreadNum - 1) break;
  1410. else downIndex++;
  1411. }
  1412. } else {
  1413. downOnce(-downThreadNum * 1000);
  1414. }
  1415. }
  1416.  
  1417. function canonicalUri(src, baseUrl) {
  1418. if (!src) {
  1419. return "";
  1420. }
  1421. if (src.charAt(0) == "#") return baseUrl + src;
  1422. if (src.charAt(0) == "?") return baseUrl.replace(/^([^\?#]+).*/, "$1" + src);
  1423. let origin = location.protocol + '//' + location.host;
  1424. let url = baseUrl || origin;
  1425. url = url.replace(/(\?|#).*/, "");
  1426. if (/https?:\/\/[^\/]+$/.test(url)) url = url + '/';
  1427. if (url.indexOf("http") !== 0) url = origin + url;
  1428. var root_page = /^[^\?#]*\//.exec(url)[0],
  1429. root_domain = /^\w+\:\/\/\/?[^\/]+/.exec(root_page)[0],
  1430. absolute_regex = /^\w+\:\/\//;
  1431. while (src.indexOf("../") === 0) {
  1432. src = src.substr(3);
  1433. root_page = root_page.replace(/\/[^\/]+\/$/, "/");
  1434. }
  1435. src = src.replace(/\.\//, "");
  1436. if (/^\/\/\/?/.test(src)) {
  1437. src = location.protocol + src;
  1438. }
  1439. return (absolute_regex.test(src) ? src : ((src.charAt(0) == "/" ? root_domain : root_page) + src));
  1440. }
  1441.  
  1442. async function checkNextPage(doc, baseUrl) {
  1443. let nextPage = null;
  1444. if (nextPageFunc) {
  1445. nextPage = await nextPageFunc(doc, baseUrl);
  1446. if (nextPage && nextPage.length === 0) nextPage = null;
  1447. } else {
  1448. let aTags = doc.querySelectorAll("a");
  1449. for (var i = 0; i < aTags.length; i++) {
  1450. let aTag = aTags[i];
  1451. if (innerNextPage.test(aTag.innerText) && aTag.href && !/javascript:|#/.test(aTag.href)) {
  1452. let nextPageHref = canonicalUri(aTag.getAttribute("href"), baseUrl || location.href);
  1453. if (nextPageHref != location.href) {
  1454. nextPage = aTag;
  1455. nextPage.href = nextPageHref;
  1456. break;
  1457. }
  1458. }
  1459. }
  1460. }
  1461. return nextPage;
  1462. }
  1463.  
  1464. function textNodesUnder(el){
  1465. var n, a=[], walk=document.createTreeWalker(el,NodeFilter.SHOW_TEXT,null,false);
  1466. while(n=walk.nextNode()) a.push(n);
  1467. return a;
  1468. }
  1469.  
  1470. function getPageContent(doc, cb, url){
  1471. if(!doc)return i18n.error;
  1472. if(doc.body && !doc.body.children.length)return doc.body.innerText;
  1473. if(processFunc){
  1474. return processFunc(doc, cb, url);
  1475. }
  1476. [].forEach.call(doc.querySelectorAll("span,div,ul"),function(item){
  1477. var thisStyle=doc.defaultView?doc.defaultView.getComputedStyle(item):item.style;
  1478. if(thisStyle && (thisStyle.display=="none" || (item.nodeName=="SPAN" && thisStyle.fontSize=="0px"))){
  1479. item.innerHTML="";
  1480. }
  1481. });
  1482. var i,j,k,rStr="",pageData=(doc.body?doc.body:doc).cloneNode(true);
  1483. pageData.innerHTML=pageData.innerHTML.replace(/\<\!\-\-((.|[\n|\r|\r\n])*?)\-\-\>/g,"");
  1484. [].forEach.call(pageData.querySelectorAll("font.jammer"),function(item){
  1485. item.innerHTML="";
  1486. });
  1487. var selectors=GM_getValue("selectors");
  1488. if(selectors){
  1489. [].forEach.call(pageData.querySelectorAll(selectors),function(item){
  1490. item.innerHTML="";
  1491. });
  1492. }
  1493. [].forEach.call(pageData.querySelectorAll("script,style,link,noscript,iframe"),function(item){
  1494. if (item && item.parentNode) {
  1495. item.parentNode.removeChild(item);
  1496. }
  1497. });
  1498. var endEle = ele => {
  1499. return /^(I|STRONG|B|FONT|P|DL|DD|H\d)$/.test(ele.nodeName) && ele.children.length <= 1;
  1500. };
  1501. var largestContent,contents=pageData.querySelectorAll("span,div,article,p,td,pre"),largestNum=0;
  1502. for(i=0;i<contents.length;i++){
  1503. let content=contents[i],hasText=false,allSingle=true,item,curNum=0;
  1504. if(/footer/.test(content.className))continue;
  1505. for(j=content.childNodes.length-1;j>=0;j--){
  1506. item=content.childNodes[j];
  1507. if(item.nodeType==3){
  1508. if(/^\s*$/.test(item.data)){
  1509. item.innerHTML="";
  1510. }else hasText=true;
  1511. }else if(/^(I|A|STRONG|B|FONT|P|DL|DD|H\d)$/.test(item.nodeName)){
  1512. hasText=true;
  1513. }else if(item.nodeType==1&&item.children.length==1&&/^(I|A|STRONG|B|FONT|P|DL|DD|H\d)$/.test(item.children[0].nodeName)){
  1514. hasText=true;
  1515. }
  1516. }
  1517. for(j=content.childNodes.length-1;j>=0;j--){
  1518. item=content.childNodes[j];
  1519. if(item.nodeType==1 && !/^(I|A|STRONG|B|FONT|BR)$/.test(item.nodeName) && /^[\s\-\_\?\>\|]*$/.test(item.innerHTML)){
  1520. item.innerHTML="";
  1521. }
  1522. }
  1523. if(content.childNodes.length>1){
  1524. let indexItem=0;
  1525. for(j=0;j<content.childNodes.length;j++){
  1526. item=content.childNodes[j];
  1527. if(item.nodeType==1){
  1528. if(item.innerText && item.innerText.length<50 && indexReg.test(item.innerText))indexItem++;
  1529. for(k=0;k<item.childNodes.length;k++){
  1530. var childNode=item.childNodes[k];
  1531. if(childNode.nodeType!=3 && !/^(I|A|STRONG|B|FONT|BR)$/.test(childNode.nodeName)){
  1532. allSingle=false;
  1533. break;
  1534. }
  1535. }
  1536. if(!allSingle)break;
  1537. }
  1538. }
  1539. if(indexItem>=5)continue;
  1540. }else{
  1541. allSingle=false;
  1542. }
  1543. if(!allSingle && !hasText){
  1544. continue;
  1545. }else {
  1546. if(pageData==document && content.offsetWidth<=0 && content.offsetHeight<=0){
  1547. continue;
  1548. }
  1549. [].forEach.call(content.childNodes,function(item){
  1550. if(item.nodeType==3)curNum+=item.data.trim().length;
  1551. 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);
  1552. });
  1553. }
  1554. if(curNum>largestNum){
  1555. largestNum=curNum;
  1556. largestContent=content;
  1557. }
  1558. }
  1559. if(!largestContent)return i18n.error+" : NO TEXT CONTENT";
  1560. var retainImage=!!GM_getValue("retainImage");
  1561. function getContentByLargest() {
  1562. var childlist=pageData.querySelectorAll(largestContent.nodeName);//+(largestContent.className?"."+largestContent.className.replace(/(^\s*)|(\s*$)/g, '').replace(/\s+/g, '.'):""));
  1563. function getRightStr(ele, noTextEnable){
  1564. [].forEach.call(ele.querySelectorAll("a[href]"), a => {
  1565. a.parentNode && a.parentNode.removeChild(a);
  1566. });
  1567. if(retainImage){
  1568. [].forEach.call(ele.querySelectorAll("img[src]"), img => {
  1569. let imgTxtNode=document.createTextNode(`![img](${canonicalUri(img.getAttribute("src"), url || location.href)})`);
  1570. img.parentNode.replaceChild(imgTxtNode, img);
  1571. });
  1572. }
  1573. let childNodes=ele.childNodes,cStr="\r\n",hasText=false;
  1574. for(let j=0;j<childNodes.length;j++){
  1575. let childNode=childNodes[j];
  1576. if(childNode.nodeType==3 && childNode.data && !/^[\s\-\_\?\>\|]*$/.test(childNode.data))hasText=true;
  1577. if(childNode.innerHTML){
  1578. childNode.innerHTML=childNode.innerHTML.replace(/\<\s*br\s*\>/gi,"\r\n").replace(/\n+/gi,"\n").replace(/\r+/gi,"\r");
  1579. }
  1580. let content=childNode.textContent;
  1581. if(content){
  1582. if(!content.trim())continue;
  1583. cStr+=content.replace(/[\uFEFF\xA0 ]+/g," ").replace(/([^\r]|^)\n([^\r]|$)/gi,"$1\r\n$2");
  1584. }
  1585. if(childNode.nodeType!=3 && !/^(I|A|STRONG|B|FONT|IMG)$/.test(childNode.nodeName))cStr+="\r\n";
  1586. else if(childNode.nextSibling && /^P$/.test(childNode.nextSibling.nodeName))cStr+="\r\n";
  1587. }
  1588. if(hasText || noTextEnable || ele==largestContent)rStr+=cStr+"\r\n";
  1589. }
  1590. var sameDepthChildren=[];
  1591. for(i=0;i<childlist.length;i++){
  1592. var child=childlist[i];
  1593. if(getDepth(child)==getDepth(largestContent)){
  1594. if(largestContent.className != child.className)continue;
  1595. sameDepthChildren.push(child);
  1596. }
  1597. }
  1598. var minLength = largestNum>>2;
  1599. var tooShort = sameDepthChildren.length <= 3;
  1600. sameDepthChildren.forEach(child => {
  1601. if(tooShort && child.innerText.length < minLength) return;
  1602. if((largestContent.className && largestContent.className == child.className) || largestContent.parentNode == child.parentNode){
  1603. getRightStr(child, true);
  1604. }else {
  1605. getRightStr(child, false);
  1606. }
  1607. });
  1608. rStr = rStr.replace(/[\n\r]+/g,"\n\r");
  1609. }
  1610. getContentByLargest();
  1611. if (rStr.length < 100) {
  1612. let articles = pageData.querySelectorAll("article,.content,#content");
  1613. if (articles && articles.length == 1) {
  1614. largestContent = articles[0];
  1615. largestNum = largestContent.innerText.length;
  1616. if (largestNum > 100) {
  1617. rStr = "";
  1618. getContentByLargest();
  1619. }
  1620. }
  1621. }
  1622. return rStr;
  1623. }
  1624.  
  1625. function getI18n(key, args){
  1626. var resultStr=i18n[key];
  1627. if(args && args.length>0){
  1628. args.forEach(function(item){
  1629. resultStr=resultStr.replace(/%s/,item);
  1630. });
  1631. }
  1632. return resultStr;
  1633. }
  1634.  
  1635. function getDepth(dom){
  1636. var pa=dom,i=0;
  1637. while(pa.parentNode){
  1638. pa=pa.parentNode;
  1639. i++;
  1640. }
  1641. return i;
  1642. }
  1643.  
  1644. async function sleep(time) {
  1645. await new Promise((resolve) => {
  1646. setTimeout(() => {
  1647. resolve();
  1648. }, time);
  1649. })
  1650. }
  1651.  
  1652. async function fetch(forceSingle){
  1653. forceSingle=forceSingle===true;
  1654. processFunc=null;
  1655. initTxtDownDiv();
  1656. var aEles=document.body.querySelectorAll("a"),list=[];
  1657. txtDownWords.innerHTML=`Analysing ( 1/${aEles.length} )......`;
  1658. txtDownContent.style.pointerEvents="none";
  1659. for(var i=0;i<aEles.length;i++){
  1660. if (i % 100 == 0) {
  1661. await sleep(1);
  1662. }
  1663. txtDownWords.innerHTML=`Analysing ( ${i + 1}/${aEles.length} )......`;
  1664. var aEle=aEles[i],has=false;
  1665. if(aEle.dataset.href && (!aEle.href || aEle.href.indexOf("javascript")!=-1)){
  1666. aEle.href=aEle.dataset.href;
  1667. }
  1668. if(aEle.href==location.href)continue;
  1669. for(var j=0;j<list.length;j++){
  1670. if(list[j].href==aEle.href){
  1671. aEle=list[j];
  1672. list.splice(j,1);
  1673. list.push(aEle);
  1674. has=true;
  1675. break;
  1676. }
  1677. }
  1678. if(!has && aEle.href && /^http/i.test(aEle.href) && ((aEle.innerText.trim()!="" && indexReg.test(aEle.innerText.trim())) || /chapter[\-_]?\d/.test(aEle.href))){
  1679. list.push(aEle);
  1680. }
  1681. }
  1682. txtDownContent.style.display="none";
  1683. txtDownContent.style.pointerEvents="";
  1684. txtDownWords.innerHTML="Analysing......";
  1685. if(list.length>2 && !forceSingle){
  1686. useIframe = false;
  1687. filterList(list);
  1688. }else{
  1689. 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"});
  1690. saveAs(blob, document.title+".txt");
  1691. }
  1692. }
  1693.  
  1694. function customDown(urls){
  1695. processFunc = null;
  1696. useIframe = false;
  1697. if(urls){
  1698. urls=decodeURIComponent(urls.replace(/%/g,'%25'));
  1699. GM_setValue("DACrules_"+document.domain, urls);
  1700. var processEles=[];
  1701. let urlsArr=urls.split("@@"),eles=[];
  1702. if(/^http|^ftp/.test(urlsArr[0])){
  1703. [].forEach.call(urlsArr[0].split(","),function(i){
  1704. var curEle;
  1705. var varNum=/\[\d+\-\d+\]/.exec(i);
  1706. if(varNum){
  1707. varNum=varNum[0].trim();
  1708. }else{
  1709. curEle=document.createElement("a");
  1710. curEle.href=i;
  1711. curEle.innerText="Added Url";
  1712. processEles.push(curEle);
  1713. return;
  1714. }
  1715. var num1=/\[(\d+)/.exec(varNum)[1].trim();
  1716. var num2=/(\d+)\]/.exec(varNum)[1].trim();
  1717. var num1Int=parseInt(num1);
  1718. var num2Int=parseInt(num2);
  1719. var numLen=num1.length;
  1720. var needAdd=num1.charAt(0)=="0";
  1721. if(num1Int>=num2Int)return;
  1722. for(var j=num1Int;j<=num2Int;j++){
  1723. var urlIndex=j.toString();
  1724. if(needAdd){
  1725. while(urlIndex.length<numLen)urlIndex="0"+urlIndex;
  1726. }
  1727. var curUrl=i.replace(/\[\d+\-\d+\]/,urlIndex).trim();
  1728. curEle=document.createElement("a");
  1729. curEle.href=curUrl;
  1730. curEle.innerText="Added Url " + processEles.length.toString();
  1731. processEles.push(curEle);
  1732. }
  1733. });
  1734. }else{
  1735. let urlSel=urlsArr[0].split(">>");
  1736. try{
  1737. eles=document.querySelectorAll(urlSel[0]);
  1738. eles=[].filter.call(eles, ele=>{
  1739. return ele.nodeName=='BODY'||(!!ele.offsetParent&&getComputedStyle(ele).display!=='none');
  1740. })
  1741. }catch(e){}
  1742. if(eles.length==0){
  1743. eles=[];
  1744. var eleTxts=urlsArr[0].split(/(?<=[^\\])[,,]/),exmpEles=[],excludeTxts={};
  1745. [].forEach.call(document.querySelectorAll("a"),function(item){
  1746. if(!item.offsetParent)return;
  1747. eleTxts.forEach(txt=>{
  1748. var txtArr=txt.split("!");
  1749. if(item.innerText.indexOf(txtArr[0])!=-1){
  1750. exmpEles.push(item);
  1751. excludeTxts[item]=txtArr.splice(1);
  1752. }
  1753. });
  1754. })
  1755. exmpEles.forEach(e=>{
  1756. var cssSelStr="a",pa=e.parentNode,excludeTxt=excludeTxts[e];
  1757. if(e.className)cssSelStr+="."+CSS.escape(e.className.replace(/\s+/g, ".")).replace(/\\\./g, '.');
  1758. while(pa && pa.nodeName!="BODY"){
  1759. cssSelStr=pa.nodeName+">"+cssSelStr;
  1760. pa=pa.parentNode;
  1761. }
  1762. cssSelStr="body>"+cssSelStr;;
  1763. [].forEach.call(document.querySelectorAll(cssSelStr),function(item){
  1764. if(!item.offsetParent)return;
  1765. var isExclude=false;
  1766. for(var t in excludeTxt){
  1767. if(item.innerText.indexOf(excludeTxt[t])!=-1){
  1768. isExclude=true;
  1769. break;
  1770. }
  1771. }
  1772. if(!isExclude && eles.indexOf(item)==-1){
  1773. eles.push(item);
  1774. }
  1775. });
  1776. });
  1777. }
  1778. function addItem(item) {
  1779. let has=false;
  1780. for(var j=0;j<processEles.length;j++){
  1781. if(processEles[j].href==item.href){
  1782. processEles.splice(j,1);
  1783. processEles.push(item);
  1784. has=true;
  1785. break;
  1786. }
  1787. }
  1788. if((!item.href || item.href.indexOf("javascript")!=-1) && item.dataset.href){
  1789. item.href=item.dataset.href;
  1790. }
  1791. if(!has && item.href && /^http/i.test(item.href)){
  1792. processEles.push(item.cloneNode(1));
  1793. }
  1794. }
  1795. [].forEach.call(eles,function(item){
  1796. if(urlSel[1]){
  1797. item=Function("item",urlSel[1])(item);
  1798. let items;
  1799. if (Array.isArray(item)) {
  1800. items = item;
  1801. } else items = [item];
  1802. items.forEach(item => {
  1803. if(!item || !item.href)return;
  1804. if(!item.nodeName || item.nodeName!="A"){
  1805. let href=item.href;
  1806. let innerText=item.innerText;
  1807. item=document.createElement("a");
  1808. item.href=href;
  1809. item.innerText=innerText;
  1810. }
  1811. addItem(item);
  1812. });
  1813. } else {
  1814. addItem(item);
  1815. }
  1816. });
  1817. }
  1818. if(urlsArr[1]){
  1819. processEles.forEach(ele=>{
  1820. ele.href=ele.href.replace(new RegExp(urlsArr[1]), urlsArr[2]);
  1821. });
  1822. }
  1823. var retainImage=!!GM_getValue("retainImage");
  1824. var evalCode = urlsArr[3];
  1825. if (evalCode) {
  1826. evalCode = evalCode.trim();
  1827. if (/^iframe:/.test(evalCode)) {
  1828. evalCode = evalCode.replace("iframe:", "");
  1829. useIframe = true;
  1830. iframeSandbox = false;
  1831. iframeInit = false;
  1832. while (/^(sandbox|init):/.test(evalCode)) {
  1833. iframeSandbox = evalCode.match(/^sandbox:\{(.*?)\}/);
  1834. if (iframeSandbox) {
  1835. evalCode = evalCode.replace(iframeSandbox[0], "");
  1836. iframeSandbox = iframeSandbox[1];
  1837. }
  1838. iframeInit = evalCode.match(/^init:\{(.*?)\}/);
  1839. if (iframeInit) {
  1840. evalCode = evalCode.replace(iframeInit[0], "");
  1841. iframeInit = iframeInit[1];
  1842. }
  1843. }
  1844. }
  1845. let charsetMatch = evalCode.match(/^charset:{(.+?)}/);
  1846. if (charsetMatch) {
  1847. charset = charsetMatch[1];
  1848. evalCode = evalCode.replace(charsetMatch[0], "");
  1849. }
  1850. let nextMatch = evalCode.match(/^next:(\{+)/);
  1851. if (nextMatch) {
  1852. let splitLen = nextMatch[1].length;
  1853. nextMatch = evalCode.match(new RegExp(`^next:\\{{${splitLen}}(.*?)\\}{${splitLen}}`));
  1854. if (nextMatch) {
  1855. let nextCode = nextMatch[1];
  1856. evalCode = evalCode.replace(nextMatch[0], "");
  1857. nextPageFunc = async (doc, url) => {
  1858. let result;
  1859. if (/\breturn\b/.test(nextCode)) {
  1860. result = await new AsyncFunction('doc', 'url', '"use strict";' + nextCode)(doc, url);
  1861. } else {
  1862. try {
  1863. result = doc.querySelectorAll(nextCode);
  1864. if (result && result.length) {
  1865. [].forEach.call(result, ele => {
  1866. ele.href = canonicalUri(ele.getAttribute("href"), url || location.href);
  1867. });
  1868. } else result = null;
  1869. } catch(e) {}
  1870. }
  1871. return result;
  1872. }
  1873. }
  1874. }
  1875. }
  1876. if(evalCode){
  1877. processFunc=(data, cb, url)=>{
  1878. let doc=data;
  1879. if(evalCode.indexOf("return ")==-1){
  1880. if(evalCode.indexOf("@")==0){
  1881. let content="";
  1882. var selectors=GM_getValue("selectors");
  1883. if(selectors){
  1884. [].forEach.call(data.querySelectorAll(selectors),function(item){
  1885. item.innerHTML="";
  1886. });
  1887. }
  1888. [].forEach.call(data.querySelectorAll("script,style,link,noscript,iframe"),function(item){
  1889. if (item && item.parentNode) {
  1890. item.parentNode.removeChild(item);
  1891. }
  1892. });
  1893. if(retainImage){
  1894. [].forEach.call(data.querySelectorAll("img[src]"), img => {
  1895. let imgTxt=`![img](${canonicalUri(img.getAttribute("src"), location.href)})`;
  1896. let imgTxtNode=document.createTextNode(imgTxt);
  1897. img.parentNode.replaceChild(imgTxtNode, img);
  1898. });
  1899. }
  1900. [].forEach.call(data.querySelectorAll(evalCode.slice(1)), ele=>{
  1901. [].forEach.call(ele.childNodes, child=>{
  1902. if(child.innerHTML){
  1903. child.innerHTML=child.innerHTML.replace(/\<\s*br\s*\>/gi,"\r\n").replace(/\n+/gi,"\n").replace(/\r+/gi,"\r");
  1904. }
  1905. if(child.textContent){
  1906. content+=(child.textContent.replace(/ +/g," ").replace(/([^\r]|^)\n([^\r]|$)/gi,"$1\r\n$2")+"\r\n");
  1907. }
  1908. });
  1909. content+="\r\n";
  1910. });
  1911. return content;
  1912. }else return eval(evalCode);
  1913. }else{
  1914. return Function("data", "doc", "cb", "url", evalCode)(data, doc, cb, url);
  1915. }
  1916. };
  1917. }else{
  1918. if(win.dacProcess){
  1919. processFunc=win.dacProcess;
  1920. }
  1921. }
  1922. filterList(processEles);
  1923. }
  1924. }
  1925. const configPage = "https://hoothin.github.io/UserScripts/DownloadAllContent/";
  1926. 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>';
  1927. function searchRule(){
  1928. GM_openInTab(configPage + "#@" + location.hostname, {active: true});
  1929. }
  1930. var downloadShortcut = GM_getValue("downloadShortcut") || {ctrlKey: true, shiftKey: false, altKey: false, metaKey: false, key: 'F9'};
  1931. var downloadSingleShortcut = GM_getValue("downloadSingleShortcut") || {ctrlKey: true, shiftKey: true, altKey: false, metaKey: false, key: 'F9'};
  1932. var downloadCustomShortcut = GM_getValue("downloadCustomShortcut") || {ctrlKey: true, shiftKey: false, altKey: true, metaKey: false, key: 'F9'};
  1933.  
  1934. var enableTouch = GM_getValue("enableTouch");
  1935. if (enableTouch) {
  1936. const minLength = 256, tg = 0.5, atg = 2;
  1937. var lastX, lastY, signs, lastSign;
  1938. function tracer(e) {
  1939. let curX = e.changedTouches[0].clientX, curY = e.changedTouches[0].clientY;
  1940. let distanceX = curX - lastX, distanceY = curY - lastY;
  1941. let distance = distanceX * distanceX + distanceY * distanceY;
  1942. if (distance > minLength) {
  1943. lastX = curX;
  1944. lastY = curY;
  1945. let direction = "";
  1946. let slope = Math.abs(distanceY / distanceX);
  1947. if (slope > atg) {
  1948. if (distanceY > 0) {
  1949. direction = "↓";
  1950. } else {
  1951. direction = "↑";
  1952. }
  1953. } else if (slope < tg) {
  1954. if (distanceX > 0) {
  1955. direction = "→";
  1956. } else {
  1957. direction = "←";
  1958. }
  1959. }
  1960. if (direction && lastSign != direction) {
  1961. signs += direction;
  1962. lastSign = direction;
  1963. }
  1964. }
  1965. }
  1966. document.addEventListener("touchstart", function(e) {
  1967. lastX = e.changedTouches[0].clientX;
  1968. lastY = e.changedTouches[0].clientY;
  1969. lastSign = signs = "";
  1970. document.addEventListener("touchmove", tracer, false);
  1971. }, false);
  1972. document.addEventListener("touchend", function(e) {
  1973. document.removeEventListener("touchmove", tracer, false);
  1974. if (signs == "→↓←↑") {
  1975. e.stopPropagation();
  1976. e.preventDefault();
  1977. startCustom();
  1978. }
  1979. }, false);
  1980. }
  1981.  
  1982. if (location.origin + location.pathname == configPage) {
  1983. let exampleNode = document.getElementById("example");
  1984. if (!exampleNode) return;
  1985.  
  1986. exampleNode = exampleNode.parentNode;
  1987. let ruleList = exampleNode.nextElementSibling.nextElementSibling;
  1988. let searchInput = document.createElement("input");
  1989. let inputTimer;
  1990. function searchByInput() {
  1991. clearTimeout(inputTimer);
  1992. inputTimer = setTimeout(() => {
  1993. let curValue = searchInput.value;
  1994. let matchRules = [];
  1995. let dontMatchRules = [];
  1996. if (curValue) {
  1997. for (let i = 0; i < ruleList.children.length; i++) {
  1998. let curRule = ruleList.children[i];
  1999. let aHref = curRule.firstChild.href;
  2000. if (aHref.indexOf(curValue) == -1) {
  2001. dontMatchRules.push(curRule);
  2002. } else {
  2003. matchRules.push(curRule);
  2004. }
  2005. }
  2006. } else {
  2007. dontMatchRules = ruleList.children;
  2008. }
  2009. if (matchRules.length) {
  2010. for (let i = 0; i < dontMatchRules.length; i++) {
  2011. let curRule = dontMatchRules[i];
  2012. curRule.style.display = "none";
  2013. }
  2014. for (let i = 0; i < matchRules.length; i++) {
  2015. let curRule = matchRules[i];
  2016. curRule.style.display = "";
  2017. }
  2018. } else {
  2019. for (let i = 0; i < dontMatchRules.length; i++) {
  2020. let curRule = dontMatchRules[i];
  2021. curRule.style.display = "";
  2022. }
  2023. }
  2024. }, 500);
  2025. }
  2026. searchInput.style.margin = "10px";
  2027. searchInput.style.width = "100%";
  2028. searchInput.placeholder = i18n.searchRule;
  2029. searchInput.addEventListener("input", function(e) {
  2030. searchByInput();
  2031. });
  2032. if (location.hash) {
  2033. let hash = location.hash.slice(1);
  2034. if (hash.indexOf("@") == 0) {
  2035. setTimeout(() => {
  2036. exampleNode.scrollIntoView();
  2037. }, 500);
  2038. searchInput.value = hash.slice(1);
  2039. searchByInput();
  2040. }
  2041. }
  2042. [].forEach.call(ruleList.querySelectorAll("div.highlight"), highlight => {
  2043. highlight.style.position = "relative";
  2044. highlight.innerHTML = highlight.innerHTML + copySvg;
  2045. let svg = highlight.children[1];
  2046. svg.addEventListener("click", function(e) {
  2047. GM_setClipboard(highlight.children[0].innerText);
  2048. svg.style.opacity = 0;
  2049. setTimeout(() => {
  2050. svg.style.opacity = 1;
  2051. }, 1000);
  2052. });
  2053. });
  2054. exampleNode.parentNode.insertBefore(searchInput, ruleList);
  2055.  
  2056.  
  2057. let donateNode = document.querySelector("[alt='donate']");
  2058. if (!donateNode) return;
  2059. let insertPos = donateNode.parentNode.nextElementSibling;
  2060. let radioIndex = 0;
  2061. function createOption(_name, _value, _type) {
  2062. if (!_type) _type = "input";
  2063. let con = document.createElement("div");
  2064. let option = document.createElement("input");
  2065. let cap = document.createElement("b");
  2066. option.type = _type;
  2067. option.value = _value;
  2068. option.checked = _value;
  2069. cap.style.margin = "0px 10px 0px 0px";
  2070. if (_type == "radio") {
  2071. let label = document.createElement("label");
  2072. label.innerText = _name;
  2073. radioIndex++;
  2074. option.id = "radio" + radioIndex;
  2075. label.setAttribute("for", option.id);
  2076. cap.appendChild(label);
  2077. } else {
  2078. if (_type == "input") {
  2079. option.style.flexGrow = "1";
  2080. }
  2081. cap.innerText = _name;
  2082. }
  2083. con.style.margin = "10px 0";
  2084. con.style.display = "flex";
  2085. con.style.alignItems = "center";
  2086. con.appendChild(cap);
  2087. con.appendChild(option);
  2088. insertPos.parentNode.insertBefore(con, insertPos);
  2089. return option;
  2090. }
  2091. function formatShortcut(e) {
  2092. let result = [];
  2093. if (e.ctrlKey) {
  2094. result.push("Ctrl");
  2095. }
  2096. if (e.shiftKey) {
  2097. result.push("Shift");
  2098. }
  2099. if (e.altKey) {
  2100. result.push("Alt");
  2101. }
  2102. if (e.metaKey) {
  2103. result.push("Meta");
  2104. }
  2105. result.push(e.key);
  2106. return result.join(" + ");
  2107. }
  2108. function geneShortcutData(str) {
  2109. if (!str) return "";
  2110. let result = {ctrlKey: false, shiftKey: false, altKey: false, metaKey: false, key: ''};
  2111. str.split(" + ").forEach(item => {
  2112. switch(item) {
  2113. case "Ctrl":
  2114. result.ctrlKey = true;
  2115. break;
  2116. case "Shift":
  2117. result.shiftKey = true;
  2118. break;
  2119. case "Alt":
  2120. result.altKey = true;
  2121. break;
  2122. case "Meta":
  2123. result.metaKey = true;
  2124. break;
  2125. default:
  2126. result.key = item;
  2127. break;
  2128. }
  2129. });
  2130. return result;
  2131. }
  2132. let showFilterList = createOption(i18n.showFilterList, !!GM_getValue("showFilterList"), "checkbox");
  2133. let downloadShortcutInput = createOption(i18n.downloadShortcut, formatShortcut(downloadShortcut) || "");
  2134. let downloadSingleShortcutInput = createOption(i18n.downloadSingleShortcut, formatShortcut(downloadSingleShortcut) || "");
  2135. let downloadCustomShortcutInput = createOption(i18n.downloadCustomShortcut, formatShortcut(downloadCustomShortcut) || "");
  2136. downloadShortcutInput.setAttribute("readonly", "true");
  2137. downloadSingleShortcutInput.setAttribute("readonly", "true");
  2138. downloadCustomShortcutInput.setAttribute("readonly", "true");
  2139. downloadShortcutInput.style.cursor = "cell";
  2140. downloadSingleShortcutInput.style.cursor = "cell";
  2141. downloadCustomShortcutInput.style.cursor = "cell";
  2142. let keydonwHandler = e => {
  2143. if (e.key) {
  2144. if (e.key == "Backspace") {
  2145. e.target.value = "";
  2146. } else if (e.key != "Control" && e.key != "Shift" && e.key != "Alt" && e.key != "Meta") {
  2147. e.target.value = formatShortcut(e);
  2148. }
  2149. }
  2150. e.preventDefault();
  2151. e.stopPropagation();
  2152. };
  2153. downloadShortcutInput.addEventListener("keydown", keydonwHandler);
  2154. downloadSingleShortcutInput.addEventListener("keydown", keydonwHandler);
  2155. downloadCustomShortcutInput.addEventListener("keydown", keydonwHandler);
  2156. let enableTouchInput = createOption(i18n.enableTouch, !!enableTouch, "checkbox");
  2157.  
  2158. let delSelector = createOption(i18n.del, GM_getValue("selectors") || "");
  2159. delSelector.setAttribute("placeHolder", ".mask,.ksam");
  2160. let downThreadNum = createOption(i18n.downThreadNum, GM_getValue("downThreadNum") || "20", "number");
  2161. let maxDlPerMin = createOption(i18n.maxDlPerMin, GM_getValue("maxDlPerMin") || "0", "number");
  2162. let customTitle = createOption(i18n.customTitle, GM_getValue("customTitle") || "");
  2163. let saveUrl = createOption(i18n.saveUrl, !!GM_getValue("saveUrl"), "checkbox");
  2164. let disableAutoStartSave = createOption(i18n.disableAutoStartSave, !!GM_getValue("disableAutoStartSave"), "checkbox");
  2165. customTitle.setAttribute("placeHolder", "title");
  2166. let minTxtLength = createOption(i18n.minTxtLength, GM_getValue("minTxtLength") || "100", "number");
  2167. let contentSortUrlValue = GM_getValue("contentSortUrl") || false;
  2168. let contentSortValue = GM_getValue("contentSort") || false;
  2169. let reSortDefault = createOption(i18n.reSortDefault, !contentSortUrlValue && !contentSortValue, "radio");
  2170. let reSortUrl = createOption(i18n.reSortUrl, contentSortUrlValue || false, "radio");
  2171. let contentSort = createOption(i18n.reSort, contentSortValue || false, "radio");
  2172. reSortDefault.name = "sort";
  2173. reSortUrl.name = "sort";
  2174. contentSort.name = "sort";
  2175. let reverse = createOption(i18n.reverseOrder, !!GM_getValue("reverse"), "checkbox");
  2176. let prefix = createOption(i18n.prefix, GM_getValue("prefix") || "");
  2177. let disableNextPage = !!GM_getValue("disableNextPage");
  2178. let nextPage = createOption(i18n.nextPage, !disableNextPage, "checkbox");
  2179. let nextPageReg = createOption(i18n.nextPageReg, GM_getValue("nextPageReg") || "");
  2180. let retainImage = createOption(i18n.retainImage, !!GM_getValue("retainImage"), "checkbox");
  2181. prefix.setAttribute("placeHolder", "第 $i 章:");
  2182. nextPageReg.setAttribute("placeHolder", "^\\s*(下一[页頁张張]|next\\s*page|次のページ)");
  2183. if (disableNextPage) {
  2184. nextPageReg.parentNode.style.display = "none";
  2185. }
  2186. nextPage.onclick = e => {
  2187. nextPageReg.parentNode.style.display = nextPage.checked ? "flex" : "none";
  2188. }
  2189. let saveBtn = document.createElement("button");
  2190. saveBtn.innerText = i18n.saveBtn;
  2191. saveBtn.style.margin = "0 0 20px 0";
  2192. insertPos.parentNode.insertBefore(saveBtn, insertPos);
  2193. saveBtn.onclick = e => {
  2194. GM_setValue("selectors", delSelector.value || "");
  2195. GM_setValue("downThreadNum", parseInt(downThreadNum.value || 20));
  2196. GM_setValue("maxDlPerMin", parseInt(maxDlPerMin.value || 20));
  2197. GM_setValue("minTxtLength", parseInt(minTxtLength.value || 100));
  2198. GM_setValue("customTitle", customTitle.value || "");
  2199. GM_setValue("disableAutoStartSave", disableAutoStartSave.checked);
  2200. GM_setValue("saveUrl", saveUrl.checked);
  2201. if (reSortUrl.checked) {
  2202. GM_setValue("contentSortUrl", true);
  2203. GM_setValue("contentSort", false);
  2204. } else if (contentSort.checked) {
  2205. GM_setValue("contentSortUrl", false);
  2206. GM_setValue("contentSort", true);
  2207. } else {
  2208. GM_setValue("contentSortUrl", false);
  2209. GM_setValue("contentSort", false);
  2210. }
  2211. GM_setValue("reverse", reverse.checked);
  2212. GM_setValue("enableTouch", enableTouchInput.checked);
  2213. GM_setValue("retainImage", retainImage.checked);
  2214. GM_setValue("showFilterList", showFilterList.checked);
  2215. GM_setValue("disableNextPage", !nextPage.checked);
  2216. GM_setValue("nextPageReg", nextPageReg.value || "");
  2217. GM_setValue("prefix", prefix.value || "");
  2218. GM_setValue("downloadShortcut", geneShortcutData(downloadShortcutInput.value) || "");
  2219. GM_setValue("downloadSingleShortcut", geneShortcutData(downloadSingleShortcutInput.value) || "");
  2220. GM_setValue("downloadCustomShortcut", geneShortcutData(downloadCustomShortcutInput.value) || "");
  2221. alert(i18n.saveOk);
  2222. };
  2223. return;
  2224. }
  2225.  
  2226. function setDel(){
  2227. GM_openInTab(configPage + "#操作說明", {active: true});
  2228. }
  2229.  
  2230. function checkKey(shortcut1, shortcut2) {
  2231. return shortcut1.ctrlKey == shortcut2.ctrlKey && shortcut1.shiftKey == shortcut2.shiftKey && shortcut1.altKey == shortcut2.altKey && shortcut1.metaKey == shortcut2.metaKey && shortcut1.key == shortcut2.key;
  2232. }
  2233.  
  2234. function startCustom() {
  2235. var customRules = GM_getValue("DACrules_" + document.domain);
  2236. var urls = window.prompt(i18n.customInfo + ":\nhttps://xxx.xxx/book-[20-99].html, https://xxx.xxx/book-[01-10].html", customRules || "");
  2237. if (urls) {
  2238. customDown(urls);
  2239. }
  2240. }
  2241.  
  2242. document.addEventListener("keydown", function(e) {
  2243. if (checkKey(downloadCustomShortcut, e)) {
  2244. startCustom();
  2245. } else if (checkKey(downloadSingleShortcut, e)) {
  2246. fetch(true);
  2247. } else if (checkKey(downloadShortcut, e)) {
  2248. fetch(false);
  2249. }
  2250. });
  2251. GM_registerMenuCommand(i18n.custom, () => {
  2252. startCustom();
  2253. });
  2254. GM_registerMenuCommand(i18n.fetch, fetch);
  2255. GM_registerMenuCommand(i18n.setting, setDel);
  2256. GM_registerMenuCommand(i18n.searchRule, searchRule);
  2257. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址