Tapas.io RSS Button

Adds RSS buttons to webcomics on tapas.io.

当前为 2021-10-13 提交的版本,查看 最新版本

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