Tapas.io RSS Button

Adds RSS buttons to webcomics on tapas.io.

  1.  
  2. // ==UserScript==
  3. // @name Tapas.io RSS Button
  4. // @description Adds RSS buttons to webcomics on tapas.io.
  5. // @author Alex V.H.
  6. // @namespace https://avhn.us/
  7. // @version 2.05
  8. // @license CC0-1.0
  9. // Copyright: the embedded RSS icon and the script's icon are both derived from assets from tapas.io
  10. // I believe that their use in this script is okay, since the RSS icon is generic and universal,
  11. // and both the tapas logo and their RSS sprite have been transformed from the original.
  12. // @supportURL https://forums.tapas.io/t/rss-feeds-gone/41724
  13. // @icon https://archive.org/download/tapas-rss-icon/tapas-rss.gif
  14. // @compatible firefox browser used for development by author
  15. // @compatible chrome briefly tested by author
  16. // @match https://tapas.io/series/*
  17. // @match https://tapas.io/episode/*
  18. // @match https://m.tapas.io/series/*
  19. // @match https://m.tapas.io/episode/*
  20. // @grant none
  21. // ==/UserScript==
  22. const rssIcon = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAQAAAC1+jfqAAAAAmJLR0QA/4ePzL8AAAAJcEhZcwAALiMAAC4jAXilP3YAAAAHdElNRQfkBg8CGwrcReAoAAAAGXRFWHRDb21tZW50AENyZWF0ZWQgd2l0aCBHSU1QV4EOFwAAAPxJREFUKM99kD9LwgEURc/PQJAQIwkqopaawm/Q3lI0Orrkx2hxaXBrdQmsJmlJ6CsU9IegKQoiSgIriginOg2+zCy643uHd+99ic8M80iLa/bZS+4ZlP36sGnhP0D13ZqZ733iIi+MMskCS+RjesJKcvvbbMiSN3Hnzin+khnrgRyHkWtuWLForgdVAqn9DNmxajaQesQtDLY4dzqMulmagIdue2Cnh2QBLMVfxr98x2wEUo1GD6qW+xs0IksOwE1VdzFxznRc6RoVAVxV9TTFFhccmYakzRkA8wC0AJhIsQwUmAHgCoARAJ4AyKdY55UdLgGYBaANQPdxb589AOJXV9GZEgAAAABJRU5ErkJggg==";
  23. // tapas.io webcomic series all have a unique ID: a few-digit (4-ish?) base-10 number.
  24. // The ID is needed to construct the RSS URL.
  25. // Fortunately, there are a couple of places to find the ID.
  26. // The following functions return the ID, or -1 if they fail to find it.
  27. function findIDfromMetas(){
  28. // tapas.io sends a great deal of meta tags in their pages.
  29. // Many of these tags are to allow different mobile apps to get to the same page.
  30. // These tags have the format "tapastic://series/id[/episode/N]".
  31. const tapastic_regex = new RegExp("tapastic:\/\/series\/[0-9]+");
  32. const id_regex = new RegExp("[0-9]+");
  33. const metas = document.getElementsByTagName('meta');
  34. console.log(metas);
  35. for (let i=0; i<metas.length; i++){
  36. let meta = metas[i];
  37. let content = meta.getAttribute("content");
  38. if (tapastic_regex.test(content)) {
  39. console.log("Found a series ID in", meta);
  40. //let id=content.match("[0-9]+")[0];
  41. let id = id_regex.exec(content)[0];
  42. return id;
  43. }
  44. }
  45. console.log("Couldn't find a meta tag with the series ID!");
  46. return -1;
  47. }
  48. function findIDfromSubscribers(){
  49. // Get the js-subscribe-cnt classed elements, they have an href with the series ID.
  50. // In a browser, you can see the URL (and ID) by hovering over the subscriber count in the info page.
  51. let jscs = document.getElementsByClassName("js-subscribe-cnt");
  52. if (jscs.length < 1) {console.log("No place to get series id!"); return -1;}
  53. jsc = jscs[0];
  54. console.log(jsc.href);
  55. if (jscs.length > 1) {console.log("More than one \"js-subscribe-cnt\"s found. Using the first one and ignoring the rest.");}
  56. let numbers = jsc.href.match("[0-9]+"); // regex: a series of one or more digits
  57. if (numbers.length < 1) {console.log("No series ID found! Searched this string for the ID, a base-10 number:", jsc.href); return -1;}
  58. console.log("Found a series ID in", jsc);
  59. let id = numbers[0];
  60. if (numbers.length > 1) {console.log("More than one base-10 number found where ID should be! Using the first one and ignoring the rest.");}
  61. return id;
  62. }
  63. function findID(){
  64. let id = findIDfromMetas();
  65. if (id == -1){
  66. id = findIDfromSubscribers();
  67. if (id == -1){
  68. console.log("Tried everything but couldn't find series ID! Script is broken!");
  69. console.log("Please file a bug at https://forums.tapas.io/t/rss-feeds-gone/41724 and ping the author!");
  70. }
  71. }
  72. return id;
  73. }
  74. function rssURL(id){
  75. return "https://tapas.io/rss/series/" + id;
  76. }
  77. function addRSSButtons(id){
  78. if (id < 0){return;}
  79. // There are 4 places to add an RSS button:
  80. // In the Button Wrapper in series-root__footer (visible on the mobile website)
  81. let srfs = document.getElementsByClassName("series-root__footer");
  82. console.log(srfs);
  83. if (srfs.length > 0){
  84. console.log("There is a series root footer. Looking for its button-wrapper.");
  85. let srf = srfs[0];
  86. let bws = srf.getElementsByClassName("button-wrapper");
  87. console.log(bws);
  88. if (bws.length > 0){
  89. console.log("There is a button-wrapper in the series root footer.");
  90. let bw = bws[0];
  91. let rbs = bw.getElementsByClassName("button-rss");
  92. if (rbs.length > 0) {
  93. console.log("This button-wrapper already has an RSS button. Not adding another one.");
  94. } else {
  95. console.log("Adding an RSS button to the series root footer's button-wrapper.");
  96. let img = document.createElement("img");
  97. img.style = "filter: invert(100%)";
  98. img.alt = "RSS Icon";
  99. img.src = rssIcon;
  100. let btn = document.createElement("a");
  101. btn.className = "button button--subscribe button-rss";
  102. btn.href = rssURL(id);
  103. btn.appendChild(img);
  104. bw.appendChild(btn);
  105. console.log("Button added to", bw);
  106. }
  107. }
  108. }
  109. // In the Button Wrapper in section__top
  110. let sts = document.getElementsByClassName("section__top");
  111. console.log(sts);
  112. if (sts.length > 0){
  113. // There are 2 section__top elements. One of them is section__top--simple. Don't put a button on it.
  114. // The other is just section__top. It (and not section__top--simple) has a button-wrapper. Do put a button in there.
  115. let st;
  116. for(let i=0; i<sts.length; i++){
  117. let st = sts[i];
  118. let bws = st.getElementsByClassName("button-wrapper");
  119. if (bws.length > 0){
  120. console.log(st, "has a button-wrapper.");
  121. let bw = bws[0];
  122. let rbs = bw.getElementsByClassName("button-rss");
  123. if (rbs.length > 0){
  124. console.log("This button-wrapper already has an RSS button. Not adding another one.");
  125. } else {
  126. console.log("Adding an RSS button to this section__top's button-wrapper.");
  127. let img = document.createElement("img");
  128. img.alt = "RSS Icon";
  129. img.src = rssIcon;
  130. let btn = document.createElement("a");
  131. btn.className = "button button--read button-rss";
  132. // This particular button needs to be re-styled.
  133. btn.style = "min-width: 16px; padding: 12px 12px; margin-left: 6px;"
  134. btn.href = rssURL(id);
  135. btn.appendChild(img);
  136. bw.appendChild(btn);
  137. console.log("Button added to", bw);
  138. }
  139. }
  140. }
  141. }
  142. // In the center of the Toolbar at the bottom of the comic browser
  143. let trcs = document.getElementsByClassName("toolbar__row--center");
  144. if (trcs.length > 0){
  145. console.log(trcs);
  146. let trc = trcs[0];
  147. let ris = trc.getElementsByClassName("row-item");
  148. console.log(ris);
  149. if (ris.length > 0){
  150. let ri = ris[0];
  151. let rbs = ri.getElementsByClassName("button-rss");
  152. if (rbs.length > 0){
  153. console.log("This row-item alreads has an RSS button. Not adding another one.");
  154. } else {
  155. console.log("Adding an RSS button to this toolbar__row--center's row-item.");
  156. let a = document.createElement("a");
  157. a.className = "toolbar-btn button-rss";
  158. a.href = rssURL(id);
  159. let div = document.createElement("div");
  160. div.className = "ico-wrap";
  161. let img = document.createElement("img");
  162. img.class = "ico";
  163. img.style = "margin: auto;";
  164. img.alt = "RSS Icon";
  165. img.src = rssIcon;
  166. div.appendChild(img);
  167. let span = document.createElement("span");
  168. let text = document.createTextNode("RSS");
  169. span.appendChild(text);
  170. a.appendChild(div);
  171. a.appendChild(span);
  172. ri.appendChild(a);
  173. console.log("Button added to", ri);
  174. }
  175. }
  176. }
  177. // In the Dropdown List in the tpFloatMenu (on the mobile website)
  178. let tfm = document.getElementById("tpFloatMenu");
  179. if (tfm != null){
  180. console.log("Found a tpFloatMenu:", tfm);
  181. if (tfm.text.indexOf("button\-rss") > -1){
  182. console.log("The tpFloatMenu already contains an RSS button. Not adding another one.");
  183. } else {
  184. // tpFloatMenu is a script tag with a text node.
  185. // The text node contains the HTML that goes into the menu:
  186. // a <div> containing a <ul>, which contains a few <li>s.
  187. // We want to insert a new <li> after the first <li>.
  188. console.log(tfm.text);
  189. let lis = tfm.text.split("\<\/li\>");
  190. lis.splice(1,0,"\n <li class=\"dropdown-list__item\"> <a href=\"https://tapas.io/rss/series/" + id + "\" class=\"dropdown-list__button button-rss\"\> <span class=\"ico-wrapper\"><i class=\"ico\"> <img src=\"" + rssIcon + "\"> </i></span>RSS Feed</a>")
  191. tfm.text = lis.join("</li>");
  192. console.log(tfm.text);
  193. }
  194. }
  195. }
  196. function addLink(id){
  197. let ls = document.head.getElementsByTagName("link");
  198. for (let i=0; i<ls.length; i++){
  199. let l = ls[i];
  200. if (l.type == "application/rss+xml"){
  201. if (l.href == rssURL(id)){
  202. console.log("This page's head already has a link tag for the RSS feed. Not adding another one.");
  203. return;
  204. }
  205. }
  206. }
  207. let l = document.createElement("link");
  208. l.rel = "alternate";
  209. l.type = "application/rss+xml";
  210. l.title = "RSS Feed";
  211. l.href = rssURL(id);
  212. document.head.appendChild(l);
  213. console.log("Added link tag to head:", l);
  214. }
  215. function addButtons(){
  216. let id = findID();
  217. if (id < 0) {return;}
  218. addRSSButtons(id);
  219. addLink(id);
  220. }
  221. // Tapas has a website feature where,
  222. // if while reading a comic you click on the "more..." in the description in the top-right,
  223. // the comic's info page loads and is overlaid over the episode page.
  224. // This changes the window URL and loads a bunch of new content,
  225. // which needs an RSS button added.
  226. // But since the overlaid info page is loaded and rendered after the page load,
  227. // the script will not have added an RSS button to it.
  228. // But because the window URL has changed,
  229. // if you click the "more..." link and then refresh the page,
  230. // the RSS button will be added to the info page!
  231. // The script also works fine if you navigate directly to the info page.
  232. // To get around this post-page-load content-and-URL-change,
  233. // the script detects clicks on the "more..." link,
  234. // waits 2 seconds to give time for the overlaid info page to load,
  235. // and then adds the RSS button.
  236. function delayAddButtons(){window.setTimeout(addButtons, 2000);}
  237. let mlbs = document.getElementsByClassName("more-less-btn");
  238. console.log(mlbs.length);
  239. if (mlbs.length > 0) {
  240. mlb = mlbs[0];
  241. console.log("Adding callback to more-less-button:", mlb);
  242. mlb.addEventListener("click", delayAddButtons);
  243. }
  244. // Do the thing!
  245. addButtons();
  246.