reddit styles

reddit themes for the common folk

当前为 2015-04-23 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name reddit styles
  3. // @namespace http://nucular.github.io
  4. // @version 0.6.1
  5. // @description reddit themes for the common folk
  6. // @copyright 2015, nucular
  7. // @license MIT License
  8. // @include http://www.reddit.com/*
  9. // @include https://www.reddit.com/*
  10. // @run-at document-start
  11. // @grant GM_getValue
  12. // @grant GM_setValue
  13. // @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js
  14. // ==/UserScript==
  15.  
  16. var featured = [
  17. "NautClassic",
  18. "serene",
  19. "carbon",
  20. "edurne",
  21. "mindashq"
  22. ];
  23.  
  24. var activated = GM_getValue("activated", false);
  25. var subreddit = GM_getValue("subreddit", "");
  26. var excluded = GM_getValue("excluded", "").split(",");
  27. var otherpages = GM_getValue("otherpages", true);
  28. var dontoverride = GM_getValue("dontoverride", false);
  29.  
  30. function setActivated(a) {
  31. activated = a;
  32. GM_setValue("activated", a);
  33. }
  34.  
  35. function setOtherPages(a) {
  36. otherpages = a;
  37. GM_setValue("otherpages", a);
  38. }
  39.  
  40. function setDontOverride(a) {
  41. dontoverride = a;
  42. GM_setValue("dontoverride", a);
  43. }
  44.  
  45. function setSubreddit(s) {
  46. var sane = s.replace(/\/r\/|[^\w]/g, "");
  47. subreddit = sane;
  48. if ($("#styles_subreddit").length > 0)
  49. $("#styles_subreddit").val(sane);
  50. GM_setValue("subreddit", sane);
  51. }
  52.  
  53. function setExcluded(e) {
  54. var split = e.split(",");
  55. var sane = [];
  56. for (var i = 0; i < split.length; i++) {
  57. var v = split[i].replace(/\/r\/|[^\w*?+]/g, "");
  58. if (v)
  59. sane.push(v);
  60. }
  61. excluded = sane;
  62. sane = sane.join(",");
  63. if ($("#styles_excluded").length > 0)
  64. $("#styles_excluded").val(sane);
  65. GM_setValue("excluded", sane);
  66. }
  67.  
  68. function loadStyle() {
  69. if (subreddit == "") {
  70. return;
  71. }
  72. // jQuery may not be loaded yet here
  73. var links = document.getElementsByTagName("link");
  74. var href = "https://www.reddit.com/r/" + subreddit + "/stylesheet/";
  75. var done = false;
  76. for (var i = 0; i < links.length; i++) {
  77. if (links[i].title == "applied_subreddit_stylesheet") {
  78. if (links[i].href != href)
  79. links[i].href = href;
  80. done = true;
  81. }
  82. }
  83. if (!done) {
  84. // Assume we're on another page
  85. var link = document.createElement("link");
  86. link.title = "applied_subreddit_stylesheet";
  87. link.rel = "stylesheet";
  88. link.href = href;
  89. document.head.appendChild(link);
  90. }
  91. }
  92.  
  93. function loadPrefs() {
  94. document.addEventListener("DOMContentLoaded", function() {
  95. $("<tr><th>reddit styles</th><td class='prefright'>"
  96. + "<input name='styles_activated' id='styles_activated' type='checkbox'>"
  97. + "<label class='' for='styles_activated'>activate <a href='https://greasyfork.org/en/scripts/9451-reddit-styles'>reddit styles</a></label> "
  98. + "<span class='details'>use the custom theme of any subreddit everywhere</span>"
  99. + "<br>"
  100. + "use theme from /r/<input name='styles_subreddit' id='styles_subreddit' type='text' style='width: 150px'> "
  101. + "<span class='details' id='styles_featured'>e.g.&nbsp;</span>"
  102. + "<br>"
  103. + "exclude subreddits <input name='styles_excluded' id='styles_excluded' type='text' style='width: 150px'> "
  104. + "<span class='details'>separated by commas, use * and ? as wildcards</span>"
  105. + "<br>"
  106. + "<input name='styles_otherpages' id='styles_otherpages' type='checkbox'>"
  107. + "<label class='' for='styles_otherpages'>style other pages too (if available)</label> "
  108. + "<span class='details'>e.g the front, subreddits, compose/messages and explore pages</span>"
  109. + "<br>"
  110. + "<input name='styles_dontoverride' id='styles_dontoverride' type='checkbox'>"
  111. + "<label class='' for='styles_dontoverride'>don't override if a subreddit already has a custom theme</label> "
  112. + "<span class='details'>this will delay the loading of your custom stylesheet though</span>"
  113. + "</tr></td>").prependTo(".content.preftable > tbody");
  114. for (var i = 0; i < featured.length; i++) {
  115. $("<a></a>")
  116. .attr("href", "https://www.reddit.com/r/" + featured[i])
  117. .text(featured[i])
  118. .appendTo("#styles_featured")
  119. .on("click", function(e) {
  120. e.preventDefault();
  121. setSubreddit($(this).text());
  122. });
  123. if (i < featured.length-1)
  124. $("<span>,</span>").appendTo("#styles_featured");
  125. }
  126. $("#styles_activated")
  127. .on("change", function(e) {
  128. setActivated(this.checked);
  129. })[0].checked = activated;
  130. $("#styles_otherpages")
  131. .on("change", function(e) {
  132. setOtherPages(this.checked);
  133. })[0].checked = otherpages;
  134. $("#styles_dontoverride")
  135. .on("change", function(e) {
  136. setDontOverride(this.checked);
  137. })[0].checked = dontoverride;
  138. $("#styles_subreddit")
  139. .val(subreddit)
  140. .on("change", function(e) {
  141. setSubreddit($(this).val());
  142. });
  143. $("#styles_excluded")
  144. .val(excluded.join(","))
  145. .on("change", function(e) {
  146. setExcluded($(this).val());
  147. });
  148. });
  149. }
  150.  
  151.  
  152. var page = window.location.pathname.match(/^(\/\w+)/);
  153. if (!page) page = "/";
  154. else page = page[1];
  155.  
  156. if (activated) {
  157. // Subreddits
  158. if (page == "/r") {
  159. var sub = window.location.pathname.match(/^\/r\/([\w+]+)/)[1];
  160. // First check if the sub is excluded
  161. for (var i = 0; i < excluded.length; i++) {
  162. if (sub.match(new RegExp("^" + excluded[i].replace(/([\*\?])/g, ".$1") + "$")))
  163. return;
  164. }
  165. // Gotta poke the server about this, sadly
  166. if (dontoverride) {
  167. var http = new XMLHttpRequest();
  168. http.open("HEAD", "https://www.reddit.com/r/" + sub + "/stylesheet/");
  169. http.onreadystatechange = function() {
  170. if (this.readyState == this.HEADERS_RECEIVED) {
  171. if (this.status == 403 || this.status == 404) { // 403 = banned/private, 404 = no style
  172. loadStyle();
  173. setInterval(loadStyle, 300);
  174. }
  175. }
  176. };
  177. http.send();
  178. } else {
  179. loadStyle();
  180. setInterval(loadStyle, 300);
  181. }
  182. }
  183. // Other areas
  184. else if (otherpages && page != "/prefs") { //(page == "/" || page == "/compose" || page == "/subreddits" || page == "/messages" || page == "/explore")) {
  185. loadStyle();
  186. setInterval(loadStyle, 300);
  187. }
  188. }
  189. // Preferences
  190. if (page == "/prefs") {
  191. loadPrefs();
  192. }