Add non-MFC figure

Track preordered non-MFC items on collection screen

当前为 2020-07-16 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Add non-MFC figure
  3. // @namespace https://tharglet.me.uk
  4. // @version 1.0
  5. // @description Track preordered non-MFC items on collection screen
  6. // @author Tharglet
  7. // @match https://myfigurecollection.net/users.v4.php?*mode=view&*tab=collection&*
  8. // @grant GM_setValue
  9. // @grant GM_getValue
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14. var additionalFigures = JSON.parse(GM_getValue("additionalFigures", "[]"));
  15. var drawAdditionalFigures = () => {
  16. const urlParams = new URLSearchParams(window.location.search);
  17. const sortType = urlParams.get("sort");
  18. const output = urlParams.get("output");
  19. //Clear all additions before redo
  20. $("#nonMfcAdditions").remove();
  21. $(".nonMfcIcon").remove();
  22. $(".nonMfcDelete").remove();
  23. $(".nonMfcAddedyear").remove();
  24. if(additionalFigures.length > 0) {
  25. //Pre-add setup
  26. const addList = $("#nonMfcFigureList");
  27. let dateHeadings;
  28. if(sortType == "groupByReleaseDates") {
  29. if(output === "0") {
  30. dateHeadings = $("li.listing-title");
  31. } else {
  32. dateHeadings = $(".listing-item h3");
  33.  
  34. }
  35. } else if(sortType.startsWith("groupBy")) {
  36. if(output === "0") {
  37. $(".listing-toggles:last").after('<li id="nonMfcAdditions" class="listing-title">Non-MFC</li>');
  38. } else {
  39. $(".listing-item .item-icons").prepend('<div class="item-group-by" id="nonMfcAdditions"><h3>Non-MFC</h3></div>');
  40. }
  41.  
  42. }
  43. //Add icons to page and delete box
  44. additionalFigures.forEach((fig, idx) => {
  45. let linkLine;
  46. const figYear = fig.date.substring(0, 4);
  47. const figMonth = fig.date.substring(5);
  48. const figureThumb = `<span class="item-icon nonMfcIcon">
  49. <a href="${fig.link}" class="tbx-tooltip item-root-0 item-category-1">
  50. <img src="${fig.image}">
  51. </a>
  52. </span>`;
  53. if(output === "0") {
  54. linkLine = `<div class='listing-item nonMfcIcon'><div class="stamp item-stamp">
  55. <a href="/item/872779" class="tbx-tooltip">
  56. <img class="stamp-icon" src="${fig.image}"</a><div class="stamp-anchor"><a class="tbx-tooltip item-category-1" href="${fig.link}">${fig.name}</a>
  57. </div></div></div>`
  58. }
  59. addList.append(figureThumb + `<a href="#" class='nonMfcDelete' title="Delete" data-index="${idx}"><span class="tiny-icon-only icon-trash-o" data-index="${idx}"></span></a>`);
  60. let toAppend = true;
  61. if(sortType == "groupByReleaseDates") {
  62. dateHeadings.each((idx, heading) => {
  63. if(toAppend) {
  64. const headingYear = $(heading).text().substring(0,4);
  65. const headingMonth = $(heading).text().substring(5);
  66. if(figYear == headingYear && figMonth == headingMonth) {
  67. if(output === "0") {
  68. $(heading).after(linkLine);
  69. } else {
  70. $(heading).after(figureThumb);
  71. }
  72. toAppend = false;
  73. } else if(new Date(figYear, figMonth, 1) > new Date(headingYear, headingMonth, 1)) {
  74. if(output === "0") {
  75. const newHeading = `<li class="listing-title nonMfcAddedyear" id='nhi_${idx}'>${figYear}-${figMonth}</li></h3>`
  76. $(heading).before(newHeading + linkLine);
  77. } else {
  78. const newHeading = `<h3 class="nonMfcAddedyear" id='nhi_${idx}'>${figYear}-${figMonth}</h3>`
  79. $(heading).before(newHeading + figureThumb);
  80. }
  81. dateHeadings.splice(idx, 0, $(`#nhi_${idx}`));
  82. toAppend = false;
  83. }
  84. }
  85. });
  86. } else if(sortType.startsWith("groupBy")) {
  87. if(output === "0") {
  88. $("#nonMfcAdditions").after(linkLine)
  89. } else {
  90. $("#nonMfcAdditions h3").after(figureThumb);
  91. }
  92. } else {
  93. if(output === "0") {
  94. $(".listing-toggles:last").after(linkLine);
  95. } else {
  96. $(".listing-item .item-icons").prepend(figureThumb);
  97. }
  98. }
  99. });
  100. $('.nonMfcDelete').click((evt) => {
  101. evt.preventDefault();
  102. if(confirm("Delete this figure?")) {
  103. additionalFigures.splice($(evt.target).attr("data-index"), 1);
  104. additionalFigures.sort((a, b) => (a.date < b.date) ? 1 : -1);
  105. GM_setValue("additionalFigures", JSON.stringify(additionalFigures));
  106. drawAdditionalFigures();
  107. }
  108. });
  109. }
  110. }
  111. $().ready(() => {
  112. const addSection = `<section>
  113. <h2>Add non-MFC figure</h2>
  114. <div class='form'>
  115. <div class='bigchar form-field'>
  116. <div class='form-label'>Figure name</div>
  117. <div class='form-input'><input type='text' id='nonMfcItemName'/></div>
  118. </div>
  119. <div class='bigchar form-field'>
  120. <div class='form-label'>Image URL *</div>
  121. <div class='form-input'><input type='text' id='nonMfcItemImage'/></div>
  122. </div>
  123. <div class='bigchar form-field'>
  124. <div class='form-label'>Item link *</div>
  125. <div class='form-input'><input type='text' id='nonMfcItemLink'/></div>
  126. </div>
  127. <div class='bigchar form-field'>
  128. <div class='form-label'>Release date (YYYY-MM) *</div>
  129. <div class='form-input'><input type='text' id='nonMfcItemReleaseDate'/></div>
  130. </div>
  131. <div class='form-field'>
  132. <div class='form-input'>
  133. <button id='addNonMfcItem'>Add</button>
  134. </div>
  135. </div>
  136. </div>
  137. </section>
  138. <section>
  139. <h2>Added non-MFC figures</h2>
  140. <div class='form'>
  141. <div id='nonMfcFigureList' class='item-icons'>
  142. </div>
  143. </div>
  144. </section>`
  145. $("#side section:last").after(addSection);
  146. drawAdditionalFigures();
  147. $("#addNonMfcItem").click((e) => {
  148. e.preventDefault();
  149. const name = $("#nonMfcItemName").val() || "Non-MFC figure";
  150. const image = $("#nonMfcItemImage").val();
  151. const link = $("#nonMfcItemLink").val();
  152. const date = $("#nonMfcItemReleaseDate").val();
  153. if(image && link && date && name) {
  154. if(date.match(/^\d{4}-0[1-9]|1[0-2]$/)) {
  155. additionalFigures.push({
  156. name: name,
  157. image: image,
  158. link: link,
  159. date: date
  160. });
  161. additionalFigures.sort((a, b) => (a.date < b.date) ? 1 : -1);
  162. GM_setValue("additionalFigures", JSON.stringify(additionalFigures));
  163. drawAdditionalFigures();
  164. $("#nonMfcItemName").val("");
  165. $("#nonMfcItemImage").val("");
  166. $("#nonMfcItemLink").val("");
  167. $("#nonMfcItemReleaseDate").val("");
  168. } else {
  169. alert("Please enter a valid date in YYYY-MM format");
  170. }
  171. } else {
  172. alert("Please fill in all mandatory fields");
  173. }
  174. });
  175. });
  176. })();