Chapter Downloader Auto

Tự động tải tối đa 200 chương trên truyen.tangthuvien.vn

当前为 2025-04-21 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Chapter Downloader Auto
  3. // @namespace http://tampermonkey.net/
  4. // @version 3.0
  5. // @description Tự động tải tối đa 200 chương trên truyen.tangthuvien.vn
  6. // @match https://truyen.tangthuvien.vn/doc-truyen/*
  7. // @grant none
  8. // ==/UserScript==
  9.  
  10. (function () {
  11. 'use strict';
  12.  
  13. const MAX_CHAPTERS = 200;
  14. let downloadedCount = parseInt(localStorage.getItem('dl_count') || '0');
  15. let isAutoDownloading = localStorage.getItem('dl_active') === 'true';
  16.  
  17. const textarea = document.createElement('textarea');
  18. textarea.style.cssText = `width: 100%; height: 300px; margin-bottom: 10px;`;
  19.  
  20. const panel = document.createElement('div');
  21. panel.style.cssText = `
  22. position: fixed; top: 10%; right: 10px; z-index: 9999;
  23. width: 300px; background: white; border: 1px solid #ccc; padding: 10px;
  24. `;
  25. const startBtn = document.createElement('button');
  26. startBtn.textContent = isAutoDownloading ? 'Dừng' : 'Bắt đầu';
  27. startBtn.style.cssText = `width: 100%; margin-bottom: 10px;`;
  28.  
  29. panel.appendChild(textarea);
  30. panel.appendChild(startBtn);
  31. document.body.appendChild(panel);
  32.  
  33. function saveContent() {
  34. localStorage.setItem('dl_content', textarea.value);
  35. localStorage.setItem('dl_count', downloadedCount.toString());
  36. }
  37.  
  38. textarea.value = localStorage.getItem('dl_content') || '';
  39.  
  40. function extractChapterContent() {
  41. const title = document.querySelector('h2')?.innerText.trim();
  42. const content = document.querySelector('.box-chap')?.innerText.trim();
  43. if (title && content) {
  44. textarea.value += `\n\n------------\n\n${title}\n\n${content}`;
  45. downloadedCount++;
  46. saveContent();
  47. return true;
  48. }
  49. return false;
  50. }
  51.  
  52. function goToNextChapter() {
  53. const next = document.querySelector('.bot-next_chap.bot-control') ||
  54. [...document.querySelectorAll('a')].find(a => /tiếp|next/i.test(a.textContent));
  55. if (next && next.href) {
  56. localStorage.setItem('dl_active', 'true'); // Ghi lại trạng thái đang chạy
  57. localStorage.setItem('dl_count', downloadedCount.toString());
  58. setTimeout(() => location.href = next.href, 1000); // chuyển trang
  59. return true;
  60. }
  61. return false;
  62. }
  63.  
  64. async function autoDownloadLoop() {
  65. if (downloadedCount >= MAX_CHAPTERS) {
  66. isAutoDownloading = false;
  67. localStorage.setItem('dl_active', 'false');
  68. startBtn.textContent = 'Bắt đầu';
  69. alert('Đã tải xong ' + MAX_CHAPTERS + ' chương.');
  70. return;
  71. }
  72.  
  73. if (extractChapterContent()) {
  74. goToNextChapter(); // Tự reload trang
  75. } else {
  76. isAutoDownloading = false;
  77. localStorage.setItem('dl_active', 'false');
  78. startBtn.textContent = 'Bắt đầu';
  79. }
  80. }
  81.  
  82. startBtn.addEventListener('click', () => {
  83. isAutoDownloading = !isAutoDownloading;
  84. localStorage.setItem('dl_active', isAutoDownloading ? 'true' : 'false');
  85. startBtn.textContent = isAutoDownloading ? 'Dừng' : 'Bắt đầu';
  86. if (isAutoDownloading) autoDownloadLoop();
  87. });
  88.  
  89. // Nếu reload trang và vẫn đang tải thì tiếp tục
  90. if (isAutoDownloading) {
  91. setTimeout(autoDownloadLoop, 1500); // đợi trang load xong
  92. }
  93. })();