Mangadex Toggle Tags

Hide or shows tags for spoiler purposes

  1. // ==UserScript==
  2. // @name Mangadex Toggle Tags
  3. // @namespace https://github.com/ToostInc/userscripts
  4. // @description Hide or shows tags for spoiler purposes
  5. // @include /^https?:\/\/mangadex\.org\/title\/.*$/
  6. // @author Joost Bremmer < contact at made of magic and wires dot online >
  7. // @copyright 2019, Joost Bremmer
  8. // @license MIT
  9. // @version 1.0.6
  10. // @date 2019-09-28
  11. // @grant none
  12. // @runat document-end
  13. // ==/UserScript==
  14.  
  15. /**
  16. * Mangadex Toggle Tags
  17. * Copyright © 2019 Joost Bremmer
  18. *
  19. * Permission is hereby granted, free of charge, to any person obtaining
  20. * a copy of this software and associated documentation files (the "Software"),
  21. * to deal in the Software without restriction, including without limitation
  22. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  23. * and/or sell copies of the Software, and to permit persons to whom the
  24. * Software is furnished to do so, subject to the following conditions:
  25. *
  26. * The above copyright notice and this permission notice shall be included
  27. * in all copies or substantial portions of the Software.
  28. *
  29. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  30. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  31. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  32. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  33. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  34. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  35. * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  36. */
  37.  
  38. "use strict";
  39.  
  40.  
  41. /**
  42. * Toggles the visibility state of tags
  43. *
  44. * @param {HTMLElement} container contains tag elements to show or hide
  45. * @param {Boolean} visibilityOverride overrides usual checks to force
  46. * visibilty state
  47. * @return {undefined}
  48. **/
  49. function toggleTags(container, visibilityOverride) {
  50.  
  51. let toggleBtn = container.getElementsByClassName("toggleTags")[0];
  52.  
  53. if(toggleBtn) {
  54. let tagsVisible = visibilityOverride ? visibilityOverride : ("hideTags" in toggleBtn.dataset);
  55.  
  56.  
  57. // loop tag anchors and toggle visibility state
  58. Array.prototype.forEach.call(container.getElementsByTagName("a"), anchor => {
  59. anchor.classList.toggle("display-none", !tagsVisible);
  60. });
  61.  
  62. // toggle saved state
  63. toggleBtn.innerText = tagsVisible ? "Hide Tags" : "Show Tags";
  64. if("hideTags" in toggleBtn.dataset) {
  65. delete toggleBtn.dataset.hideTags;
  66. } else {
  67. toggleBtn.dataset.hideTags = "";
  68. }
  69. }
  70. }
  71.  
  72. /**
  73. * Inserts a button that can be clicked to show or hide the tags
  74. * @param {HTMLElement} container parent element to insert button into
  75. * @return {undefined}
  76. *
  77. **/
  78. function addToggleButton(container) {
  79.  
  80. let toggleBtn = document.createElement("span");
  81.  
  82. // set toggle button details
  83. toggleBtn.classList.add("badge", "badge-warning", "toggleTags");
  84. toggleBtn.style.minWidth = "60px";
  85. toggleBtn.style.cursor = "pointer";
  86. toggleBtn.innerText = "Show Tags";
  87. toggleBtn.addEventListener("click", () => {
  88. // set visibility according to saved state
  89. toggleTags(container);
  90.  
  91. });
  92.  
  93.  
  94. // insert toggle button
  95. container.innerHTML = "&nbsp;" + container.innerHTML;
  96. container.insertBefore(toggleBtn, container.childNodes[0]);
  97. }
  98.  
  99. /**
  100. * Main function that runs the script; to be called once DOM is loaded
  101. * @return {undefined}
  102. **/
  103. function main() {
  104. let tagfields = document.querySelectorAll("a[href^='/genre']");
  105.  
  106. let containers = new Set(Array.prototype.map.call(tagfields, (anchor) => {
  107. return anchor.parentNode;
  108. }));
  109.  
  110. containers.forEach((container) => {
  111. addToggleButton(container);
  112. toggleTags(container, false);
  113. });
  114. }
  115. main();
  116.  
  117. // vim: set ts=2 sts=2 sw=2 et :

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址