RSS+Atom Feed Subscribe Button Generator

Finds RSS and/or Atom links on a page and inserts feed subscription links for use by aggregators

  1. // ==UserScript==
  2. // @id RSS+Atom Feed Subscribe Button Generator
  3. // @name RSS+Atom Feed Subscribe Button Generator
  4. // @description Finds RSS and/or Atom links on a page and inserts feed subscription links for use by aggregators
  5. // @namespace http://loonyone.livejournal.com
  6. // @include *
  7. // @creator Manpreet Singh [junkblocker@yahoo.com]
  8. // @copyright Manpreet Singh [junkblocker@yahoo.com]
  9. // @author Manpreet Singh [junkblocker@yahoo.com]
  10. // @version 2.7
  11. // @date 2022-04-20
  12. // @inject-into content
  13. // ==/UserScript==
  14.  
  15. /*
  16. * Copyright (c) 2006-2022, Manpreet Singh [junkblocker@yahoo.com]
  17. *
  18. * Permission is hereby granted, free of charge, to any person
  19. * obtaining a copy of this software and associated documentation
  20. * files (the "Software"), to deal in the Software without
  21. * restriction, including without limitation the rights to use,
  22. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  23. * copies of the Software, and to permit persons to whom the
  24. * Software is furnished to do so, subject to the following
  25. * conditions:
  26. *
  27. * The above copyright notice and this permission notice shall be
  28. * included in all copies or substantial portions of the Software.
  29. *
  30. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  31. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  32. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  33. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  34. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  35. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  36. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  37. * OTHER DEALINGS IN THE SOFTWARE.
  38. */
  39.  
  40. // ******
  41. // Opera, Chrome, Chromium, Safari and IE7Pro compatible except
  42. //
  43. // 1) link tags injection does not work in IE7Pro
  44. //
  45. // due to non-firefox platform limitations/differences.
  46. //
  47. // IE7Pro bundles an older version of this script by default.
  48. // ******
  49.  
  50. // Version 2.7 - Further improve display CSS enforcement
  51. // Version 2.6 - Better injection of icons on some strict CSP sites
  52. // Version 2.5 - New home at greasyfork.org
  53. // Removed auto-update code
  54. // Version 2.4 - Constrained container background and border
  55. // Version 2.3 - Constrained button dimensions some more
  56. // Version 2.2 - Constrained button dimensions
  57. // Version 2.1 - Fixed Content Security Policy issues
  58. // Version 2.0 - 'more' button was broken for me
  59. // some javascript lint cleanup
  60. // Lighter icons by simon!
  61. // Version 1.9 - Fixed invalid update metadata
  62. // Version 1.8 - Layout fix on apple.com/macupdate.com etc.
  63. // Version 1.7 - Minor layout fix on arstechnica.com
  64. // Version 1.6 - Insert discovered link tags in header for the browser's detection to list them too
  65. // Version 1.5 - Added license info
  66. // Added checks for feeds like &feed=rss or &feed=atom and some case insensitivity
  67. // Version 1.4 - Made the feed check slightly more stringent by excluding javascript: mailto: etc. urls
  68. // Version 1.3 - Reduce the number of button shown initially to a sane minimum
  69. // Version 1.2 - Fixed partially broken auto update.
  70. // Don't increase the install count on userscripts.org while checking for updates
  71. // Version 1.1 - Misc code cleanup
  72. // Version 1.0 - Added auto update without requiring an external script
  73. // Version 0.9 - Added a close button
  74. // Version 0.8 - Checks some wiki feeds with ?action=rss_rc and feeds with /rss/$ or /atom/$
  75. // Version 0.7 - Check for feedburner feeds
  76. // Version 0.6 - Fixed a major issue with using snapshotLength when it was not available
  77. // Version 0.5 - Updated contact info
  78. // Version 0.4 - Added optional auto-update facility
  79. // Version 0.3 - Added some speed optimizations
  80. // Version 0.2 - Merged Iain Brodfoot's floating div behaviour and removed purl.org links
  81. // - Allow revert to old behaviour by hand editing this script
  82. // Version 0.1 - First release
  83.  
  84. /*
  85. * Options contribution by Iain Broadfoot (ibroadfo@geeksoc.org)
  86. * - remove link to purl
  87. * (Manually edit this script to restore old behaviour. See TWEAK NOTE 1 below)
  88. * - Float the buttons to enable better behaviour on blogger/blogspot.com
  89. * (Manually edit this script to restore old behaviour. See TWEAK NOTE 2 below)
  90. *
  91. * Initially lifted mostly from
  92. * Generate RSS and ATOM tags: http://highbyte.bounceme.net/greasemonkey/genrsslinkrel.user.js
  93. * Amazon Atom Injector: http://docs.g-blog.net/code/greasemonkey/amazon_xml_feeds.user.js
  94. * Add RSS Index: http://www.xs4all.nl/~jlpoutre/BoT/Javascript/RSSpanel/rsspanel.user.js
  95. *
  96. * Original buttons from the excellent: http://kalsey.com/tools/buttonmaker/
  97. */
  98.  
  99. console.log('rssatom_feed_subscribe_b.user.js starting');
  100. try {
  101. (function() {
  102. function addEventHandler(target, eventName, eventHandler, scope) {
  103. var f = scope ? function() {
  104. eventHandler.apply(scope, arguments);
  105. } : eventHandler;
  106. if (target.addEventListener) {
  107. target.addEventListener(eventName, f, true);
  108. } else if (target.attachEvent) {
  109. target.attachEvent('on' + eventName, f);
  110. }
  111. return f;
  112. }
  113.  
  114. var head = document.getElementsByTagName('head');
  115. if (head && head[0]) head = head[0];
  116. var INITIAL_COUNT = 5;
  117. var ATOM_ICON = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFAAAAAPBAMAAACB51W8AAAAAXNSR0IArs4c6QAAACRQTFRFAAEAAxx+JCYjYWNgho53kJKPy6Gio63Ys7Wysb3lyMnG/f/8AJKv4wAAAAFiS0dEAIgFHUgAAAAJcEhZcwAACxMAAAsTAQCanBgAAAAHdElNRQfcBhsNKDWiRqoQAAAA2UlEQVQoz42SoQ7CMBCGL10w7CWWEAwzNRiwOEzD5DwvgSGoJVUoBAZFSGeWQZPSeznabutYtiX709b0y3/X/wowVSFW0vZ4iSMOq2hAGVgux2d98dl15UFmQMVr4zHwAAsFsMXMAJdltKKzYVACgTVAbA2RucYdaLZfDtSJhJgGPMPvHRk5QwKpvWvXvi5NjZ3pkeP1hDQoiaxAb9eAb0gtqDneULMW7DmWplb8gI19rOiC3R4VkCgsYW5T539gPx7lhmITFH4So4G7qM1kpoBC5DiqYvLn+QGXavuS25r9yAAAAABJRU5ErkJggg==";
  118. var RSS_ICON = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFAAAAAPAgMAAAAOp6AcAAAAAXNSR0IArs4c6QAAAAxQTFRFAAAA/2YAiY55////S8IQBgAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9wLFRMROauDVnkAAAB7SURBVBjTpc8xDoAgDEBRLuXsKToaT0FMungfMLgz4CmMdygzC1G0yGIii/EPDM0LaYWo1Z6vdtFADtWj6ctwhKNfcTE26ehm8nHhodxAApKx3iifX3vL0AM6Y09NxDJlOUAHK2o2ihkVKbcwAiZNaSb25c9/e1Zvr3UBWOasaPHYnJwAAAAASUVORK5CYII=";
  119. var RDF_ICON = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFAAAAAPAgMAAAAOp6AcAAAAAXNSR0IArs4c6QAAAAxQTFRFAAAA/2YAiY55////S8IQBgAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9wLFRMKD80PCHoAAAB3SURBVBjTpc8xDsAQGIZhl+rcU4jJMaSJxX1o2A0cwx1+s0Ua+muXJrU0fQfDlycCIbPW/uogCx1J/Wj/MipWFJXR+mZqcJBrxFEkLqgE67PVeZz+khxlsL4bAJRtyI0NadBoZHBLkQqOzUBzgP6+8987p3+fdQKJXassgfh4CAAAAABJRU5ErkJggg==";
  120. var XML_ICON = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFAAAAAPAgMAAAAOp6AcAAAAAXNSR0IArs4c6QAAAAxQTFRFAAAA/2YAiY55////S8IQBgAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9wLFRMRFO5cCgwAAAB3SURBVBjTY2DABuz/Y4A/DKahIFC1CgmsIUWwtPTq9dCq/et2/Vv9a9/6V69/7QcKxl79DhR8tW7X63WrXoPIXUDB8NC7QMF963b9X/3qFVDlP4hKkPbVQDWrgMpeQVSCzPxa9W/1q3/rXwHVQ8ykzJ1Y/Y4NAADEZ67GIr3R+wAAAABJRU5ErkJggg==";
  121. var CLOSE_ICON = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA8AAAAPAgMAAABGuH3ZAAAAAXNSR0IArs4c6QAAAAlQTFRF/2YA////AAAALA0mBgAAAAFiS0dEAIgFHUgAAAAJcEhZcwAAEnMAABJzAYwiuQcAAAAHdElNRQfcCxUTCRv8+I/EAAAAMklEQVQI12NYtWrVCoapoaERDBMYGCSghAMLkBAQABIsDkCCkQHGAouBZeGKwXpBpgAAhtMRBKRIycIAAAAASUVORK5CYII=";
  122. var MORE_ICON = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAPAgMAAADrOWVwAAAAAXNSR0IArs4c6QAAAAxQTFRFAAEA/mcAh494/v/8doDPMwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9wLFRMNODrzO7IAAABNSURBVAjXY2BAAPv/UPCHwTQUDKpWrcHGfAVklpbWxl/9W/ULyKz9ej229GrVPyCzvPQ6SPQ/WMH12KsQUaDa2FKIWiQTsFqB5AYEAABsXUkpSEP5/AAAAABJRU5ErkJggg==";
  123. // array to keep track of 'seen' feed links.
  124. var seen = [];
  125.  
  126. var fsbdiv = document.createElement("div");
  127. fsbdiv.setAttribute("id", "XmlButtons");
  128. fsbdiv.style.cssText = "text-align: left !important; margin: 0px !important; padding: 0px 3px 0px 3px !important; border: none !important; background: none !important; position: fixed !important; top: 0px !important; left: 0px !important; z-index: 99999 !important; display: inline !important;";
  129.  
  130. // Get links already declared properly in <link> elements in <head>
  131. function getKnownTags() {
  132. try {
  133. var linkRelElems = document.getElementsByTagName("link");
  134. var tlink;
  135.  
  136. for (var i = 0, l = linkRelElems.length; i < l; i++) {
  137. tlink = linkRelElems[i];
  138. if (!tlink) continue;
  139. try {
  140. var thref = tlink.href;
  141. var type = tlink.type;
  142.  
  143. if (type && type.match(/.*\/rs[sd](\+xml)?$/i)) {
  144. addRssLinkTag(thref, RSS_ICON);
  145. } else if (type && type.match(/.*\/atom\+xml$/i)) {
  146. addRssLinkTag(thref, ATOM_ICON);
  147. } else if (type && type.match(/^text\/xml$/i)) {
  148. addRssLinkTag(thref, RSS_ICON);
  149. }
  150. } catch (e) {
  151. console.log(e);
  152. }
  153. }
  154. } catch (ex) {
  155. console.log(ex);
  156. }
  157. }
  158.  
  159. function discoverUnknownFeeds() {
  160. try {
  161. if (!document.links) {
  162. document.links = document.getElementsByTagName("a");
  163. }
  164. var links = document.links;
  165. for (var a = 0, len = links.length; a < len; a++) {
  166. var link = links[a];
  167. linkhref = link.href;
  168.  
  169. if (linkhref.match(/^rss:/) ||
  170. linkhref.match(/^(http|ftp|feed).*([\.\/]rss([\.\/]xml|\.aspx|\.jsp|\/)?$|\/node\/feed$|\/rss\/[a-z0-9]+$|[?&;](rss|xml|rdf)=|[?&;]feed=rss[0-9.]*$|[?&;]action=rss_rc$|feeds\.feedburner\.com\/[a-z0-9]+$)/i)) {
  171. addRssLinkTag(linkhref, RSS_ICON, "rss");
  172. } else if (linkhref.match(/^(http|ftp|feed).*\/atom(\.xml|\.aspx|\.jsp|\/)?$|[?&;]feed=atom[0-9.]*$/i)) {
  173. addRssLinkTag(linkhref, ATOM_ICON, "atom");
  174. } else if (linkhref.match(/^(http|ftp|feed).*(\/feeds?\/[^.\/]*\.xml$|.*\/index\.xml$|feed\/msgs\.xml(\?num=\d+)?$)/i)) {
  175. addRssLinkTag(linkhref, XML_ICON, "rss");
  176. } else if (linkhref.match(/^(http|ftp|feed).*\.rdf$/i)) {
  177. addRssLinkTag(linkhref, RDF_ICON, "rss");
  178. } else if (linkhref.match(/^feed:\/\//i)) {
  179. addRssLinkTag(linkhref, RSS_ICON, "rss");
  180. }
  181. }
  182. } catch (e) {
  183. console.log(e);
  184. }
  185. }
  186.  
  187. function beenThere(linkhref) {
  188. if (seen.length <= 0) return false;
  189.  
  190. var href = linkhref.toLowerCase();
  191. for (var i = seen.length - 1; i >= 0; --i) {
  192. if (seen[i].toLowerCase() == href) return true;
  193. }
  194.  
  195. return false;
  196. }
  197.  
  198. function moreFSB() {
  199. var elem;
  200. for (var i = 1;; i++) {
  201. elem = document.getElementById("XmlButton" + i);
  202. if (!elem) break;
  203. try {
  204. elem.style.cssText = "display: inline !important;";
  205. } catch (e) {}
  206. }
  207. document.getElementById("MoreFSBButton").style.cssText = "display: none !important;";
  208. return false;
  209. }
  210.  
  211. function addRssLinkTag(linkhref, icon, addheadtype) {
  212. try {
  213. if (beenThere(linkhref)) {
  214. return;
  215. } else {
  216. seen.push(linkhref);
  217. }
  218.  
  219. if (seen.length == (INITIAL_COUNT + 1)) {
  220. var moreButton = document.createElement('a');
  221.  
  222. var moreButton_img = document.createElement('img');
  223. moreButton_img.src = MORE_ICON;
  224. moreButton_img.style.cssText = "border: none !important; margin: 0px !important; width: 40px !important; height: 15px !important; display: inline !important";
  225. moreButton.appendChild(moreButton_img);
  226.  
  227. moreButton.style.cssText = "display: inline !imporant; cursor: pointer !important;";
  228. moreButton.id = 'MoreFSBButton';
  229. addEventHandler(moreButton, 'click', moreFSB);
  230. fsbdiv.appendChild(moreButton);
  231. fsbdiv.appendChild(document.createTextNode(" "));
  232. }
  233. var flink = document.createElement("a");
  234.  
  235. var flink_img = document.createElement("img");
  236. flink_img.src = icon;
  237. flink_img.style.cssText = "border: none !important; margin: 0px !important; width: 80px !important; height: 15px !important; display: inline !important";
  238.  
  239. flink.appendChild(flink_img);
  240.  
  241. flink.href = linkhref;
  242. // TWEAK NOTE 2 START : Uncomment the href line below to restore linking
  243. // to purl.org pages for easy subscription in feed readers. This service
  244. // creates link for all known aggregators which support RSS subscription
  245. // service via a URL.
  246.  
  247. // href = "http://purl.org/net/syndication/subscribe/?rss=" + linkhref;
  248.  
  249. // TWEAK NOTE 2 END
  250. flink.alt = linkhref;
  251. flink.title = linkhref;
  252. flink.id = 'XmlButton' + seen.length;
  253. if (seen.length >= (INITIAL_COUNT + 1)) {
  254. flink.style.cssText = "display: none !important";
  255. }
  256. fsbdiv.appendChild(flink);
  257. fsbdiv.appendChild(document.createTextNode(" "));
  258. // <link href="url" rel="alternate" title="desc" type="application/rss+xml" />
  259. if (addheadtype && head) {
  260. var link = document.createElement("link");
  261. link.href = linkhref;
  262. link.rel = "alternate";
  263. link.title = linkhref + " (discovered by RSS+Atom Feed Subscribe Button Generator)";
  264. link.type = "application/" + addheadtype + "+xml";
  265. head.appendChild(link);
  266. }
  267. } catch (e) {
  268. console.log(e);
  269. }
  270. }
  271.  
  272. try {
  273. getKnownTags();
  274. discoverUnknownFeeds();
  275. } catch (e) {}
  276.  
  277. var body = document.getElementsByTagName("body")[0];
  278.  
  279. function closeFSB() {
  280. try {
  281. document.getElementById("XmlButtons").style.cssText = "display: none !important";
  282. } catch (e) {}
  283. }
  284. try {
  285. // insert the div only if something was found
  286. if (seen.length > 0) {
  287. if (head) {
  288. var closeButton = document.createElement('a');
  289. var closeButton_img = document.createElement('img');
  290. closeButton_img.src = CLOSE_ICON;
  291. closeButton_img.style.cssText = "border: none !important; margin: 0px !important; width: 15px !important; height: 15px !important; display: inline !important";
  292.  
  293. closeButton.appendChild(closeButton_img);
  294. addEventHandler(closeButton, 'click', closeFSB);
  295. closeButton.style.cssText = "display: inline !important; cursor: pointer !important;";
  296. }
  297. fsbdiv.insertBefore(closeButton, fsbdiv.firstChild);
  298. setTimeout(function() {
  299. body.insertBefore(fsbdiv, body.firstChild);
  300. }, 333); // hack to workaround the duplicated content problem
  301. }
  302. } catch (e) {}
  303.  
  304. })();
  305. } catch (outere) {
  306. console.log(outere);
  307. }
  308. console.log('rssatom_feed_subscribe_b.user.js ended');
  309. // vim: set et fdm=indent fenc=utf-8 ff=unix ft=javascript sts=0 sw=4 ts=4 tw=0 nowrap :