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

目前為 2025-03-08 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name TTV Auto Upload
  3. // @namespace http://tampermonkey.net/
  4. // @version 6.1
  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.  
  29. GM_addStyle(`
  30. #modern-uploader {
  31. background-color: white;
  32. padding: 20px;
  33. border-radius: 10px;
  34. box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
  35. position: fixed;
  36. right: 20px;
  37. top: 50%;
  38. transform: translateY(-50%);
  39. width: 400px;
  40. max-height: 90vh;
  41. overflow-y: auto;
  42. z-index: 1000;
  43. }
  44. @keyframes shortChapterBlink {
  45. 0% { background-color: rgba(255, 0, 0, 0.1); }
  46. 50% { background-color: rgba(255, 0, 0, 0.2); }
  47. 100% { background-color: rgba(255, 0, 0, 0.1); }
  48. }
  49. textarea[name^="introduce"] {
  50. transition: all 0.3s ease;
  51. }
  52. textarea[name^="introduce"].short-chapter {
  53. animation: shortChapterBlink 1s infinite;
  54. border: 2px solid #ff0000 !important;
  55. background-color: rgba(255, 0, 0, 0.1) !important;
  56. }
  57. .chapter-character-count {
  58. text-align: right;
  59. font-size: 12px;
  60. margin-top: 5px;
  61. color: #666;
  62. }
  63. .short-chapters-warning {
  64. color: #ff0000;
  65. font-weight: bold;
  66. animation: shortChapterBlink 1s infinite;
  67. }
  68. .button-container {
  69. display: flex;
  70. justify-content: space-between;
  71. align-items: center;
  72. gap: 15px;
  73. margin-top: 15px;
  74. }
  75. #modern-uploader .btn {
  76. padding: 10px 20px;
  77. border-radius: 6px;
  78. cursor: pointer;
  79. font-weight: 600;
  80. font-size: 14px;
  81. transition: all 0.2s ease;
  82. }
  83. #modern-uploader .form-control {
  84. width: 100%;
  85. padding: 15px;
  86. border: 1px solid #ddd;
  87. border-radius: 8px;
  88. margin-bottom: 15px;
  89. font-size: 16px;
  90. transition: border-color 0.2s ease;
  91. }
  92. #modern-uploader .form-control:focus {
  93. border-color: #4285f4;
  94. outline: none;
  95. }
  96. `);
  97.  
  98. const dăngnhanhTTV = {
  99. STATE: {
  100. CHAP_NUMBER: 1,
  101. CHAP_STT: 1,
  102. CHAP_SERIAL: 1,
  103. CHAP_NUMBER_ORIGINAL: 1,
  104. CHAP_STT_ORIGINAL: 1,
  105. CHAP_SERIAL_ORIGINAL: 1,
  106. AUTO_MODE: false
  107. },
  108.  
  109. ELEMENTS: {
  110. qpContent: null,
  111. qpButtonPaste: null,
  112. qpOptionAuto: null
  113. },
  114.  
  115. init: function() {
  116. try {
  117. console.log('[TTV-DEBUG] Script bắt đầu khởi tạo...');
  118. this.initializeChapterValues();
  119. this.createInterface();
  120. this.cacheElements();
  121. this.registerEvents();
  122. console.log('[TTV-DEBUG] Script đã khởi động thành công');
  123. showNotification('Công cụ đã chạy', 'success');
  124.  
  125. // Khôi phục trạng thái tự động
  126. const isAutoMode = localStorage.getItem('TTV_AUTO_MODE') === 'true';
  127. if (isAutoMode) {
  128. this.ELEMENTS.qpOptionAuto.prop('checked', true);
  129. this.STATE.AUTO_MODE = true;
  130. this.handlePasteButton(); // Tự động paste nếu đang ở chế độ tự động
  131. }
  132. } catch (e) {
  133. console.error('[TTV-ERROR] Lỗi khởi tạo:', e);
  134. showNotification('Có lỗi khi khởi tạo Script', 'error');
  135. }
  136. },
  137.  
  138. createInterface: function() {
  139. const html = `
  140. <div id="modern-uploader">
  141. <div class="text-center mb-4">
  142. <h3 style="color: #4285f4; margin-bottom: 15px; font-weight: 700; font-size: 18px;">📝 CÔNG C ĐĂNG NHANH</h3>
  143. </div>
  144. <div class="form-group">
  145. <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>
  146. </div>
  147. <div class="text-center mb-3">
  148. <label style="color: #bef385;">
  149. <input type="checkbox" id="qpOptionAuto" class="form-control" style="height:10px;width: 10px;display: inline-block;">
  150. Chế độ t động
  151. </label>
  152. </div>
  153. <div class="button-container" style="display: flex; justify-content: center; gap: 15px;">
  154. <button class="btn btn-primary" id="qpButtonPaste">📋 Paste</button>
  155. </div>
  156. <div class="notification-container"></div>
  157. </div>`;
  158.  
  159. jQuery(".list-in-user").before(html);
  160. },
  161.  
  162. initializeChapterValues: function() {
  163. try {
  164. const chap_number = parseInt(jQuery('#chap_number').val());
  165. let chap_stt = parseInt(jQuery('.chap_stt1').val());
  166. let chap_serial = parseInt(jQuery('.chap_serial').val());
  167.  
  168. if (parseInt(jQuery('#chap_stt').val()) > chap_stt) {
  169. chap_stt = parseInt(jQuery('#chap_stt').val());
  170. }
  171. if (parseInt(jQuery('#chap_serial').val()) > chap_serial) {
  172. chap_serial = parseInt(jQuery('#chap_serial').val());
  173. }
  174.  
  175. this.STATE.CHAP_NUMBER = this.STATE.CHAP_NUMBER_ORIGINAL = chap_number || 1;
  176. this.STATE.CHAP_STT = this.STATE.CHAP_STT_ORIGINAL = chap_stt || 1;
  177. this.STATE.CHAP_SERIAL = this.STATE.CHAP_SERIAL_ORIGINAL = chap_serial || 1;
  178. } catch (e) {
  179. console.error("Error initializing chapter values:", e);
  180. }
  181. },
  182.  
  183. cacheElements: function() {
  184. this.ELEMENTS.qpContent = jQuery("#qpContent");
  185. this.ELEMENTS.qpButtonPaste = jQuery("#qpButtonPaste");
  186. this.ELEMENTS.qpOptionAuto = jQuery("#qpOptionAuto");
  187. },
  188.  
  189. registerEvents: function() {
  190. this.ELEMENTS.qpContent.on("paste", this.handlePaste.bind(this));
  191. this.ELEMENTS.qpButtonPaste.on('click', this.handlePasteButton.bind(this));
  192. this.ELEMENTS.qpOptionAuto.on('change', this.toggleAutoMode.bind(this));
  193. setupCharacterCounter();
  194. },
  195.  
  196. toggleAutoMode: function() {
  197. this.STATE.AUTO_MODE = this.ELEMENTS.qpOptionAuto.prop('checked');
  198. localStorage.setItem('TTV_AUTO_MODE', this.STATE.AUTO_MODE);
  199.  
  200. if (this.STATE.AUTO_MODE) {
  201. showNotification('Đã bật chế độ tự động - sẽ tự động paste và đăng chương', 'info');
  202. this.handlePasteButton(); // Tự động paste khi bật chế độ tự động
  203. } else {
  204. showNotification('Đã tắt chế độ tự động', 'info');
  205. }
  206. },
  207.  
  208. handlePasteButton: function() {
  209. this.showLoading();
  210. navigator.clipboard.readText()
  211. .then(text => {
  212. this.ELEMENTS.qpContent.val(text);
  213. setTimeout(() => {
  214. this.performAction();
  215. this.hideLoading();
  216. }, 100);
  217. })
  218. .catch(err => {
  219. console.error('Không thể đọc dữ liệu từ clipboard:', err);
  220. this.hideLoading();
  221. showNotification('Không thể truy cập clipboard. Vui lòng dán trực tiếp vào ô nội dung.', 'error');
  222. });
  223. },
  224.  
  225. handlePaste: function(e) {
  226. e.preventDefault();
  227. this.ELEMENTS.qpContent.val("");
  228. this.showLoading();
  229. const pastedText = e.originalEvent.clipboardData.getData('text');
  230. this.ELEMENTS.qpContent.val(pastedText);
  231. setTimeout(() => {
  232. this.performAction();
  233. this.hideLoading();
  234. }, 100);
  235. },
  236.  
  237. performAction: function() {
  238. try {
  239. console.log("Starting performAction");
  240. var text = this.ELEMENTS.qpContent.val();
  241.  
  242. if (!text) {
  243. showNotification('Không có nội dung để tách chương', 'error');
  244. return;
  245. }
  246.  
  247. // Xử lý tách chương và điền form
  248. var chapters = this.splitChapters(text);
  249. if (chapters.length === 0) {
  250. showNotification('Không tìm thấy chương nào', 'error');
  251. return;
  252. }
  253.  
  254. // Lấy 10 chương đầu để điền vào form
  255. const chaptersToFill = chapters.slice(0, MAX_CHAPTER_POST);
  256. const remainingChapters = chapters.slice(MAX_CHAPTER_POST);
  257.  
  258. // Điền 10 chương đầu vào form
  259. this.fillChaptersToForm(chaptersToFill);
  260.  
  261. // Copy các chương còn lại vào clipboard nếu có
  262. if (remainingChapters.length > 0) {
  263. this.copyRemainingChapters(remainingChapters);
  264. }
  265.  
  266. // Nếu đang ở chế độ tự động, đợi 2 giây rồi đăng
  267. if (this.STATE.AUTO_MODE) {
  268. showNotification('Sẽ tự động đăng sau 2 giây...', 'info');
  269. setTimeout(() => {
  270. this.submitChapters();
  271. }, 2000);
  272. }
  273.  
  274. } catch (error) {
  275. console.error('Lỗi xử lý chương:', error);
  276. showNotification('Có lỗi khi xử lý các chương. Vui lòng thử lại.', 'error');
  277. }
  278. },
  279.  
  280. splitChapters: function(text) {
  281. var chapters = [];
  282. var lines = text.split('\n');
  283. var currentChapter = [];
  284. var lastTitle = null;
  285.  
  286. for (let i = 0; i < lines.length; i++) {
  287. let line = lines[i];
  288. let isChapterTitle = /^\t[Cc]hương\s*\d+\s*:/.test(line) || /^\s{4,}[Cc]hương\s*\d+\s*:/.test(line);
  289.  
  290. if (isChapterTitle) {
  291. if (currentChapter.length > 0) {
  292. if (line !== lastTitle) {
  293. chapters.push(currentChapter.join('\n'));
  294. currentChapter = [line];
  295. lastTitle = line;
  296. }
  297. } else {
  298. currentChapter = [line];
  299. lastTitle = line;
  300. }
  301. } else if (currentChapter.length > 0) {
  302. currentChapter.push(line);
  303. }
  304. }
  305.  
  306. if (currentChapter.length > 0) {
  307. chapters.push(currentChapter.join('\n'));
  308. }
  309.  
  310. return chapters;
  311. },
  312.  
  313. fillChaptersToForm: function(chapters) {
  314. var titles = jQuery("input[name^='chap_name']");
  315. var contents = jQuery("textarea[name^='introduce']");
  316. var advs = jQuery("textarea[name^='adv']");
  317.  
  318. // Thêm form cho đủ số chương cần thiết
  319. const neededForms = chapters.length - titles.length;
  320. if (neededForms > 0 && titles.length < MAX_CHAPTER_POST) {
  321. for (let i = 0; i < neededForms && (titles.length + i) < MAX_CHAPTER_POST; i++) {
  322. this.addNewChapter();
  323. }
  324. titles = jQuery("input[name^='chap_name']");
  325. contents = jQuery("textarea[name^='introduce']");
  326. advs = jQuery("textarea[name^='adv']");
  327. }
  328.  
  329. // Điền nội dung vào form
  330. jQuery.each(titles, function(k, v) {
  331. if (k < chapters.length) {
  332. var content = chapters[k].split('\n');
  333. var title = content.shift().trim();
  334. var chapterTitle = title;
  335. if (title.includes(':')) {
  336. chapterTitle = title.substring(title.indexOf(':') + 1).trim();
  337. }
  338. if (!chapterTitle || chapterTitle.trim() === '') {
  339. chapterTitle = "Vô đề";
  340. }
  341. titles[k].value = chapterTitle;
  342. contents[k].value = HEADER_SIGN + "\r\n" + content.join('\n') + "\r\n" + FOOTER_SIGN;
  343. if (advs[k]) advs[k].value = "";
  344. jQuery(contents[k]).trigger('input');
  345. }
  346. });
  347.  
  348. showNotification(`Đã đin ${chapters.length} chương vào form`, 'success');
  349. },
  350.  
  351. copyRemainingChapters: function(chapters) {
  352. try {
  353. const clipboardContent = chapters.map(chap => {
  354. const lines = chap.trim().split('\n');
  355. if (lines.length > 0 && !lines[0].startsWith('\t')) {
  356. lines[0] = '\t' + lines[0];
  357. }
  358. return lines.join('\n');
  359. }).join('\n\n---CHAPTER_SEPARATOR---\n\n');
  360.  
  361. navigator.clipboard.writeText(clipboardContent)
  362. .then(() => {
  363. showNotification(`Đã copy ${chapters.length} chương còn li vào clipboard`, 'success');
  364. })
  365. .catch(() => {
  366. showNotification('Không thể copy vào clipboard', 'error');
  367. });
  368. } catch (error) {
  369. console.error('Lỗi copy clipboard:', error);
  370. showNotification('Có lỗi khi copy các chương còn lại', 'error');
  371. }
  372. },
  373.  
  374. submitChapters: function() {
  375. // Kiểm tra nút submit
  376. const postButton = jQuery('button[type="submit"]');
  377. if (!postButton.length) {
  378. showNotification('Không tìm thấy nút đăng chương!', 'error');
  379. return;
  380. }
  381.  
  382. // Kiểm tra độ dài chương
  383. if (!validateChapterLengths()) {
  384. showNotification('Có chương chưa đủ độ dài tối thiểu (3000 ký tự)!', 'error');
  385. return;
  386. }
  387.  
  388. // Đăng chương
  389. postButton.click();
  390. showNotification('Đang đăng chương...', 'info');
  391.  
  392. // Nếu đang ở chế độ tự động, reload trang sau 5 giây
  393. if (this.STATE.AUTO_MODE) {
  394. setTimeout(() => {
  395. window.location.reload();
  396. }, 5000);
  397. }
  398. },
  399.  
  400. addNewChapter: function() {
  401. if ((this.STATE.CHAP_NUMBER + 1) <= MAX_CHAPTER_POST) {
  402. this.STATE.CHAP_NUMBER++;
  403. this.STATE.CHAP_STT++;
  404. this.STATE.CHAP_SERIAL++;
  405. var html = createChapterHTML(this.STATE.CHAP_NUMBER);
  406. jQuery('#div_chapt_upload').append(html);
  407. }
  408. },
  409.  
  410. showLoading: function() {
  411. jQuery(".loading-overlay").remove();
  412. var loading = jQuery("<div>", {
  413. class: "loading-overlay",
  414. css: {
  415. position: "fixed",
  416. top: "0",
  417. left: "0",
  418. width: "100%",
  419. height: "100%",
  420. backgroundColor: "rgba(0, 0, 0, 0.5)",
  421. zIndex: "9999",
  422. display: "flex",
  423. justifyContent: "center",
  424. alignItems: "center"
  425. }
  426. });
  427. loading.append(`
  428. <div style="
  429. background-color: white;
  430. padding: 20px;
  431. border-radius: 10px;
  432. text-align: center;
  433. ">
  434. <div style="
  435. border: 4px solid #f3f3f3;
  436. border-top: 4px solid #3498db;
  437. border-radius: 50%;
  438. width: 40px;
  439. height: 40px;
  440. margin: 0 auto 10px;
  441. animation: spin 1s linear infinite;
  442. "></div>
  443. <divang x lý...</div>
  444. </div>
  445. `);
  446. jQuery("body").append(loading);
  447. jQuery("head").append(`
  448. <style>
  449. @keyframes spin {
  450. 0% { transform: rotate(0deg); }
  451. 100% { transform: rotate(360deg); }
  452. }
  453. </style>
  454. `);
  455. },
  456.  
  457. hideLoading: function() {
  458. jQuery(".loading-overlay").remove();
  459. }
  460. };
  461.  
  462. function showNotification(message, type) {
  463. jQuery('#modern-uploader .notification-container').remove();
  464. const container = jQuery("<div>", {
  465. class: "notification-container",
  466. css: {
  467. width: "100%",
  468. padding: "10px 0",
  469. marginTop: "10px",
  470. textAlign: "left",
  471. borderTop: "1px solid rgba(0,0,0,0.1)"
  472. }
  473. });
  474. const notification = jQuery("<div>", {
  475. class: `notification-${type}`,
  476. css: {
  477. backgroundColor: type === 'success' ? "#e8f5e9" : (type === 'error' ? "#ffebee" : "#fff8e1"),
  478. color: type === 'success' ? "#000000" : (type === 'error' ? "#d32f2f" : "#ff9800"),
  479. padding: "10px 15px",
  480. borderRadius: "8px",
  481. fontSize: "14px",
  482. fontWeight: "500",
  483. boxShadow: "0 4px 10px rgba(0,0,0,0.15)",
  484. display: "inline-block",
  485. maxWidth: "90%",
  486. margin: "0",
  487. wordBreak: "break-word",
  488. border: type === 'success' ? "1px solid #81c784" : (type === 'error' ? "1px solid #d32f2f" : "1px solid #ff9800")
  489.  
  490. }
  491. });
  492. const lines = message.split('\n');
  493. lines.forEach((line, index) => {
  494. notification.append(jQuery("<div>").html(line));
  495. });
  496. container.append(notification);
  497. jQuery("#modern-uploader .button-container").after(container);
  498. notification.fadeIn(300);
  499. }
  500.  
  501. function createChapterHTML(chapNum) {
  502. const chap_vol = parseInt(jQuery('.chap_vol').val()) || 1;
  503. const chap_vol_name = jQuery('.chap_vol_name').val() || '';
  504. return `
  505. <div data-gen="MK_GEN" id="COUNT_CHAP_${chapNum}_MK">
  506. <div class="col-xs-12 form-group"></div>
  507. <div class="form-group">
  508. <label class="col-sm-2" for="chap_stt">STT</label>
  509. <div class="col-sm-8">
  510. <input class="form-control" required name="chap_stt[${chapNum}]" value="${dăngnhanhTTV.STATE.CHAP_STT}" placeholder="Số thứ tự của chương" type="text"/>
  511. </div>
  512. </div>
  513. <div class="form-group">
  514. <label class="col-sm-2" for="chap_number">Chương thứ..</label>
  515. <div class="col-sm-8">
  516. <input value="${dăngnhanhTTV.STATE.CHAP_SERIAL}" required class="form-control" name="chap_number[${chapNum}]" placeholder="Chương thứ.. (1,2,3..)" type="text"/>
  517. </div>
  518. </div>
  519. <div class="form-group">
  520. <label class="col-sm-2" for="chap_name">Quyn số</label>
  521. <div class="col-sm-8">
  522. <input class="form-control" name="vol[${chapNum}]" placeholder="Quyển số" type="number" value="${chap_vol}" required/>
  523. </div>
  524. </div>
  525. <div class="form-group">
  526. <label class="col-sm-2" for="chap_name">Tên quyn</label>
  527. <div class="col-sm-8">
  528. <input class="form-control chap_vol_name" name="vol_name[${chapNum}]" placeholder="Tên quyển" type="text" value="${chap_vol_name}" />
  529. </div>
  530. </div>
  531. <div class="form-group">
  532. <label class="col-sm-2" for="chap_name">Tên chương</label>
  533. <div class="col-sm-8">
  534. <input required class="form-control" name="chap_name[${chapNum}]" placeholder="Tên chương" type="text"/>
  535. </div>
  536. </div>
  537. <div class="form-group">
  538. <label class="col-sm-2" for="introduce">Ni dung</label>
  539. <div class="col-sm-8">
  540. <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>
  541. <div class="chapter-character-count"></div>
  542. </div>
  543. </div>
  544. <div class="form-group">
  545. <label class="col-sm-2" for="adv">Qung cáo</label>
  546. <div class="col-sm-8">
  547. <textarea maxlength="1000" class="form-control" name="adv[${chapNum}]" placeholder="Quảng cáo" type="text"></textarea>
  548. </div>
  549. </div>
  550. </div>`;
  551. }
  552.  
  553. function setupCharacterCounter() {
  554. jQuery(document).on("input", "[name^=introduce]", function() {
  555. const text = jQuery(this).val();
  556. const charCount = text.length;
  557. let charCountElement = jQuery(this).next('.chapter-character-count');
  558. if (charCountElement.length === 0) {
  559. charCountElement = jQuery('<div class="chapter-character-count"></div>');
  560. jQuery(this).after(charCountElement);
  561. }
  562. if(charCount < 3000) {
  563. jQuery(this).addClass('short-chapter');
  564. charCountElement.html(`<span class="short-chapters-warning">${charCount.toLocaleString()}/40.000 ký tự</span>`);
  565. } else {
  566. jQuery(this).removeClass('short-chapter');
  567. if(charCount > 40000) {
  568. charCountElement.html(`<span style="color: #fbbc05;">${charCount.toLocaleString()}/40.000 ký tự</span>`);
  569. } else {
  570. charCountElement.html(`<span style="color: #34a853;">${charCount.toLocaleString()}/40.000 ký tự</span>`);
  571. }
  572. }
  573. });
  574. }
  575.  
  576. function validateChapterLengths() {
  577. let hasError = false;
  578. jQuery('form[name="postChapForm"] .chapter-detail').each(function() {
  579. const form = this;
  580. const contentTextarea = form.querySelector('textarea[name^="introduce"]');
  581. const content = contentTextarea.value;
  582. if (content.length < 3000) {
  583. jQuery(contentTextarea).addClass('short-chapter');
  584. let warningIcon = form.querySelector('.warning-icon');
  585. if (!warningIcon) {
  586. warningIcon = document.createElement('div');
  587. warningIcon.className = 'warning-icon';
  588. warningIcon.innerHTML = '⚠️';
  589. contentTextarea.parentNode.appendChild(warningIcon);
  590. }
  591. hasError = true;
  592. } else {
  593. jQuery(contentTextarea).removeClass('short-chapter');
  594. const warningIcon = form.querySelector('.warning-icon');
  595. if (warningIcon) {
  596. warningIcon.remove();
  597. }
  598. }
  599. });
  600. return !hasError;
  601. }
  602.  
  603. dăngnhanhTTV.init();
  604. })();