TTV Auto Upload

Công cụ đăng chương hiện đại cho Tàng Thư Viện với UI/UX được tối ưu

  1. // ==UserScript==
  2. // @name TTV Auto Upload
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.6
  5. // @description Công cụ đăng chương hiện đại cho Tàng Thư Viện với UI/UX được tối ưu
  6. // @author HA
  7. // @match https://tangthuvien.net/dang-chuong/story/*
  8. // @match https://tangthuvien.net/danh-sach-chuong/story/*
  9. // @grant GM_addStyle
  10. // @grant GM_setValue
  11. // @grant GM_getValue
  12. // @required https://code.jquery.com/jquery-3.2.1.min.js
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17. if (window.location.href.includes('/danh-sach-chuong/story/')) {
  18. const storyId = window.location.pathname.split('/').pop();
  19. setTimeout(() => {
  20. window.location.href = `https://tangthuvien.net/dang-chuong/story/${storyId}`;
  21. }, 3000);
  22. return;
  23. }
  24.  
  25. const HEADER_SIGN = "";
  26. const FOOTER_SIGN = "";
  27. const MAX_CHAPTER_POST = 10;
  28. GM_addStyle(`
  29. /* Vị trí và giao diện của công cụ đăng nhanh */
  30. #modern-uploader {
  31. background: linear-gradient(145deg, #ffffff, #f5f8ff);
  32. padding: 28px;
  33. border-radius: 16px;
  34. box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
  35. position: fixed;
  36. right: 25px;
  37. top: 50%;
  38. transform: translateY(-50%);
  39. width: 450px;
  40. max-height: 92vh;
  41. overflow-y: auto;
  42. z-index: 1000;
  43. font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  44. border: 1px solid #e0e0e0;
  45. transition: all 0.3s ease;
  46. }
  47. #modern-uploader:hover {
  48. box-shadow: 0 12px 28px rgba(0, 0, 0, 0.15);
  49. }
  50. #modern-uploader::-webkit-scrollbar {
  51. width: 8px;
  52. }
  53. #modern-uploader::-webkit-scrollbar-track {
  54. background: #f5f5f5;
  55. border-radius: 8px;
  56. }
  57. #modern-uploader::-webkit-scrollbar-thumb {
  58. background: linear-gradient(180deg, #4285f4, #34a853);
  59. border-radius: 8px;
  60. }
  61. #modern-uploader::-webkit-scrollbar-thumb:hover {
  62. background: linear-gradient(180deg, #1a73e8, #27833c);
  63. }
  64. @keyframes shortChapterBlink {
  65. 0% { background-color: rgba(255, 0, 0, 0.1); }
  66. 50% { background-color: rgba(255, 0, 0, 0.2); }
  67. 100% { background-color: rgba(255, 0, 0, 0.1); }
  68. }
  69. textarea[name^="introduce"] {
  70. transition: all 0.3s ease;
  71. font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  72. line-height: 1.6;
  73. }
  74. textarea[name^="introduce"].short-chapter {
  75. animation: shortChapterBlink 1s infinite;
  76. border: 2px solid #ff0000 !important;
  77. background-color: rgba(255, 0, 0, 0.1) !important;
  78. }
  79. .chapter-character-count {
  80. text-align: right;
  81. font-size: 12px;
  82. margin-top: 10px;
  83. color: #666;
  84. font-weight: 500;
  85. }
  86. .short-chapters-warning {
  87. color: #ff0000;
  88. font-weight: bold;
  89. animation: shortChapterBlink 1s infinite;
  90. }
  91.  
  92. .button-container {
  93. display: flex;
  94. justify-content: center;
  95. align-items: center;
  96. gap: 14px;
  97. margin-top: 24px;
  98. flex-wrap: wrap;
  99. }
  100. #modern-uploader .btn {
  101. padding: 12px 20px;
  102. border-radius: 10px;
  103. cursor: pointer;
  104. font-weight: 600;
  105. font-size: 14px;
  106. transition: all 0.3s ease;
  107. border: none;
  108. box-shadow: 0 4px 6px rgba(0,0,0,0.1);
  109. display: flex;
  110. align-items: center;
  111. gap: 6px;
  112. }
  113. #modern-uploader .btn:hover {
  114. transform: translateY(-3px);
  115. box-shadow: 0 6px 10px rgba(0,0,0,0.15);
  116. }
  117. #modern-uploader .btn:active {
  118. transform: translateY(-1px);
  119. box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  120. }
  121. #modern-uploader .btn-primary {
  122. background: linear-gradient(140deg, #4285f4, #1a73e8);
  123. color: white;
  124. }
  125. #modern-uploader .btn-success {
  126. background: linear-gradient(140deg, #34a853, #27833c);
  127. color: white;
  128. }
  129. #modern-uploader .btn-danger {
  130. background: linear-gradient(140deg, #ea4335, #d32f2f);
  131. color: white;
  132. }
  133. #modern-uploader .btn-secondary {
  134. background: linear-gradient(140deg, #9e9e9e, #757575);
  135. color: white;
  136. }
  137. #modern-uploader .btn-warning {
  138. background: linear-gradient(140deg, #fbbc05, #f5a623);
  139. color: #212529;
  140. }
  141. #modern-uploader .btn-info {
  142. background: linear-gradient(140deg, #17a2b8, #0097a7);
  143. color: white;
  144. }
  145. #modern-uploader .form-control {
  146. width: 100%;
  147. padding: 16px;
  148. border: 1px solid #ddd;
  149. border-radius: 10px;
  150. margin-bottom: 18px;
  151. font-size: 15px;
  152. transition: all 0.3s ease;
  153. font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  154. }
  155. #modern-uploader .form-control:focus {
  156. border-color: #4285f4;
  157. outline: none;
  158. box-shadow: 0 0 0 3px rgba(66, 133, 244, 0.2);
  159. }
  160. #modern-uploader .loading-overlay {
  161. position: fixed;
  162. inset: 0;
  163. background-color: rgba(0, 0, 0, 0.7);
  164. display: flex;
  165. align-items: center;
  166. justify-content: center;
  167. z-index: 9999;
  168. backdrop-filter: blur(5px);
  169. }
  170. #modern-uploader .loading-spinner {
  171. width: 60px;
  172. height: 60px;
  173. border: 5px solid rgba(255, 255, 255, 0.3);
  174. border-radius: 50%;
  175. border-top-color: #ffffff;
  176. animation: spin 1s linear infinite;
  177. box-shadow: 0 0 20px rgba(255, 255, 255, 0.5);
  178. }
  179. @keyframes spin {
  180. to { transform: rotate(360deg); }
  181. }
  182. /* Checkbox tự động */
  183. #modern-uploader .checkbox-container {
  184. display: flex;
  185. justify-content: center;
  186. align-items: center;
  187. margin-bottom: 20px;
  188. background: linear-gradient(145deg, #f8f9fa, #f1f3f5);
  189. border-radius: 12px;
  190. padding: 14px;
  191. border: 1px solid #e9ecef;
  192. position: relative;
  193. overflow: hidden;
  194. }
  195. #modern-uploader .checkbox-container::before {
  196. content: '';
  197. position: absolute;
  198. width: 100%;
  199. height: 3px;
  200. bottom: 0;
  201. left: 0;
  202. background: linear-gradient(90deg, #4285f4, #34a853, #fbbc05, #ea4335);
  203. opacity: 0.7;
  204. }
  205. #qpOptionLoop {
  206. margin-right: 10px;
  207. width: 18px;
  208. height: 18px;
  209. cursor: pointer;
  210. accent-color: #4285f4;
  211. }
  212. /* Header styling */
  213. #modern-uploader .text-center {
  214. text-align: center;
  215. }
  216. #modern-uploader .uploader-header {
  217. margin-bottom: 25px;
  218. position: relative;
  219. }
  220. #modern-uploader .uploader-header::after {
  221. content: '';
  222. position: absolute;
  223. bottom: -10px;
  224. left: 50%;
  225. transform: translateX(-50%);
  226. width: 80%;
  227. height: 3px;
  228. background: linear-gradient(90deg, transparent, #4285f4, transparent);
  229. border-radius: 3px;
  230. }
  231. #modern-uploader h3 {
  232. color: #4285f4;
  233. font-weight: 700;
  234. font-size: 22px;
  235. display: flex;
  236. align-items: center;
  237. justify-content: center;
  238. gap: 10px;
  239. margin: 0;
  240. padding: 0;
  241. letter-spacing: 0.5px;
  242. }
  243. /* Notification styling */
  244. #modern-uploader .notification-container {
  245. width: 100%;
  246. padding: 14px 0;
  247. margin-top: 18px;
  248. text-align: left;
  249. border-top: 1px solid rgba(0,0,0,0.06);
  250. }
  251. #modern-uploader .notification-success,
  252. #modern-uploader .notification-error,
  253. #modern-uploader .notification-warning,
  254. #modern-uploader .notification-info {
  255. padding: 14px 18px;
  256. border-radius: 12px;
  257. font-size: 14px;
  258. font-weight: 500;
  259. box-shadow: 0 4px 12px rgba(0,0,0,0.08);
  260. display: inline-block;
  261. max-width: 100%;
  262. margin: 0;
  263. word-break: break-word;
  264. animation: fadeInUp 0.4s ease;
  265. line-height: 1.5;
  266. }
  267. @keyframes fadeInUp {
  268. from { opacity: 0; transform: translateY(15px); }
  269. to { opacity: 1; transform: translateY(0); }
  270. }
  271. #modern-uploader .notification-success {
  272. background: linear-gradient(145deg, #e8f5e9, #c8e6c9);
  273. color: #2e7d32;
  274. border-left: 4px solid #4caf50;
  275. }
  276. #modern-uploader .notification-error {
  277. background: linear-gradient(145deg, #ffebee, #ffcdd2);
  278. color: #c62828;
  279. border-left: 4px solid #f44336;
  280. }
  281. #modern-uploader .notification-warning {
  282. background: linear-gradient(145deg, #fff8e1, #ffecb3);
  283. color: #f57f17;
  284. border-left: 4px solid #ffb300;
  285. }
  286. #modern-uploader .notification-info {
  287. background: linear-gradient(145deg, #e3f2fd, #bbdefb);
  288. color: #1565c0;
  289. border-left: 4px solid #2196f3;
  290. }
  291. /* Google-style waves */
  292. @keyframes wave-animation {
  293. 0% {transform: translateX(-100%);}
  294. 100% {transform: translateX(100%);}
  295. }
  296. #modern-uploader .wave-bottom {
  297. position: absolute;
  298. bottom: 0;
  299. left: 0;
  300. width: 100%;
  301. height: 5px;
  302. overflow: hidden;
  303. z-index: 1;
  304. }
  305. #modern-uploader .wave-bottom::after {
  306. content: '';
  307. position: absolute;
  308. width: 200%;
  309. height: 100%;
  310. background: linear-gradient(90deg, transparent, rgba(66, 133, 244, 0.3), transparent);
  311. animation: wave-animation 2s infinite linear;
  312. }
  313. `);
  314.  
  315. function showNotification(message, type) {
  316. jQuery('#modern-uploader .notification-container').remove();
  317. const container = jQuery("<div>", {
  318. class: "notification-container"
  319. });
  320. const notification = jQuery("<div>", {
  321. class: `notification-${type}`
  322. });
  323. const lines = message.split('\n');
  324. lines.forEach((line, index) => {
  325. notification.append(jQuery("<div>").html(line));
  326. });
  327. container.append(notification);
  328. jQuery("#modern-uploader .button-container").after(container);
  329. notification.fadeIn(300);
  330. }
  331.  
  332. function createChapterHTML(chapNum) {
  333. const chap_vol = parseInt(jQuery('.chap_vol').val()) || 1;
  334. const chap_vol_name = jQuery('.chap_vol_name').val() || '';
  335. return `
  336. <div data-gen="MK_GEN" id="COUNT_CHAP_${chapNum}_MK">
  337. <div class="col-xs-12 form-group"></div>
  338. <div class="form-group">
  339. <label class="col-sm-2" for="chap_stt">STT</label>
  340. <div class="col-sm-8">
  341. <input class="form-control" required name="chap_stt[${chapNum}]" value="${dangNhanhTTV.STATE.CHAP_STT}" placeholder="Số thứ tự của chương" type="text"/>
  342. </div>
  343. </div>
  344. <div class="form-group">
  345. <label class="col-sm-2" for="chap_number">Chương thứ..</label>
  346. <div class="col-sm-8">
  347. <input value="${dangNhanhTTV.STATE.CHAP_SERIAL}" required class="form-control" name="chap_number[${chapNum}]" placeholder="Chương thứ.. (1,2,3..)" type="text"/>
  348. </div>
  349. </div>
  350. <div class="form-group">
  351. <label class="col-sm-2" for="chap_name">Quyn số</label>
  352. <div class="col-sm-8">
  353. <input class="form-control" name="vol[${chapNum}]" placeholder="Quyển số" type="number" value="${chap_vol}" required/>
  354. </div>
  355. </div>
  356. <div class="form-group">
  357. <label class="col-sm-2" for="chap_name">Tên quyn</label>
  358. <div class="col-sm-8">
  359. <input class="form-control chap_vol_name" name="vol_name[${chapNum}]" placeholder="Tên quyển" type="text" value="${chap_vol_name}" />
  360. </div>
  361. </div>
  362. <div class="form-group">
  363. <label class="col-sm-2" for="chap_name">Tên chương</label>
  364. <div class="col-sm-8">
  365. <input required class="form-control" name="chap_name[${chapNum}]" placeholder="Tên chương" type="text"/>
  366. </div>
  367. </div>
  368. <div class="form-group">
  369. <label class="col-sm-2" for="introduce">Ni dung</label>
  370. <div class="col-sm-8">
  371. <textarea maxlength="75000" style="color:#000;font-weight: 400;" required class="form-control" name="introduce[${chapNum}]" rows="20" placeholder="Nội dung" type="text"></textarea>
  372. <div class="chapter-character-count"></div>
  373. </div>
  374. </div>
  375. <div class="form-group">
  376. <label class="col-sm-2" for="adv">Qung cáo</label>
  377. <div class="col-sm-8">
  378. <textarea maxlength="1000" class="form-control" name="adv[${chapNum}]" placeholder="Quảng cáo" type="text"></textarea>
  379. </div>
  380. </div>
  381. </div>`;
  382. }
  383.  
  384. function setupCharacterCounter() {
  385. jQuery(document).on("input", "[name^=introduce]", function() {
  386. const text = jQuery(this).val();
  387. const charCount = text.length;
  388. let charCountElement = jQuery(this).next('.chapter-character-count');
  389. if (charCountElement.length === 0) {
  390. charCountElement = jQuery('<div class="chapter-character-count"></div>');
  391. jQuery(this).after(charCountElement);
  392. }
  393. if(charCount < 3000) {
  394. jQuery(this).addClass('short-chapter');
  395. charCountElement.html(`<span class="short-chapters-warning">${charCount.toLocaleString()}/40.000 ký tự</span>`);
  396. } else {
  397. jQuery(this).removeClass('short-chapter');
  398. if(charCount > 40000) {
  399. charCountElement.html(`<span style="color: #fbbc05;">${charCount.toLocaleString()}/40.000 ký tự</span>`);
  400. } else {
  401. charCountElement.html(`<span style="color: #34a853;">${charCount.toLocaleString()}/40.000 ký tự</span>`);
  402. }
  403. }
  404. });
  405. }
  406.  
  407. function validateChapterLengths() {
  408. let hasError = false;
  409. jQuery('form[name="postChapForm"] .chapter-detail').each(function() {
  410. const form = this;
  411. const contentTextarea = form.querySelector('textarea[name^="introduce"]');
  412. const content = contentTextarea.value;
  413. if (content.length < 3000) {
  414. jQuery(contentTextarea).addClass('short-chapter');
  415. let warningIcon = form.querySelector('.warning-icon');
  416. if (!warningIcon) {
  417. warningIcon = document.createElement('div');
  418. warningIcon.className = 'warning-icon';
  419. warningIcon.innerHTML = '⚠️';
  420. contentTextarea.parentNode.appendChild(warningIcon);
  421. }
  422. hasError = true;
  423. } else {
  424. jQuery(contentTextarea).removeClass('short-chapter');
  425. const warningIcon = form.querySelector('.warning-icon');
  426. if (warningIcon) {
  427. warningIcon.remove();
  428. }
  429. }
  430. });
  431. return !hasError;
  432. }
  433.  
  434. const dangNhanhTTV = {
  435. STATE: {
  436. CHAP_NUMBER: 1,
  437. CHAP_STT: 1,
  438. CHAP_SERIAL: 1,
  439. CHAP_NUMBER_ORIGINAL: 1,
  440. CHAP_STT_ORIGINAL: 1,
  441. CHAP_SERIAL_ORIGINAL: 1,
  442. AUTO_POST: false,
  443. TOTAL_CHAPTERS: 0,
  444. POSTED_CHAPTERS: 0
  445. },
  446. ELEMENTS: {
  447. qpContent: null,
  448. qpButtonSubmit: null,
  449. qpButtonRemoveEmpty: null,
  450. qpButtonReset: null,
  451. qpButtonPaste: null,
  452. qpButtonAutoPost: null,
  453. qpOptionLoop: null
  454. },
  455. init: function() {
  456. try {
  457. console.log('[TTV-DEBUG] Script bắt đầu khởi tạo...');
  458. console.log('[TTV-DEBUG] Phiên bản script: 3.0');
  459. this.initializeChapterValues();
  460. console.log('[TTV-DEBUG] Đã khởi tạo giá trị chương');
  461.  
  462. // Khôi phục trạng thái tự động đăng
  463. this.loadAutoPostState();
  464. console.log('[TTV-DEBUG] Đã khôi phục trạng thái tự động đăng');
  465.  
  466. this.createInterface();
  467. console.log('[TTV-DEBUG] Đã tạo giao diện');
  468. this.cacheElements();
  469. console.log('[TTV-DEBUG] Đã cache các elements');
  470. this.registerEvents();
  471. console.log('[TTV-DEBUG] Đã đăng ký các events');
  472. console.log('[TTV-DEBUG] Script đã khởi động thành công');
  473. showNotification('Công cụ đã chạy thành công', 'success');
  474.  
  475. // Cập nhật hiển thị nút tự động đăng
  476. if (this.STATE.AUTO_POST) {
  477. this.ELEMENTS.qpButtonAutoPost.text(`🔄 T ĐỘNG (${this.STATE.POSTED_CHAPTERS}/${this.STATE.TOTAL_CHAPTERS})`);
  478. this.ELEMENTS.qpButtonAutoPost.removeClass('btn-warning').addClass('btn-info');
  479. }
  480.  
  481. // Khôi phục trạng thái chế độ tự động đăng
  482. const isAutoMode = localStorage.getItem('TTV_AUTO_MODE') === 'true';
  483. if (isAutoMode) {
  484. this.ELEMENTS.qpOptionLoop.prop('checked', true);
  485. this.toggleAutoMode(); // Áp dụng giao diện theo chế độ
  486. }
  487. } catch (e) {
  488. console.error('[TTV-ERROR] Lỗi khởi tạo:', e);
  489. showNotification('Có lỗi khi khởi tạo Script', 'error');
  490. }
  491. },
  492.  
  493. loadAutoPostState: function() {
  494. // Khôi phục trạng thái tự động đăng từ localStorage
  495. const autoPost = localStorage.getItem('TTV_AUTO_POST') === 'true';
  496. this.STATE.AUTO_POST = autoPost;
  497.  
  498. if (autoPost) {
  499. this.STATE.TOTAL_CHAPTERS = parseInt(localStorage.getItem('TTV_TOTAL_CHAPTERS') || '0');
  500. this.STATE.POSTED_CHAPTERS = parseInt(localStorage.getItem('TTV_POSTED_CHAPTERS') || '0');
  501.  
  502. console.log(`[TTV-DEBUG] Khôi phc t động đăng: ${this.STATE.POSTED_CHAPTERS}/${this.STATE.TOTAL_CHAPTERS}`);
  503. }
  504. },
  505.  
  506. createInterface: function() {
  507. const html = `
  508. <div id="modern-uploader">
  509. <div class="uploader-header">
  510. <h3>📚 CÔNG C ĐĂNG CHƯƠNG</h3>
  511. </div>
  512. <div class="form-group">
  513. <textarea placeholder="Nội dung truyện (Dán vào đây để tự động tách chương)" id="qpContent" class="form-control" rows="5"></textarea>
  514. </div>
  515. <div class="checkbox-container">
  516. <input type="checkbox" id="qpOptionLoop">
  517. <label for="qpOptionLoop" style="color: #1a73e8; font-weight: 600; margin-left: 8px;">Chế độ t động đăng</label>
  518. </div>
  519. <div class="button-container">
  520. <button class="btn btn-primary" id="qpButtonPaste">📋 Dán ni dung</button>
  521. <button class="btn btn-success" id="qpButtonSubmit">📤 Đăng chương</button>
  522. <button class="btn btn-danger" id="qpButtonRemoveEmpty" style="display: none;">🗑️ n chương trng</button>
  523. <button class="btn btn-warning" id="qpButtonAutoPost" style="display: none;">🔄 T ĐỘNG (TT)</button>
  524. <button class="btn btn-secondary" id="qpButtonReset" style="display: none;">🔁 Reset đếm</button>
  525. </div>
  526. <div class="notification-container"></div>
  527. <div class="wave-bottom"></div>
  528. </div>`;
  529.  
  530. jQuery(".list-in-user").before(html);
  531. },
  532.  
  533. initializeChapterValues: function() {
  534. try {
  535. const chap_number = parseInt(jQuery('#chap_number').val());
  536. let chap_stt = parseInt(jQuery('.chap_stt1').val());
  537. let chap_serial = parseInt(jQuery('.chap_serial').val());
  538.  
  539. if (parseInt(jQuery('#chap_stt').val()) > chap_stt) {
  540. chap_stt = parseInt(jQuery('#chap_stt').val());
  541. }
  542. if (parseInt(jQuery('#chap_serial').val()) > chap_serial) {
  543. chap_serial = parseInt(jQuery('#chap_serial').val());
  544. }
  545.  
  546. this.STATE.CHAP_NUMBER = this.STATE.CHAP_NUMBER_ORIGINAL = chap_number || 1;
  547. this.STATE.CHAP_STT = this.STATE.CHAP_STT_ORIGINAL = chap_stt || 1;
  548. this.STATE.CHAP_SERIAL = this.STATE.CHAP_SERIAL_ORIGINAL = chap_serial || 1;
  549. } catch (e) {
  550. console.error("Error initializing chapter values:", e);
  551. }
  552. },
  553.  
  554. cacheElements: function() {
  555. this.ELEMENTS.qpContent = jQuery("#qpContent");
  556. this.ELEMENTS.qpButtonSubmit = jQuery("#qpButtonSubmit");
  557. this.ELEMENTS.qpButtonRemoveEmpty = jQuery("#qpButtonRemoveEmpty");
  558. this.ELEMENTS.qpButtonPaste = jQuery("#qpButtonPaste");
  559. this.ELEMENTS.qpButtonAutoPost = jQuery("#qpButtonAutoPost");
  560. this.ELEMENTS.qpButtonReset = jQuery("#qpButtonReset");
  561. this.ELEMENTS.qpOptionLoop = jQuery("#qpOptionLoop");
  562. },
  563.  
  564. registerEvents: function() {
  565. this.ELEMENTS.qpContent.on("paste", this.handlePaste.bind(this));
  566. this.ELEMENTS.qpButtonRemoveEmpty.on('click', this.removeEmptyChapters.bind(this));
  567. this.ELEMENTS.qpButtonSubmit.on('click', this.submitChapters.bind(this));
  568. this.ELEMENTS.qpButtonPaste.on('click', this.handlePasteButton.bind(this));
  569. this.ELEMENTS.qpButtonAutoPost.on('click', this.toggleAutoPost.bind(this));
  570. this.ELEMENTS.qpButtonReset.on('click', this.resetAutoPost.bind(this));
  571. this.ELEMENTS.qpOptionLoop.on('change', this.toggleAutoMode.bind(this));
  572. setupCharacterCounter();
  573.  
  574. // Kiểm tra và bắt đầu tự động đăng nếu đã bật
  575. if (window.location.href.includes('/dang-chuong/story/')) {
  576. setTimeout(() => {
  577. if (this.STATE.AUTO_POST && this.STATE.POSTED_CHAPTERS < this.STATE.TOTAL_CHAPTERS) {
  578. this.runAutoPostSequence();
  579. }
  580. }, 2000);
  581. }
  582. },
  583.  
  584. handlePasteButton: function() {
  585. this.showLoading();
  586. navigator.clipboard.readText()
  587. .then(text => {
  588. this.ELEMENTS.qpContent.val(text);
  589. setTimeout(() => {
  590. this.performAction();
  591. this.hideLoading();
  592. }, 100);
  593. })
  594. .catch(err => {
  595. console.error('Không thể đọc dữ liệu từ clipboard:', err);
  596. this.hideLoading();
  597. showNotification('Không thể truy cập clipboard. Vui lòng dán trực tiếp vào ô nội dung.', 'error');
  598. });
  599. },
  600.  
  601. handlePaste: function(e) {
  602. e.preventDefault();
  603. this.ELEMENTS.qpContent.val("");
  604. this.showLoading();
  605. const pastedText = e.originalEvent.clipboardData.getData('text');
  606. this.ELEMENTS.qpContent.val(pastedText);
  607. setTimeout(() => {
  608. this.performAction();
  609. this.hideLoading();
  610. }, 100);
  611. },
  612.  
  613. performAction: function() {
  614. try {
  615. console.log("Starting performAction");
  616. var text = this.ELEMENTS.qpContent.val();
  617.  
  618. if (!text) {
  619. showNotification('Không có nội dung để tách chương', 'error');
  620. return 0;
  621. }
  622. var debugOutput = [];
  623. var chapters = [];
  624. var lines = text.split('\n');
  625. var currentChapter = [];
  626. var lastTitle = null;
  627. debugOutput.push("=== Processing Text ===");
  628. debugOutput.push(`Total lines: ${lines.length}`);
  629. debugOutput.push("=== Line Analysis ===");
  630. function visualizeWhitespace(str) {
  631. return str.split('').map(c => {
  632. if (c === '\t') return '\\t';
  633. if (c === ' ') return '·';
  634. if (c === '\n') return '\\n';
  635. return c;
  636. }).join('');
  637. }
  638.  
  639. // Hàm lấy mã chương dựa vào tiêu đề
  640. function getChapterCode(title) {
  641. // Lấy số chương + tên chương, bỏ qua các ký tự đặc biệt
  642. const match = title.match(/[Cc]hương\s*(\d+)\s*:/);
  643. if (!match) return title.trim();
  644. const chapterNum = match[1];
  645. return `chap_${chapterNum}`;
  646. }
  647.  
  648. for (let i = 0; i < lines.length; i++) {
  649. let line = lines[i];
  650. let isChapterTitle = /^\t[Cc]hương\s*\d+\s*:/.test(line) || /^\s{4,}[Cc]hương\s*\d+\s*:/.test(line);
  651. debugOutput.push(`Line ${i}: ${visualizeWhitespace(line.substring(0, 50))}${line.length > 50 ? '...' : ''}`);
  652. debugOutput.push(` Is chapter: ${isChapterTitle}`);
  653.  
  654. if (isChapterTitle) {
  655. // Lấy mã chương để so sánh
  656. const currentChapterCode = getChapterCode(line);
  657. const lastTitleCode = lastTitle ? getChapterCode(lastTitle) : null;
  658.  
  659. if (currentChapter.length > 0) {
  660. // Kiểm tra nếu chương hiện tại khác chương trước đó
  661. if (currentChapterCode !== lastTitleCode) {
  662. chapters.push(currentChapter.join('\n'));
  663. currentChapter = [line];
  664. lastTitle = line;
  665. debugOutput.push(` -> New chapter started: ${currentChapterCode}`);
  666. } else {
  667. debugOutput.push(` -> Skipped duplicate chapter: ${currentChapterCode}`);
  668. // Không cần thêm dòng này vào chapter hiện tại vì nó là tiêu đề trùng lặp
  669. }
  670. } else {
  671. currentChapter = [line];
  672. lastTitle = line;
  673. debugOutput.push(` -> First chapter started: ${currentChapterCode}`);
  674. }
  675. } else if (currentChapter.length > 0) {
  676. currentChapter.push(line);
  677. }
  678. }
  679. if (currentChapter.length > 0) {
  680. chapters.push(currentChapter.join('\n'));
  681. debugOutput.push("-> Added final chapter");
  682. }
  683. debugOutput.push("=== Results ===");
  684. debugOutput.push(`Found ${chapters.length} chapters`);
  685. const processedChapters = [];
  686. for (let i = 0; i < chapters.length; i++) {
  687. const chapterLines = chapters[i].split('\n');
  688. const title = chapterLines.shift().trim();
  689. const chapterText = chapterLines.join('\n');
  690. const charCount = chapterText.length;
  691. debugOutput.push(`Chapter ${i+1} character count: ${charCount}`);
  692. if (charCount > 40000) {
  693. const parts = Math.ceil(charCount / 40000);
  694. debugOutput.push(`Splitting into ${parts} parts`);
  695. const charsPerPart = Math.ceil(charCount / parts);
  696. debugOutput.push(`Characters per part: ~${charsPerPart}`);
  697. let currentText = chapterText;
  698. let totalProcessed = 0;
  699. for (let part = 0; part < parts; part++) {
  700. const isLastPart = part === parts - 1;
  701. const targetSize = isLastPart ? currentText.length : charsPerPart;
  702. let endPos = Math.min(targetSize, currentText.length);
  703. if (!isLastPart && endPos < currentText.length) {
  704. const nextParagraph = currentText.indexOf('\n\n', endPos - 500);
  705. if (nextParagraph !== -1 && nextParagraph < endPos + 500) {
  706. endPos = nextParagraph + 2;
  707. } else {
  708. const sentenceEnd = Math.max(
  709. currentText.lastIndexOf('. ', endPos),
  710. currentText.lastIndexOf('! ', endPos),
  711. currentText.lastIndexOf('? ', endPos)
  712. );
  713. if (sentenceEnd !== -1 && sentenceEnd > endPos - 500) {
  714. endPos = sentenceEnd + 2;
  715. }
  716. }
  717. }
  718. const partContent = currentText.substring(0, endPos);
  719. totalProcessed += partContent.length;
  720. currentText = currentText.substring(endPos);
  721. let chapterTitle = title;
  722. if (title.includes(':')) {
  723. chapterTitle = title.substring(title.indexOf(':') + 1).trim();
  724. }
  725. let newTitle = `${title} (Phn ${part+1}/${parts})`;
  726. processedChapters.push(newTitle + '\n' + partContent);
  727. debugOutput.push(`Part ${part+1}: ${partContent.length} chars`);
  728. }
  729. debugOutput.push(`Total processed: ${totalProcessed}/${charCount} chars`);
  730. } else {
  731. processedChapters.push(chapters[i]);
  732. }
  733. }
  734. debugOutput.push(`After processing: ${processedChapters.length} chapters`);
  735. const chaptersToFill = processedChapters.slice(0, MAX_CHAPTER_POST);
  736. const remainingChapters = processedChapters.slice(MAX_CHAPTER_POST);
  737. var titles = jQuery("input[name^='chap_name']");
  738. var contents = jQuery("textarea[name^='introduce']");
  739. var advs = jQuery("textarea[name^='adv']");
  740. debugOutput.push(`Forms found: ${titles.length}`);
  741. if (processedChapters.length === 0) {
  742. showNotification('Không tìm thấy chương nào', 'error');
  743. jQuery('#debug-output').text(debugOutput.join('\n'));
  744. return;
  745. }
  746. if (remainingChapters.length > 0) {
  747. debugOutput.push(`${remainingChapters.length} chapters will be copied to clipboard`);
  748. }
  749. const neededForms = chaptersToFill.length - titles.length;
  750. if (neededForms > 0 && titles.length < MAX_CHAPTER_POST) {
  751. debugOutput.push(`Need to add ${neededForms} more forms`);
  752. for (let i = 0; i < neededForms && (titles.length + i) < MAX_CHAPTER_POST; i++) {
  753. this.addNewChapter();
  754. }
  755. titles = jQuery("input[name^='chap_name']");
  756. contents = jQuery("textarea[name^='introduce']");
  757. advs = jQuery("textarea[name^='adv']");
  758. }
  759. debugOutput.push(`Filling ${chaptersToFill.length} chapters into forms`);
  760. jQuery.each(titles, function(k, v) {
  761. if (k < chaptersToFill.length) {
  762. var content = chaptersToFill[k].split('\n');
  763. var title = content.shift().trim();
  764. var chapterTitle = title;
  765. if (title.includes(':')) {
  766. chapterTitle = title.substring(title.indexOf(':') + 1).trim();
  767. }
  768. debugOutput.push(`\nFilling chapter ${k + 1}:`);
  769. debugOutput.push(`Original title: ${title}`);
  770. debugOutput.push(`Extracted title: ${chapterTitle}`);
  771. debugOutput.push(`Content length: ${content.length} lines`);
  772. if (!chapterTitle || chapterTitle.trim() === '') {
  773. chapterTitle = "Vô đề";
  774. debugOutput.push(`Empty title detected, using default: ${chapterTitle}`);
  775. }
  776. titles[k].value = chapterTitle;
  777. contents[k].value = HEADER_SIGN + "\r\n" + content.join('\n') + "\r\n" + FOOTER_SIGN;
  778. if (advs[k]) advs[k].value = "";
  779. jQuery(contents[k]).trigger('input');
  780. }
  781. });
  782. if (remainingChapters.length > 0) {
  783. try {
  784. const clipboardContent = remainingChapters.map(chap => {
  785. const lines = chap.trim().split('\n');
  786. if (lines.length > 0) {
  787. if (!lines[0].startsWith('\t')) {
  788. lines[0] = '\t' + lines[0];
  789. }
  790. }
  791. return lines.join('\n');
  792. }).join('\n\n---CHAPTER_SEPARATOR---\n\n');
  793. let splitChapters = 0;
  794. let shortChapters = 0;
  795. let shortChapterDetails = [];
  796. let longChapterDetails = [];
  797. for (let i = 0; i < chapters.length; i++) {
  798. const chapterLines = chapters[i].split('\n');
  799. const title = chapterLines.shift().trim();
  800. const chapterText = chapterLines.join('\n');
  801. if (chapterText.length > 40000) {
  802. splitChapters++;
  803. const partsCount = Math.ceil(chapterText.length / 40000);
  804. longChapterDetails.push({
  805. title: title,
  806. parts: partsCount
  807. });
  808. }
  809. if (chapterText.length < 3000) {
  810. shortChapters++;
  811. shortChapterDetails.push({
  812. title: title,
  813. length: chapterText.length
  814. });
  815. }
  816. }
  817. const splittedChaptersCount = processedChapters.length - (chapters.length - splitChapters);
  818. let message = '';
  819. message = message.concat(`📝 Đã x lý ${processedChapters.length} Chương\n`);
  820. message = message.concat(`📝 Đã nhp ${Math.min(processedChapters.length, MAX_CHAPTER_POST)} Chương\n`);
  821.  
  822. if (remainingChapters.length > 0) {
  823. message = message.concat(`📋 Đã lưu li ${remainingChapters.length} Chương\n`);
  824. }
  825. if (splitChapters > 0) {
  826. message = message.concat(`📑 Có ${splitChapters} Chương đã chia thành ${splittedChaptersCount} Chương\n`);
  827. longChapterDetails.forEach(chapter => {
  828. let chapterName = chapter.title;
  829. if (chapterName.includes(':')) {
  830. chapterName = chapterName.trim();
  831. }
  832. message = message.concat(` - ${chapterName}: ${chapter.parts} Chương\n`);
  833. });
  834. }
  835. if (shortChapters > 0) {
  836. message = message.concat(`⚠️<span class="short-chapters-warning">Có ${shortChapters} chương dưới 3000 ký tự</span>\n`);
  837. shortChapterDetails.forEach(chapter => {
  838. let chapterName = chapter.title;
  839. if (chapterName.includes(':')) {
  840. chapterName = chapterName.trim();
  841. }
  842. message = message.concat(`<span class="short-chapters-warning"> - ${chapterName}: có ${chapter.length.toLocaleString()} ký tự</span>\n`);
  843. });
  844. }
  845. if (navigator.clipboard && navigator.clipboard.writeText) {
  846. navigator.clipboard.writeText(clipboardContent)
  847. .then(() => {
  848. debugOutput.push(`📋Đã lưu li ${remainingChapters.length} Chương\n`);
  849. showNotification(message, remainingChapters.length > 0 ? 'warning' : 'success');
  850. })
  851. .catch(err => {
  852. throw err;
  853. });
  854. } else {
  855. const tempTextarea = document.createElement('textarea');
  856. tempTextarea.style.position = 'fixed';
  857. tempTextarea.style.top = '0';
  858. tempTextarea.style.left = '0';
  859. tempTextarea.style.width = '2em';
  860. tempTextarea.style.height = '2em';
  861. tempTextarea.style.opacity = '0';
  862. tempTextarea.style.pointerEvents = 'none';
  863. tempTextarea.value = clipboardContent;
  864. document.body.appendChild(tempTextarea);
  865. tempTextarea.focus();
  866. tempTextarea.select();
  867. const successful = document.execCommand('copy');
  868. document.body.removeChild(tempTextarea);
  869. if (!successful) {
  870. throw new Error('Không thể sao chép vào clipboard');
  871. }
  872. debugOutput.push(`Đã sao chép ${remainingChapters.length} chương vào clipboard (execCommand)`);
  873. showNotification(message, 'success');
  874. }
  875. } catch (err) {
  876. console.error('Lỗi khi sao chép vào clipboard:', err);
  877. debugOutput.push(`Li khi sao chép vào clipboard: ${err.message}`);
  878. showNotification('Không thể sao chép vào clipboard. Vui lòng thử lại.', 'error');
  879. const manualCopyArea = document.createElement('div');
  880. manualCopyArea.style.position = 'fixed';
  881. manualCopyArea.style.top = '50%';
  882. manualCopyArea.style.left = '50%';
  883. manualCopyArea.style.transform = 'translate(-50%, -50%)';
  884. manualCopyArea.style.backgroundColor = 'white';
  885. manualCopyArea.style.padding = '20px';
  886. manualCopyArea.style.borderRadius = '8px';
  887. manualCopyArea.style.boxShadow = '0 4px 6px -1px rgba(0, 0, 0, 0.1)';
  888. manualCopyArea.style.zIndex = '10000';
  889. manualCopyArea.style.maxWidth = '80%';
  890. manualCopyArea.style.maxHeight = '80%';
  891. manualCopyArea.style.overflow = 'auto';
  892. manualCopyArea.innerHTML = `
  893. <h3 style="margin-top: 0;">Sao chép th công</h3>
  894. <p>Không th sao chép t động. Vui lòng chn toàn b ni dung bên dưới và sao chép (Ctrl+C):</p>
  895. <textarea style="width: 100%; height: 300px; padding: 10px;">${clipboardContent}</textarea>
  896. <div style="text-align: right; margin-top: 10px;">
  897. <button id="closeManualCopy" style="padding: 8px 16px; background: #3b82f6; color: white; border: none; border-radius: 4px; cursor: pointer;">Đóng</button>
  898. </div>
  899. `;
  900. document.body.appendChild(manualCopyArea);
  901. document.getElementById('closeManualCopy').addEventListener('click', () => {
  902. document.body.removeChild(manualCopyArea);
  903. });
  904. }
  905. }
  906. this.ELEMENTS.qpButtonSubmit.removeClass("btn-disable").addClass("btn-success");
  907. this.ELEMENTS.qpContent.val("Đã xử lý xong");
  908. if (remainingChapters.length === 0) {
  909. let splitChapters = 0;
  910. let shortChapters = 0;
  911. let shortChapterDetails = [];
  912. let longChapterDetails = [];
  913. for (let i = 0; i < chapters.length; i++) {
  914. const chapterLines = chapters[i].split('\n');
  915. const title = chapterLines.shift().trim();
  916. const chapterText = chapterLines.join('\n');
  917. if (chapterText.length > 40000) {
  918. splitChapters++;
  919. const partsCount = Math.ceil(chapterText.length / 40000);
  920. longChapterDetails.push({
  921. title: title,
  922. parts: partsCount
  923. });
  924. }
  925. if (chapterText.length < 3000) {
  926. shortChapters++;
  927. shortChapterDetails.push({
  928. title: title,
  929. length: chapterText.length
  930. });
  931. }
  932. }
  933. const splittedChaptersCount = processedChapters.length - (chapters.length - splitChapters);
  934. let message = '';
  935. message = message.concat(`📝 Đã x lý ${processedChapters.length} Chương\n`);
  936. message = message.concat(`📝 Đã nhp ${Math.min(processedChapters.length, MAX_CHAPTER_POST)} Chương\n`);
  937. if (splitChapters > 0) {
  938. message = message.concat(`📑 Có ${splitChapters} Chương dài chia thành ${splittedChaptersCount} Chương\n`);
  939. longChapterDetails.forEach(chapter => {
  940. let chapterName = chapter.title;
  941. if (chapterName.includes(':')) {
  942. chapterName = chapterName.trim();
  943. }
  944. message = message.concat(` - ${chapterName}: ${chapter.parts} Chương\n`);
  945. });
  946. }
  947. if (shortChapters > 0) {
  948. message = message.concat(`⚠️<span class="short-chapters-warning">Có ${shortChapters} chương dưới 3000 ký tự</span>\n`);
  949. shortChapterDetails.forEach(chapter => {
  950. let chapterName = chapter.title;
  951. if (chapterName.includes(':')) {
  952. chapterName = chapterName.trim();
  953. }
  954. message = message.concat(`<span class="short-chapters-warning"> - ${chapterName}: có ${chapter.length.toLocaleString()} ký tự</span>\n`);
  955. });
  956. }
  957. showNotification(message, 'success');
  958. }
  959. jQuery('#debug-output').text(debugOutput.join('\n'));
  960. return processedChapters.length;
  961. } catch (e) {
  962. console.error("Error in performAction:", e);
  963. showNotification('Có lỗi khi tách chương', 'error');
  964. return 0;
  965. }
  966. },
  967.  
  968. removeEmptyChapters: function() {
  969. const forms = document.querySelectorAll('[data-gen="MK_GEN"]');
  970. let removed = 0;
  971.  
  972. forms.forEach(form => {
  973. const content = form.querySelector('textarea[name^="introduce"]').value.trim();
  974. if (!content) {
  975. form.remove();
  976. removed++;
  977. this.updateChapNumber(false);
  978. }
  979. });
  980. showNotification(`🧹 Đã x lý ${forms.length} chương, xóa ${removed} chương trng`, 'info');
  981. },
  982.  
  983. toggleAutoPost: function() {
  984. this.STATE.AUTO_POST = !this.STATE.AUTO_POST;
  985.  
  986. if (this.STATE.AUTO_POST) {
  987. // Lưu trạng thái tự động đăng vào localStorage
  988. localStorage.setItem('TTV_AUTO_POST', 'true');
  989. this.STATE.TOTAL_CHAPTERS = parseInt(prompt("Nhập tổng số lần tự động đăng:", "10")) || 0;
  990. this.STATE.POSTED_CHAPTERS = parseInt(localStorage.getItem('TTV_POSTED_CHAPTERS') || '0');
  991.  
  992. localStorage.setItem('TTV_TOTAL_CHAPTERS', this.STATE.TOTAL_CHAPTERS.toString());
  993.  
  994. this.ELEMENTS.qpButtonAutoPost.text(`🔄 T ĐỘNG (${this.STATE.POSTED_CHAPTERS}/${this.STATE.TOTAL_CHAPTERS})`);
  995. this.ELEMENTS.qpButtonAutoPost.removeClass('btn-warning').addClass('btn-info');
  996.  
  997. showNotification(`✅ Đã bt t động đăng (${this.STATE.POSTED_CHAPTERS}/${this.STATE.TOTAL_CHAPTERS})`, 'success');
  998.  
  999. // Bắt đầu quy trình tự động
  1000. if (window.location.href.includes('/dang-chuong/story/')) {
  1001. setTimeout(() => this.runAutoPostSequence(), 2000);
  1002. }
  1003. } else {
  1004. // Tắt tự động đăng
  1005. localStorage.setItem('TTV_AUTO_POST', 'false');
  1006. // Reset số lần đã đăng về 0
  1007. this.STATE.POSTED_CHAPTERS = 0;
  1008. localStorage.setItem('TTV_POSTED_CHAPTERS', '0');
  1009. // Reset tổng số lần đăng về 0
  1010. this.STATE.TOTAL_CHAPTERS = 0;
  1011. localStorage.setItem('TTV_TOTAL_CHAPTERS', '0');
  1012. this.ELEMENTS.qpButtonAutoPost.text('🔄 TỰ ĐỘNG (TẮT)');
  1013. this.ELEMENTS.qpButtonAutoPost.removeClass('btn-info').addClass('btn-warning');
  1014. showNotification('❌ Đã tắt tự động đăng và reset số lần đăng', 'info');
  1015. }
  1016. },
  1017.  
  1018. runAutoPostSequence: function() {
  1019. // Kiểm tra trước khi chạy tự động
  1020. if (this.STATE.POSTED_CHAPTERS >= this.STATE.TOTAL_CHAPTERS) {
  1021. // Reset trạng thái tự động nếu đã hoàn thành
  1022. this.STATE.AUTO_POST = false;
  1023. localStorage.setItem('TTV_AUTO_POST', 'false');
  1024. this.ELEMENTS.qpButtonAutoPost.text('🔄 TỰ ĐỘNG (TẮT)');
  1025. this.ELEMENTS.qpButtonAutoPost.removeClass('btn-info').addClass('btn-warning');
  1026.  
  1027. // Reset số lần đã đăng
  1028. this.STATE.POSTED_CHAPTERS = 0;
  1029. localStorage.setItem('TTV_POSTED_CHAPTERS', '0');
  1030.  
  1031. showNotification(`🎉 Đã hoàn thành t động đăng ${this.STATE.TOTAL_CHAPTERS}/${this.STATE.TOTAL_CHAPTERS} chương và đã reset s ln đăng`, 'success');
  1032. return;
  1033. }
  1034.  
  1035. if (!this.STATE.AUTO_POST) {
  1036. return;
  1037. }
  1038.  
  1039. // Tự động nhấn nút Paste sau 2 giây
  1040. setTimeout(() => {
  1041. if (this.STATE.AUTO_POST) {
  1042. this.handlePasteButton();
  1043.  
  1044. // Tự động nhấn nút Đăng chương sau 3 giây
  1045. setTimeout(() => {
  1046. if (this.STATE.AUTO_POST) {
  1047. this.submitChapters();
  1048. }
  1049. }, 3000);
  1050. }
  1051. }, 2000);
  1052. },
  1053.  
  1054. submitChapters: function() {
  1055. if (!validateChapterLengths()) {
  1056. showNotification('⚠️ Có chương có độ dài dưới 3000 ký tự. Vui lòng kiểm tra lại.', 'error');
  1057. return;
  1058. }
  1059. this.showLoading();
  1060. document.querySelector('form[name="postChapForm"] button[type="submit"]').click();
  1061.  
  1062. if (this.STATE.AUTO_POST) {
  1063. // Cập nhật số chương đã đăng
  1064. this.STATE.POSTED_CHAPTERS++;
  1065. localStorage.setItem('TTV_POSTED_CHAPTERS', this.STATE.POSTED_CHAPTERS.toString());
  1066.  
  1067. // Kiểm tra và tắt tự động đăng nếu đã đủ số chương
  1068. if (this.STATE.POSTED_CHAPTERS >= this.STATE.TOTAL_CHAPTERS) {
  1069. this.STATE.AUTO_POST = false;
  1070. localStorage.setItem('TTV_AUTO_POST', 'false');
  1071. this.ELEMENTS.qpButtonAutoPost.text('🔄 TỰ ĐỘNG (TẮT)');
  1072. this.ELEMENTS.qpButtonAutoPost.removeClass('btn-info').addClass('btn-warning');
  1073.  
  1074. // Reset số lần đã đăng
  1075. this.STATE.POSTED_CHAPTERS = 0;
  1076. localStorage.setItem('TTV_POSTED_CHAPTERS', '0');
  1077.  
  1078. setTimeout(() => {
  1079. showNotification(`🎉 Đã hoàn thành t động đăng ${this.STATE.TOTAL_CHAPTERS}/${this.STATE.TOTAL_CHAPTERS} chương và đã reset s ln đăng`, 'success');
  1080. }, 3000);
  1081. } else {
  1082. // Cập nhật hiển thị số chương đã đăng
  1083. this.ELEMENTS.qpButtonAutoPost.text(`🔄 T ĐỘNG (${this.STATE.POSTED_CHAPTERS}/${this.STATE.TOTAL_CHAPTERS})`);
  1084. }
  1085. }
  1086.  
  1087. setTimeout(() => this.hideLoading(), 2000);
  1088. },
  1089.  
  1090. addNewChapter: function() {
  1091. if ((this.STATE.CHAP_NUMBER + 1) <= MAX_CHAPTER_POST) {
  1092. this.updateChapNumber(true);
  1093. const html = createChapterHTML(this.STATE.CHAP_NUMBER);
  1094. jQuery('#add-chap').before(html);
  1095. setupCharacterCounter();
  1096. } else {
  1097. showNotification(`⚠️ Ch có th đăng ti đa ${MAX_CHAPTER_POST} chương mt ln`, 'warning');
  1098. }
  1099. },
  1100.  
  1101. updateChapNumber: function(isAdd) {
  1102. try{
  1103. if (isAdd) {
  1104. let maxStt = 0;
  1105. let maxSerial = 0;
  1106. jQuery('input[name^="chap_stt"]').each(function() {
  1107. const val = parseInt(jQuery(this).val()) || 0;
  1108. maxStt = Math.max(maxStt, val);
  1109. });
  1110. jQuery('input[name^="chap_number"]').each(function() {
  1111. const val = parseInt(jQuery(this).val()) || 0;
  1112. maxSerial = Math.max(maxSerial, val);
  1113. });
  1114. const chapStt = parseInt(jQuery('.chap_stt1').val()) || 0;
  1115. const chapSerial = parseInt(jQuery('.chap_serial').val()) || 0;
  1116. maxStt = Math.max(maxStt, chapStt);
  1117. maxSerial = Math.max(maxSerial, chapSerial);
  1118. this.STATE.CHAP_STT = maxStt + 1;
  1119. this.STATE.CHAP_SERIAL = maxSerial + 1;
  1120. this.STATE.CHAP_NUMBER++;
  1121. } else {
  1122. if (this.STATE.CHAP_NUMBER > this.STATE.CHAP_NUMBER_ORIGINAL) {
  1123. this.STATE.CHAP_NUMBER--;
  1124. }
  1125. if (this.STATE.CHAP_STT > this.STATE.CHAP_STT_ORIGINAL) {
  1126. this.STATE.CHAP_STT--;
  1127. }
  1128. if (this.STATE.CHAP_SERIAL > this.STATE.CHAP_SERIAL_ORIGINAL) {
  1129. this.STATE.CHAP_SERIAL--;
  1130. }
  1131. }
  1132. jQuery('#chap_number').val(this.STATE.CHAP_NUMBER);
  1133. jQuery('#chap_stt').val(this.STATE.CHAP_STT);
  1134. jQuery('#chap_serial').val(this.STATE.CHAP_SERIAL);
  1135. jQuery('#countNumberPost').text(this.STATE.CHAP_NUMBER);
  1136. } catch (e) {
  1137. console.log("Lỗi: " + e);
  1138. }
  1139. },
  1140.  
  1141. showLoading: function() {
  1142. const loading = jQuery("<div>", {
  1143. class: "loading-overlay",
  1144. html: "<div class='loading-spinner'></div>"
  1145. });
  1146. jQuery("body").append(loading);
  1147. },
  1148.  
  1149. hideLoading: function() {
  1150. jQuery(".loading-overlay").remove();
  1151. },
  1152.  
  1153. resetAutoPost: function() {
  1154. this.STATE.TOTAL_CHAPTERS = 0;
  1155. this.STATE.POSTED_CHAPTERS = 0;
  1156. localStorage.removeItem('TTV_TOTAL_CHAPTERS');
  1157. localStorage.removeItem('TTV_POSTED_CHAPTERS');
  1158. this.ELEMENTS.qpButtonAutoPost.text('🔄 TỰ ĐỘNG (TẮT)');
  1159. this.ELEMENTS.qpButtonAutoPost.removeClass('btn-info').addClass('btn-warning');
  1160. showNotification('🔄 Đã reset số lần tự động đăng', 'info');
  1161. },
  1162.  
  1163. toggleAutoMode: function() {
  1164. const isAutoMode = this.ELEMENTS.qpOptionLoop.is(':checked');
  1165.  
  1166. if (isAutoMode) {
  1167. // Hiển thị nút tự động đăng và reset, ẩn nút paste và đăng chương
  1168. this.ELEMENTS.qpButtonAutoPost.show();
  1169. this.ELEMENTS.qpButtonReset.show();
  1170. this.ELEMENTS.qpButtonPaste.hide();
  1171. this.ELEMENTS.qpButtonSubmit.hide();
  1172.  
  1173. showNotification('🔄 Đã bật chế độ tự động', 'info');
  1174. } else {
  1175. // Hiển thị nút paste và đăng chương, ẩn nút tự động đăng và reset
  1176. this.ELEMENTS.qpButtonAutoPost.hide();
  1177. this.ELEMENTS.qpButtonReset.hide();
  1178. this.ELEMENTS.qpButtonPaste.show();
  1179. this.ELEMENTS.qpButtonSubmit.show();
  1180.  
  1181. showNotification('❌ Đã tắt chế độ tự động', 'info');
  1182. }
  1183.  
  1184. // Lưu trạng thái vào localStorage
  1185. localStorage.setItem('TTV_AUTO_MODE', isAutoMode.toString());
  1186. }
  1187. };
  1188. dangNhanhTTV.init();
  1189. })();