Chapter Downloader Auto

Tự động tải tối đa 200 chương trên truyen.tangthuvien.vn với giao diện hiển thị rõ tiến trình

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

  1. // ==UserScript==
  2. // @name Chapter Downloader Auto
  3. // @namespace http://tampermonkey.net/
  4. // @version 3.1
  5. // @description Tự động tải tối đa 200 chương trên truyen.tangthuvien.vn với giao diện hiển thị rõ tiến trình
  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. // UI Elements
  18. const panel = document.createElement('div');
  19. const textarea = document.createElement('textarea');
  20. const startBtn = document.createElement('button');
  21. const statusDiv = document.createElement('div');
  22.  
  23. // Setup UI
  24. panel.style.cssText = `
  25. position: fixed; top: 10%; right: 10px; z-index: 9999;
  26. width: 320px; background: #fff; border: 1px solid #ccc; padding: 10px;
  27. font-family: Arial, sans-serif; font-size: 14px;
  28. `;
  29. textarea.style.cssText = `width: 100%; height: 250px; margin-bottom: 10px;`;
  30. startBtn.style.cssText = `width: 100%; margin-bottom: 10px; padding: 10px;`;
  31. statusDiv.style.cssText = `margin-bottom: 10px; color: #333; font-weight: bold;`;
  32.  
  33. startBtn.textContent = isAutoDownloading ? `⏸ Dng (${downloadedCount}/${MAX_CHAPTERS})` : '▶️ Bắt đầu';
  34.  
  35. panel.appendChild(textarea);
  36. panel.appendChild(startBtn);
  37. panel.appendChild(statusDiv);
  38. document.body.appendChild(panel);
  39.  
  40. // Load previous content
  41. textarea.value = localStorage.getItem('dl_content') || '';
  42.  
  43. function saveContent() {
  44. localStorage.setItem('dl_content', textarea.value);
  45. localStorage.setItem('dl_count', downloadedCount.toString());
  46. }
  47.  
  48. function setStatus(msg) {
  49. statusDiv.textContent = msg;
  50. }
  51.  
  52. function extractChapterContent() {
  53. const title = document.querySelector('h2')?.innerText.trim();
  54. const content = document.querySelector('.box-chap')?.innerText.trim();
  55. if (title && content) {
  56. textarea.value += `\n\n------------\n\n${title}\n\n${content}`;
  57. downloadedCount++;
  58. saveContent();
  59. setStatus(`✅ Đã ly: ${title}`);
  60. return true;
  61. } else {
  62. setStatus('❌ Không thể lấy nội dung chương.');
  63. return false;
  64. }
  65. }
  66.  
  67. function goToNextChapter() {
  68. const next = document.querySelector('.bot-next_chap.bot-control') ||
  69. [...document.querySelectorAll('a')].find(a => /tiếp|next/i.test(a.textContent));
  70. if (next && next.href) {
  71. localStorage.setItem('dl_active', 'true');
  72. localStorage.setItem('dl_count', downloadedCount.toString());
  73. setStatus('➡️ Đang chuyển sang chương tiếp...');
  74. setTimeout(() => location.href = next.href, 1500);
  75. return true;
  76. } else {
  77. setStatus('❌ Không tìm thấy nút chương tiếp theo.');
  78. return false;
  79. }
  80. }
  81.  
  82. async function autoDownloadLoop() {
  83. if (downloadedCount >= MAX_CHAPTERS) {
  84. isAutoDownloading = false;
  85. localStorage.setItem('dl_active', 'false');
  86. startBtn.textContent = '▶️ Bắt đầu';
  87. setStatus(`🏁 Đã ti xong ${MAX_CHAPTERS} chương.`);
  88. return;
  89. }
  90.  
  91. if (extractChapterContent()) {
  92. startBtn.textContent = `⏸ Dng (${downloadedCount}/${MAX_CHAPTERS})`;
  93. goToNextChapter(); // Sẽ reload lại trang và tiếp tục
  94. } else {
  95. isAutoDownloading = false;
  96. localStorage.setItem('dl_active', 'false');
  97. startBtn.textContent = '▶️ Bắt đầu';
  98. }
  99. }
  100.  
  101. startBtn.addEventListener('click', () => {
  102. isAutoDownloading = !isAutoDownloading;
  103. localStorage.setItem('dl_active', isAutoDownloading ? 'true' : 'false');
  104. startBtn.textContent = isAutoDownloading ? `⏸ Dng (${downloadedCount}/${MAX_CHAPTERS})` : '▶️ Bắt đầu';
  105. if (isAutoDownloading) autoDownloadLoop();
  106. });
  107.  
  108. if (isAutoDownloading) {
  109. setStatus('🔁 Tiếp tục tải chương...');
  110. setTimeout(autoDownloadLoop, 1500);
  111. } else {
  112. setStatus('💤 Đang chờ bắt đầu...');
  113. }
  114. })();