AO3: [Wrangling] Mark Illegal Characters in Canonicals

Warns about any canonical tag that includes characters which should, per guidelines, be avoided. Checks on new tag, edit tag, search results, wrangle bins, and tag landing pages

当前为 2023-07-29 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name AO3: [Wrangling] Mark Illegal Characters in Canonicals
  3. // @namespace https://greasyfork.org/en/users/906106-escctrl
  4. // @version 1.0
  5. // @description Warns about any canonical tag that includes characters which should, per guidelines, be avoided. Checks on new tag, edit tag, search results, wrangle bins, and tag landing pages
  6. // @author escctrl
  7. // @match *://*.archiveofourown.org/tags/*
  8. // @license MIT
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // we wanna check on a bunch of different pages, and everywhere the check is slightly different
  16.  
  17. var page_url = window.location.pathname;
  18. // just in case the URL ended with a / we get rid of that
  19. // that usually doesn't happen from AO3 links on the site, but may be how browsers store bookmarks or history
  20. if (page_url.endsWith("/")) { page_url = page_url.slice(0, page_url.length-1); }
  21.  
  22. if (page_url == "/tags/new") checkAsYouType(); // New Tag page
  23. else if (page_url == "/tags/search") checkSearchResults(); // Tag Search page
  24. else if (page_url.match(/^\/tags\/.+\/edit$/gi)) checkEditTag(); // Edit page
  25. else if (page_url.match(/^\/tags\/.+\/wrangle$/gi)) checkBinTags(); // Wrangle page
  26. else if (page_url.match(/^\/tags\/[^\/]+$/gi)) checkTag(); // Tag Landing page
  27. // that excludes anything including another slash, which would only incorrectly match on tags/new and tags/search
  28. // but those would have already jumped into the other functions and would never get here
  29. })();
  30.  
  31. // *************** GENERAL FUNCTIONS ***************
  32.  
  33. // a holistic function to check
  34. // not allowed: non-latin (including accented) characters and special chars (with a few exceptions)
  35. // two apostrophes '' (used instead of a quote ")
  36. // space at the beginning or end of the string
  37. // this returns the matched characters in an array
  38. function hasIllegalChars(string) {
  39. return string.match(/[^\p{Script=Latin}0-9 \-().&/'"|:!]|'{2,}|^ | $/gui);
  40. }
  41.  
  42. // similar to above, but in fandoms we allow letters, numbers and tone/accent marks of ANY script, not just Latin
  43. // also more special characters are allowed
  44. function hasFandomIllegalChars(string) {
  45. return string.match(/[^\p{L}\p{M}\p{N} \-().&/'"|:!#?_]|'{2,}|^ | $/gui);
  46. }
  47.  
  48. // print a box to explain the problem
  49. function insertHeadsUp(illegalChars, refNode, befNode = null, inline = false) {
  50. // describe non-printable chars and other hard to identify issues
  51. illegalChars.forEach((val, ix) => {
  52. if (val == "''") illegalChars[ix] = "2 single quotes";
  53. if (val == "\t") illegalChars[ix] = "tab";
  54. if (val === " " && ix === 0) illegalChars[ix] = "space in front";
  55. if (val === " " && ix == illegalChars.length-1) illegalChars[ix] = "space at end";
  56. });
  57. // setting up the div to contain the heads-up to the user
  58. const warningNode = document.createElement("div");
  59. warningNode.id = "illegalChars";
  60. warningNode.classList.add("notice");
  61.  
  62. warningNode.innerHTML = "<p>Questionable characters: " + illegalChars.join(", ") + "</p>";
  63.  
  64. if (inline) {
  65. warningNode.style.display = "inline-block";
  66. warningNode.style.padding = "0";
  67. warningNode.style.margin = "0.1em 0.1em 0.1em 0.5em";
  68. warningNode.children[0].style.padding = "0.1em 0.3em";
  69. warningNode.children[0].style.fontWeight = "normal";
  70. }
  71.  
  72. // if that already exists, we're gonna replace it rather than add more divs
  73. if (refNode.querySelector("#illegalChars")) refNode.replaceChild(warningNode, refNode.querySelector("#illegalChars"));
  74. else refNode.insertBefore(warningNode, befNode);
  75. }
  76.  
  77. // remove the explain box again
  78. function removeHeadsUp(refNode) {
  79. if (refNode.querySelector("#illegalChars")) refNode.removeChild(refNode.querySelector("#illegalChars"));
  80. }
  81.  
  82. // *************** PAGE HANDLING FUNCTIONS ***************
  83.  
  84. // New tag page
  85. function checkAsYouType() {
  86. // a little JS magic to quickly add the same event listener to all elements
  87. [ document.getElementById("tag_name"),
  88. document.getElementById('tag_type_fandom'),
  89. document.getElementById('tag_type_character'),
  90. document.getElementById('tag_type_relationship'),
  91. document.getElementById('tag_type_freeform')
  92. ].forEach((el) => {
  93. el.addEventListener("input", () => {
  94. var checkNode = document.getElementById("tag_name");
  95.  
  96. // which tag type are you trying to create? fandom or anything else?
  97. const isFandom = document.getElementById('tag_type_fandom').checked;
  98. var issues = (isFandom) ? hasFandomIllegalChars(checkNode.value) : hasIllegalChars(checkNode.value);
  99. if (issues !== null) insertHeadsUp(issues, checkNode.parentNode);
  100. else removeHeadsUp(checkNode.parentNode);
  101.  
  102. // extra special handling: tag length>100 error
  103. const refNode = checkNode.parentNode;
  104. if (checkNode.value.length > 100) {
  105. const errorNode = document.createElement("div");
  106. errorNode.id = "tooLong";
  107. errorNode.classList.add("error");
  108. errorNode.innerHTML = "<p>Sorry, you'll need to trim this down. You're at "+ checkNode.value.length +" characters!</p>";
  109.  
  110. // if that already exists, we're gonna replace it rather than add more divs
  111. if (refNode.querySelector("#tooLong")) refNode.replaceChild(errorNode, refNode.querySelector("#tooLong"));
  112. else refNode.insertBefore(errorNode, null);
  113. }
  114. else if (refNode.querySelector("#tooLong")) refNode.removeChild(refNode.querySelector("#tooLong"));
  115. });
  116. });
  117. // on page load, trigger event once. browser remembers previous form selections/input upon page refresh and box would otherwise not appear until another change is made
  118. document.getElementById("tag_name").dispatchEvent(new Event("input"));
  119. }
  120.  
  121. // Landing page
  122. function checkTag() {
  123. // only if the viewed tags is canonical
  124. var tagDescr = document.querySelector(".tag>p").innerText;
  125. if (tagDescr.indexOf("It's a common tag") < 0) return true;
  126.  
  127. // first the viewed tag itself
  128. var checkNode = document.querySelector(".tag .header h2.heading");
  129. var tagType = tagDescr.match(/This tag belongs to the (.+) Category/i);
  130. tagType = tagType[1];
  131. var issues = (tagType == "Fandom") ? hasFandomIllegalChars(checkNode.innerText) : hasIllegalChars(checkNode.innerText);
  132. if (issues !== null) insertHeadsUp(issues, checkNode.parentNode.parentNode, checkNode.parentNode.parentNode.children[1]);
  133.  
  134. // then the meta and subtags (if any)
  135. checkNode = document.querySelectorAll("div.meta.listbox a, div.sub.listbox a");
  136. checkNode.forEach((n) => {
  137. var issues = (tagType == "Fandom") ? hasFandomIllegalChars(n.innerText) : hasIllegalChars(n.innerText);
  138. if (issues !== null) insertHeadsUp(issues, n.parentNode, n.parentNode.children[1], true);
  139. });
  140. // it would be really cool if we could check Parent Tags as well, but we can't tell which of those are fandoms vs. anything else
  141. }
  142.  
  143. // Wrangle Bin Page
  144. // sadly we can't tell here at all if we're ever looking at fandoms
  145. function checkBinTags() {
  146. // this needs a different approach to the logic:
  147. // don't check show=mergers at all, too repetitive
  148. var searchParams = new URLSearchParams(window.location.search);
  149. if (searchParams.get('show') == "mergers") return true;
  150.  
  151. // create a key -> value pair Map of the table columns, so we know which column to check
  152. var tableIndexes = new Map();
  153. document.querySelectorAll("#wrangulator table thead th").forEach((th, ix) => {
  154. tableIndexes.set(th.innerText, ix);
  155. });
  156. console.log(tableIndexes);
  157.  
  158. // now we can loop through the list of tags
  159. var issues, checkNode;
  160. var checkRows = document.querySelectorAll("#wrangulator table tbody tr");
  161. checkRows.forEach((r) => {
  162. // if there's a column "Canonical" and the cell says "Yes" then we check the tag itself
  163. if (tableIndexes.has("Canonical") && r.cells[tableIndexes.get("Canonical")].innerText == "Yes") {
  164. checkNode = r.cells[0].querySelector("label");
  165. issues = hasIllegalChars(checkNode.innerText);
  166. if (issues !== null) insertHeadsUp(issues, checkNode.parentNode);
  167. }
  168.  
  169. // if there's a column "Synonym", we check the content of that cell (there'll only be one tag)
  170. if (tableIndexes.has("Synonym") && r.cells[tableIndexes.get("Synonym")].innerText !== "") {
  171. checkNode = r.cells[tableIndexes.get("Synonym")].querySelector("a");
  172. issues = hasIllegalChars(checkNode.innerText);
  173. if (issues !== null) insertHeadsUp(issues, checkNode.parentNode);
  174. }
  175.  
  176. // if there's a column "Characters", we check the content of that cell (there might be multiple tags)
  177. if (tableIndexes.has("Characters") && r.cells[tableIndexes.get("Characters")].innerText !== "") {
  178. checkNode = r.cells[tableIndexes.get("Characters")].querySelectorAll("a");
  179. checkNode.forEach((n) => {
  180. issues = hasIllegalChars(n.innerText);
  181. if (issues !== null) insertHeadsUp(issues, n.parentNode);
  182. });
  183. }
  184.  
  185. // if there's a column "Metatag", we check the content of that cell (there might be multiple tags)
  186. if (tableIndexes.has("Metatag") && r.cells[tableIndexes.get("Metatag")].innerText !== "") {
  187. checkNode = r.cells[tableIndexes.get("Metatag")].querySelectorAll("a");
  188. checkNode.forEach((n) => {
  189. issues = hasIllegalChars(n.innerText);
  190. if (issues !== null) insertHeadsUp(issues, n.parentNode);
  191. });
  192. }
  193. });
  194. }
  195.  
  196. // Tag Search
  197. function checkSearchResults() {
  198. // with search results table userscript enabled
  199. var checkNodes = document.querySelectorAll("table#resulttable .resulttag.canonical a");
  200. checkNodes.forEach((n) => {
  201. var issues = (n.parentNode.parentNode.querySelector('td.resulttype').title == "Fandom") ? hasFandomIllegalChars(n.innerText) : hasIllegalChars(n.innerText);
  202. if (issues !== null) insertHeadsUp(issues, n.parentNode, null, true);
  203. });
  204.  
  205. // with plain search results page
  206. checkNodes = document.querySelectorAll("ol.tag li span.canonical a.tag");
  207. checkNodes.forEach((n) => {
  208. var issues = (n.parentNode.firstChild.textContent.trim() == "Fandom:") ? hasFandomIllegalChars(n.innerText) : hasIllegalChars(n.innerText);
  209. if (issues !== null) insertHeadsUp(issues, n.parentNode.parentNode, null, true);
  210. });
  211. }
  212.  
  213. // Edit Tag Page
  214. function checkEditTag() {
  215. const tagCanonical = document.getElementById('tag_canonical');
  216. const tagType = document.querySelector('#edit_tag fieldset:first-of-type dd strong').innerText;
  217.  
  218. // initial check only if the tag is already canonical
  219. if (tagCanonical.checked) {
  220. var checkNode = document.getElementById("tag_name");
  221. var issues = hasIllegalChars(checkNode.value);
  222. if (issues !== null) insertHeadsUp(issues, checkNode.parentNode);
  223. }
  224.  
  225. // if the tag's canonical status is changed
  226. tagCanonical.addEventListener("input", (event) => {
  227. var checkNode = document.getElementById("tag_name");
  228. if (event.target.checked) {
  229. var issues = (tagType == "Fandom") ? hasFandomIllegalChars(checkNode.value) : hasIllegalChars(checkNode.value);
  230. if (issues !== null) insertHeadsUp(issues, checkNode.parentNode);
  231. else removeHeadsUp(checkNode.parentNode);
  232. }
  233. else removeHeadsUp(checkNode.parentNode);
  234. });
  235.  
  236. // if this is a synonym, check the canonical tag it's synned to
  237. const synonym = document.querySelector('#edit_tag fieldset:first-of-type dd ul.autocomplete .added.tag');
  238. if (synonym !== null) {
  239. issues = (tagType == "Fandom") ? hasFandomIllegalChars(synonym.firstChild.textContent.trim()) : hasIllegalChars(synonym.firstChild.textContent.trim());
  240. if (issues !== null) insertHeadsUp(issues, synonym.parentNode.parentNode, synonym.parentNode.parentNode.children[1]);
  241. }
  242.  
  243. // if this is canonical, check its sub- and metatags
  244. const metasubs = document.querySelectorAll('#parent_MetaTag_associations_to_remove_checkboxes ul li a, #child_SubTag_associations_to_remove_checkboxes ul li a');
  245. if (metasubs !== null) {
  246. metasubs.forEach((n) => {
  247. var issues = (tagType == "Fandom") ? hasFandomIllegalChars(n.innerText) : hasIllegalChars(n.innerText);
  248. if (issues !== null) insertHeadsUp(issues, n.parentNode);
  249. });
  250. }
  251.  
  252. // if this is any other type of tag that's in a fandom, check the fandom tag
  253. const fandoms = document.querySelectorAll('#parent_Fandom_associations_to_remove_checkboxes ul li a');
  254. if (fandoms !== null) {
  255. fandoms.forEach((n) => {
  256. var issues = hasFandomIllegalChars(n.innerText);
  257. if (issues !== null) insertHeadsUp(issues, n.parentNode);
  258. });
  259. }
  260.  
  261. // if this is a relationship, check the tagged characters
  262. const chars = document.querySelectorAll('#parent_Character_associations_to_remove_checkboxes ul li a');
  263. if (chars !== null) {
  264. chars.forEach((n) => {
  265. var issues = hasIllegalChars(n.innerText);
  266. if (issues !== null) insertHeadsUp(issues, n.parentNode);
  267. });
  268. }
  269. }