Chapter Downloader

Extract content from truyen.tangthuvien.net

当前为 2025-03-09 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Chapter Downloader
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.4
  5. // @description Extract content from truyen.tangthuvien.net
  6. // @author You
  7. // @match https://truyen.tangthuvien.net/doc-truyen/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. // Create container for extracted content
  15. function createContainer() {
  16. // Check if container already exists
  17. if (document.getElementById('content-extractor')) {
  18. return;
  19. }
  20.  
  21. const container = document.createElement('div');
  22. container.id = 'content-extractor';
  23. container.style.position = 'fixed';
  24. container.style.bottom = '0';
  25. container.style.left = '0';
  26. container.style.right = '0';
  27. container.style.backgroundColor = '#f5f5f5';
  28. container.style.padding = '15px';
  29. container.style.zIndex = '9999';
  30. container.style.boxShadow = '0 -2px 10px rgba(0,0,0,0.1)';
  31. container.style.maxHeight = '50vh';
  32. container.style.overflow = 'auto';
  33.  
  34. const title = document.createElement('h3');
  35. title.textContent = 'Nội dung truyện đã trích xuất';
  36. title.style.marginTop = '0';
  37. container.appendChild(title);
  38.  
  39. const textarea = document.createElement('textarea');
  40. textarea.id = 'extracted-content';
  41. textarea.style.width = '100%';
  42. textarea.style.height = '150px';
  43. textarea.style.marginBottom = '10px';
  44. textarea.style.padding = '10px';
  45. textarea.style.boxSizing = 'border-box';
  46. container.appendChild(textarea);
  47.  
  48. const buttonContainer = document.createElement('div');
  49. buttonContainer.style.display = 'flex';
  50. buttonContainer.style.gap = '10px';
  51.  
  52. const extractButton = document.createElement('button');
  53. extractButton.textContent = 'Trích xuất nội dung trang hiện tại';
  54. extractButton.style.padding = '8px 15px';
  55. extractButton.style.backgroundColor = '#4caf50';
  56. extractButton.style.color = 'white';
  57. extractButton.style.border = 'none';
  58. extractButton.style.borderRadius = '4px';
  59. extractButton.style.cursor = 'pointer';
  60. extractButton.addEventListener('click', extractContent);
  61. buttonContainer.appendChild(extractButton);
  62.  
  63. const nextChapterButton = document.createElement('button');
  64. nextChapterButton.textContent = 'Chuyển đến chương tiếp theo';
  65. nextChapterButton.style.padding = '8px 15px';
  66. nextChapterButton.style.backgroundColor = '#2196f3';
  67. nextChapterButton.style.color = 'white';
  68. nextChapterButton.style.border = 'none';
  69. nextChapterButton.style.borderRadius = '4px';
  70. nextChapterButton.style.cursor = 'pointer';
  71. nextChapterButton.addEventListener('click', goToNextChapter);
  72. buttonContainer.appendChild(nextChapterButton);
  73.  
  74. const clearButton = document.createElement('button');
  75. clearButton.textContent = 'Xóa nội dung';
  76. clearButton.style.padding = '8px 15px';
  77. clearButton.style.backgroundColor = '#f44336';
  78. clearButton.style.color = 'white';
  79. clearButton.style.border = 'none';
  80. clearButton.style.borderRadius = '4px';
  81. clearButton.style.cursor = 'pointer';
  82. clearButton.addEventListener('click', clearContent);
  83. buttonContainer.appendChild(clearButton);
  84.  
  85. const copyButton = document.createElement('button');
  86. copyButton.textContent = 'Sao chép nội dung';
  87. copyButton.style.padding = '8px 15px';
  88. copyButton.style.backgroundColor = '#ff9800';
  89. copyButton.style.color = 'white';
  90. copyButton.style.border = 'none';
  91. copyButton.style.borderRadius = '4px';
  92. copyButton.style.cursor = 'pointer';
  93. copyButton.addEventListener('click', copyContent);
  94. buttonContainer.appendChild(copyButton);
  95.  
  96. container.appendChild(buttonContainer);
  97. document.body.appendChild(container);
  98. }
  99.  
  100. // Extract content from current page
  101. function extractContent() {
  102. const textarea = document.getElementById('extracted-content');
  103. // Get chapter title
  104. const chapterTitle = document.querySelector('h2');
  105. let titleText = chapterTitle ? chapterTitle.textContent.trim() : 'Không tìm thấy tiêu đề';
  106. // Get chapter content
  107. const chapterContentBox = document.querySelector('.box-chap');
  108. let contentText = '';
  109. if (chapterContentBox) {
  110. // Remove unwanted elements like scripts, styles, ads
  111. const contentClone = chapterContentBox.cloneNode(true);
  112. const scripts = contentClone.querySelectorAll('script, style, .ads, .banner, .comment');
  113. scripts.forEach(script => script.remove());
  114. // Get the clean text
  115. contentText = contentClone.innerText.trim();
  116. } else {
  117. contentText = 'Không tìm thấy nội dung chương';
  118. }
  119. // Append to existing content
  120. textarea.value += `\n\n${titleText}\n\n${contentText}`;
  121. // Scroll to bottom of textarea
  122. textarea.scrollTop = textarea.scrollHeight;
  123. }
  124.  
  125. // Go to next chapter
  126. function goToNextChapter() {
  127. const nextButton = document.querySelector('.bot-next_chap.bot-control');
  128. if (nextButton) {
  129. // Extract current content before moving to next chapter
  130. extractContent();
  131. // Navigate to next chapter
  132. nextButton.click();
  133. // Add a delay to let the next page load before extracting content
  134. setTimeout(() => {
  135. extractContent();
  136. }, 2000);
  137. } else {
  138. alert('Không tìm thấy nút chuyển đến chương tiếp theo');
  139. }
  140. }
  141.  
  142. // Clear content in textarea
  143. function clearContent() {
  144. const textarea = document.getElementById('extracted-content');
  145. textarea.value = '';
  146. }
  147.  
  148. // Copy content to clipboard
  149. function copyContent() {
  150. const textarea = document.getElementById('extracted-content');
  151. textarea.select();
  152. document.execCommand('copy');
  153. // Visual feedback
  154. const originalColor = textarea.style.backgroundColor;
  155. textarea.style.backgroundColor = '#e6ffe6';
  156. setTimeout(() => {
  157. textarea.style.backgroundColor = originalColor;
  158. }, 500);
  159. }
  160.  
  161. // Initialize when page is loaded
  162. function init() {
  163. createContainer();
  164. }
  165.  
  166. // Run when page is fully loaded
  167. window.addEventListener('load', init);
  168. })();