GGn E-Books upload assistant

Scans the filename for month, year, issue, format, language and fills in info based on that

  1. // ==UserScript==
  2. // @name GGn E-Books upload assistant
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description Scans the filename for month, year, issue, format, language and fills in info based on that
  6. // @author fordtransit
  7. // @match https://gazellegames.net/upload.php*
  8. // @grant GM.xmlHttpRequest
  9. // @grant GM_xmlhttpRequest
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14. const MonthsToFind = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december', 'christmas']; // Possible additions could be Summer, Fall, Spring, Easter, ...
  15. const languageMap = {
  16. 'en': 'English',
  17. 'fr': 'French',
  18. 'es': 'Spanish',
  19. 'de': 'German',
  20. 'it': 'Italian',
  21. 'pt': 'Portuguese',
  22. 'cz': 'Czech',
  23. 'zh': 'Chinese',
  24. 'ja': 'Japanese',
  25. 'ko': 'Korean',
  26. 'pl': 'Polish',
  27. 'ru': 'Russian'
  28. };
  29. const fileFormats = ['pdf', 'epub', 'mobi', 'cbz', 'cbr', 'cb7', 'azw3'];
  30.  
  31. function performScript() {
  32. let FoundMonth = [];
  33. let FoundYear = [];
  34. let FullTitle = [];
  35. let FoundLanguages = [];
  36. let FoundFileFormats = [];
  37. let Issue = [];
  38.  
  39. const Title = document.getElementById('title').value;
  40. const fileInputValue = $("#file").val().toLowerCase();
  41.  
  42. MonthsToFind.forEach(month => {
  43. const capitalizedMonth = month.charAt(0).toUpperCase() + month.slice(1);
  44. if (fileInputValue.includes(month.toLowerCase())) {
  45. FoundMonth.push(capitalizedMonth);
  46. }
  47. });
  48.  
  49. const yearPattern = /\b(19|20)\d{2}\b/g;
  50. let yearmatch;
  51.  
  52. while ((yearmatch = yearPattern.exec(fileInputValue)) !== null) {
  53. const year = parseInt(yearmatch[0]);
  54. FoundYear.push(year);
  55. }
  56.  
  57. const regex = /\((.*?)\)/g;
  58. let match;
  59.  
  60. while ((match = regex.exec(fileInputValue)) !== null) {
  61. const abbreviation = match[1].toLowerCase();
  62. if (languageMap[abbreviation]) {
  63. FoundLanguages.push(languageMap[abbreviation]);
  64. }
  65. }
  66.  
  67. fileFormats.forEach(format => {
  68. if (fileInputValue.includes(format)) {
  69. FoundFileFormats.push(format.toUpperCase());
  70. }
  71. });
  72.  
  73. // Issue number patterns
  74. const numberPattern = /\b\d{3,}\b/g; // At least 3 digits
  75. const hashPattern = /#(\d+)/g; // Digits after #
  76. const issuePattern = /Issue\s+(\d+)/gi; // Digits after Issue
  77.  
  78. function formatIssueNumber(number) {
  79. return number.padStart(3, '0');
  80. }
  81.  
  82. function isValidIssue(number) {
  83. const num = parseInt(number);
  84. return !FoundYear.includes(num);
  85. }
  86.  
  87. while ((match = numberPattern.exec(fileInputValue)) !== null) {
  88. const number = match[0];
  89. if (isValidIssue(number)) {
  90. Issue.push(formatIssueNumber(number));
  91. }
  92. }
  93.  
  94. while ((match = hashPattern.exec(fileInputValue)) !== null) {
  95. const number = match[1];
  96. if (isValidIssue(number)) {
  97. Issue.push(formatIssueNumber(number));
  98. }
  99. }
  100.  
  101. while ((match = issuePattern.exec(fileInputValue)) !== null) {
  102. const number = match[1];
  103. if (isValidIssue(number)) {
  104. Issue.push(formatIssueNumber(number));
  105. }
  106. }
  107.  
  108. Issue = [...new Set(Issue)];
  109.  
  110. if (FoundMonth.length > 0 && FoundYear.length > 0) {
  111. FullTitle = Title + " (" + FoundMonth.join(", ") + " " + FoundYear.join(", ") + ")";
  112. document.getElementById('remaster').checked = !document.getElementById('remaster').checked;
  113. document.getElementById('remaster_true').classList.remove('hidden');
  114. $("#remaster_year").val(FoundYear.join(", "));
  115. } else if (FoundYear.length > 0) {
  116. FullTitle = Title + " (" + FoundYear.join(", ") + ")";
  117. document.getElementById('remaster').checked = !document.getElementById('remaster').checked;
  118. document.getElementById('remaster_true').classList.remove('hidden');
  119. $("#remaster_year").val(FoundYear.join(", "));
  120. } else {
  121. FullTitle = Title;
  122. }
  123.  
  124. $("#release_title").val(FullTitle);
  125.  
  126. if (FoundLanguages.length > 0) {
  127. const detectedLanguage = FoundLanguages[0];
  128. $("#language").val(detectedLanguage);
  129. } else {
  130. $("#language").val('English');
  131. }
  132.  
  133. if (FoundFileFormats.length > 0) {
  134. const detectedFormat = FoundFileFormats[0];
  135. $("#format").val(detectedFormat);
  136. }
  137.  
  138. if (Issue.length > 0) {
  139. const detectedIssue = Issue[0];
  140. $("#issue").val(detectedIssue);
  141. }
  142. }
  143.  
  144. $("#file").on('change', function() {
  145. if ($("#categories").val() === 'E-Books') {
  146. performScript();
  147. }
  148. });
  149.  
  150. })();