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

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