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.2
  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 panel = document.createElement('div');
  18. panel.style.cssText = `
  19. position: fixed; top: 10%; right: 10px; z-index: 9999;
  20. width: 300px; background: white; border: 1px solid #ccc; padding: 10px;
  21. box-shadow: 0 0 8px rgba(0,0,0,0.2); font-family: Arial;
  22. `;
  23.  
  24. const textarea = document.createElement('textarea');
  25. textarea.style.cssText = `width: 100%; height: 300px; margin-bottom: 10px;`;
  26. textarea.value = localStorage.getItem('dl_content') || '';
  27. panel.appendChild(textarea);
  28.  
  29. const startBtn = document.createElement('button');
  30. startBtn.textContent = isAutoDownloading ? 'Dừng' : 'Bắt đầu';
  31. startBtn.style.cssText = `width: 100%; margin-bottom: 10px; background: ${isAutoDownloading ? '#f44336' : '#4CAF50'}; color: white; border: none; padding: 10px; border-radius: 4px; cursor: pointer;`;
  32. panel.appendChild(startBtn);
  33.  
  34. const statusDiv = document.createElement('div');
  35. statusDiv.style.cssText = `font-size: 14px; color: #333; padding-top: 6px; text-align: center;`;
  36. panel.appendChild(statusDiv);
  37.  
  38. function setStatus(text) {
  39. statusDiv.textContent = text;
  40. }
  41.  
  42. function saveContent() {
  43. localStorage.setItem('dl_content', textarea.value);
  44. localStorage.setItem('dl_count', downloadedCount.toString());
  45. }
  46.  
  47. function extractChapterContent() {
  48. const title = document.querySelector('h2')?.innerText.trim();
  49. const content = document.querySelector('.box-chap')?.innerText.trim();
  50. if (title && content) {
  51. textarea.value += `\n\n------------\n\n${title}\n\n${content}`;
  52. downloadedCount++;
  53. saveContent();
  54. setStatus(`✅ Đã ly chương: ${title}`);
  55. return true;
  56. }
  57. setStatus("❌ Không tìm được nội dung chương.");
  58. return false;
  59. }
  60.  
  61. function goToNextChapter() {
  62. const nextLink =
  63. document.querySelector('.bot-next_chap.bot-control')?.href ||
  64. [...document.querySelectorAll('a')].find(a => /tiếp|next/i.test(a.textContent))?.href ||
  65. document.querySelector('a[rel="next"]')?.href;
  66.  
  67. if (nextLink) {
  68. console.log("➡️ Chuyển đến:", nextLink);
  69. localStorage.setItem('dl_active', 'true');
  70. localStorage.setItem('dl_count', downloadedCount.toString());
  71. setStatus('➡️ Chuyển chương mới...');
  72. setTimeout(() => location.href = nextLink, 1500);
  73. return true;
  74. } else {
  75. setStatus('❌ Không tìm thấy link chương tiếp.');
  76. console.warn("❌ Không tìm thấy link chương tiếp.");
  77. return false;
  78. }
  79. }
  80.  
  81. async function autoDownloadLoop() {
  82. if (downloadedCount >= MAX_CHAPTERS) {
  83. isAutoDownloading = false;
  84. localStorage.setItem('dl_active', 'false');
  85. startBtn.textContent = 'Bắt đầu';
  86. startBtn.style.backgroundColor = '#4CAF50';
  87. setStatus(`✅ Đã ti xong ${MAX_CHAPTERS} chương.`);
  88. alert('Đã tải xong ' + MAX_CHAPTERS + ' chương.');
  89. return;
  90. }
  91.  
  92. if (extractChapterContent()) {
  93. goToNextChapter();
  94. } else {
  95. isAutoDownloading = false;
  96. localStorage.setItem('dl_active', 'false');
  97. startBtn.textContent = 'Bắt đầu';
  98. startBtn.style.backgroundColor = '#4CAF50';
  99. }
  100. }
  101.  
  102. startBtn.addEventListener('click', () => {
  103. isAutoDownloading = !isAutoDownloading;
  104. localStorage.setItem('dl_active', isAutoDownloading ? 'true' : 'false');
  105. startBtn.textContent = isAutoDownloading ? 'Dừng' : 'Bắt đầu';
  106. startBtn.style.backgroundColor = isAutoDownloading ? '#f44336' : '#4CAF50';
  107. if (isAutoDownloading) autoDownloadLoop();
  108. });
  109.  
  110. document.body.appendChild(panel);
  111.  
  112. // Nếu reload trang và vẫn đang tải thì tiếp tục
  113. if (isAutoDownloading) {
  114. setStatus(`▶️ Tiếp tc ti chương... (${downloadedCount}/${MAX_CHAPTERS})`);
  115. setTimeout(autoDownloadLoop, 2000); // đợi trang load xong
  116. } else {
  117. setStatus("⏸ Chờ bắt đầu...");
  118. }
  119. })();