Survivor Sucks Block Users

Creates a "Block" button on Sucks posts and hides all posts for

当前为 2015-05-31 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Survivor Sucks Block Users
  3. // @author jkalderash
  4. // @version 1.2
  5. // @description Creates a "Block" button on Sucks posts and hides all posts for
  6. // the user when the button is pressed. Also creates a list of blocked
  7. // users at the bottom of each page.
  8. // @match http://survivorsucks.com/*
  9. // @match http://survivorsucks.yuku.com/*
  10. // @copyright 2013+, jkalderash
  11. // @require http://code.jquery.com/jquery-2.0.0.min.js
  12. // @namespace http://greasyfork.org/users/1076-jkalderash
  13. // @grant GM_getValue
  14. // @grant GM_setValue
  15. // ==/UserScript==
  16.  
  17. // hide all posts/threads/quotes/links for a blocked username
  18. function hideAll(url) {
  19. // Hide all posts from the user.
  20. getPosts(url).hide();
  21. // Hide all threads from the user.
  22. getThreads(url).hide();
  23. // From MisterRisible! Removes the text from quotes of the blocked user.
  24. var quotedUsername = convertUrlToQuotedUsername(url);
  25. getBlockquotes(quotedUsername).each(function() {
  26. // Build a fake blockquote with a "click to show" button.
  27. var actualQuote = $(this);
  28. var fakeQuote = $("<blockquote class=\"jkalderash-blocked\"><strong class=\"quote-title\">"
  29. + quotedUsername + " wrote:</strong>&nbsp;</blockquote>");
  30. var clickToShow = $('<a href=\"javascript:\">click to show</a>');
  31. clickToShow.click(function() {
  32. $(fakeQuote).detach();
  33. $(actualQuote).show();
  34. });
  35. fakeQuote.append(clickToShow);
  36. $(this).before(fakeQuote);
  37. $(this).hide();
  38. });
  39. // Hide "Last Comment" links for a user.
  40. getLastComments(url).each(function() {
  41. // Clicking on the "<BLOCKED>" link reveals the blocked username.
  42. var actualUserName = $(this);
  43. var fakeUserName = $('<a class=\"jkalderash-blocked\" href=\"javascript:\">&lt;BLOCKED&gt;</a>');
  44. fakeUserName.click(function() {
  45. $(fakeUserName).detach();
  46. $(actualUserName).show();
  47. });
  48. actualUserName.before(fakeUserName);
  49. actualUserName.hide();
  50. });
  51. }
  52.  
  53. // unhide all posts/threads/quotes for an unblocked username
  54. function unhideAll(url) {
  55. getPosts(url).show();
  56. getThreads(url).show();
  57. var quotedUsername = convertUrlToQuotedUsername(url);
  58. getBlockquotes(quotedUsername).each(function() {
  59. $(this).siblings(".jkalderash-blocked").detach();
  60. $(this).show();
  61. });
  62. getLastComments(url).each(function() {
  63. $(this).siblings(".jkalderash-blocked").detach();
  64. $(this).show();
  65. });
  66. }
  67.  
  68. // get all of the posts by a user
  69. function getPosts(url) {
  70. return $("td.poster-name").find("a[href='http://" + url + ".yuku.com']").closest("tbody");
  71. }
  72.  
  73. // get all of the threads by a user
  74. function getThreads(url) {
  75. return $("td.author").find("a[href='http://" + url + ".yuku.com']").closest("tr");
  76. }
  77.  
  78. // gets all <blockquote> elements for quotes by a user
  79. function getBlockquotes(quotedUsername) {
  80. return $("strong.quote-title").filter(function() {
  81. return $(this).text() === quotedUsername + " wrote:";
  82. }).parent();
  83. }
  84.  
  85. // get all of the "Last Comment" links for a user
  86. function getLastComments(url) {
  87. return $("td.latest ").find("a[href='http://" + url + ".yuku.com']");
  88. }
  89.  
  90. // modify the permanent blacklist variable
  91. function updateBlacklist() {
  92. GM_setValue("blacklist", blacklist.join(" "));
  93. }
  94.  
  95. // inserts a username into the blacklist. returns true if the user was not
  96. // already blacklisted. case insensitive.
  97. function insertIntoBlacklist(username) {
  98. var usernameLower = username.toLowerCase();
  99. for (var i = 0; i < blacklist.length; i++) {
  100. otherUsernameLower = blacklist[i].toLowerCase();
  101. if (usernameLower == otherUsernameLower) {
  102. return false;
  103. }
  104. if (usernameLower < otherUsernameLower) {
  105. break;
  106. }
  107. }
  108. blacklist.splice(i, 0, username);
  109. console.log("New blacklist: " + blacklist.join(" "));
  110. updateBlacklist();
  111. return true;
  112. }
  113.  
  114. // adds a "Block" link to each post
  115. function addBlockLink(element, url) {
  116. var newItem = $('<li><a href=\"javascript:\">Block</a></li>');
  117. newItem.click(function() {
  118. console.log("BLOCK " + url);
  119. if (insertIntoBlacklist(url)) {
  120. hideAll(url);
  121. makeUnblockList();
  122. window.alert("Blocked " + convertUrlToThreadUsername(url));
  123. }
  124. });
  125. $(element).closest("tbody.thread-post").find("ul.reply-options").append(newItem);
  126. }
  127.  
  128. // returns a link to unblock a blocked user
  129. function makeUnblockLink(url) {
  130. var threadUsername = convertUrlToThreadUsername(url);
  131. var newItem = $("<a href=\"javascript:\">" + threadUsername + "</a>");
  132. newItem.click(function() {
  133. blacklist.splice(blacklist.indexOf(url), 1);
  134. console.log("New blacklist: " + blacklist.join(" "));
  135. updateBlacklist();
  136. $(this).hide();
  137. unhideAll(url);
  138. alert("Unblocked " + threadUsername);
  139. });
  140. return newItem;
  141. }
  142.  
  143. // Helper function for sorting the unblock list at the bottom of the page.
  144. function sortUnblockLinks(link1, link2) {
  145. s1 = link1.html().toLowerCase();
  146. s2 = link2.html().toLowerCase();
  147. if (s1 > s2) {
  148. return 1;
  149. }
  150. if (s2 > s1) {
  151. return -1;
  152. }
  153. return 0;
  154. }
  155.  
  156. // Clear and rebuild the unblock list.
  157. function makeUnblockList() {
  158. var list = $("#blacklist");
  159. list.find("a").detach();
  160. unblockLinks = blacklist.map(makeUnblockLink).sort(sortUnblockLinks);
  161. unblockLinks.forEach(function(unblockLink) {
  162. $(list).append(" ");
  163. $(list).append(unblockLink);
  164. });
  165. }
  166.  
  167. // get the username from the URL of the profile page.
  168. // this is the username used in quote blocks, i.e. "Joe Schmoe wrote:"
  169. function convertUrlToQuotedUsername(url) {
  170. // remove everything before the first dot
  171. var dot = url.indexOf(".");
  172. url = url.substr(0, dot);
  173. // replace hyphens with spaces
  174. url = url.replace(/-/g, " ");
  175. return url;
  176. }
  177.  
  178. // get the username from the URL of the profile page.
  179. // this is the username displayed next to the user's avatar.
  180. function convertUrlToThreadUsername(url) {
  181. // remove trailing ".u" or ".e"
  182. if (url.indexOf(".u", url.length - 2) > 0
  183. || url.indexOf(".e", url.length - 2) > 0) {
  184. url = url.substr(0, url.length - 2);
  185. }
  186. // remove trailing ".survivorsucks"
  187. var suffix = ".survivorsucks";
  188. if (url.indexOf(suffix, url.length - suffix.length) >= 0) {
  189. url = url.substring(0, url.length - suffix.length);
  190. }
  191. // replace hyphens with spaces
  192. url = url.replace(/-/g, " ");
  193. return url;
  194. }
  195.  
  196. $(document).ready(function() {
  197. // retrieve the stored value of the blacklist
  198. window.blacklist = GM_getValue("blacklist", "").split(" ");
  199. console.log("Blacklist: " + blacklist.join(" "));
  200. if (blacklist.length == 1 && blacklist[0] == "") {
  201. // split() on an empty string returns [""]
  202. blacklist.pop();
  203. }
  204. // hide the posts of each blacklisted user
  205. blacklist.forEach(hideAll);
  206. // for each post, create a "Block" link
  207. $("span.user-name a").each(function() {
  208. var url = $(this).attr("href").substr(7);
  209. url = url.substr(0, url.length - 9);
  210. addBlockLink(this, url);
  211. });
  212. // create a list of blocked users at the end of the page
  213. var footer = $("div.myfooter");
  214. var list = $("<p id=\"blacklist\">LIST OF BLOCKED USERNAMES (click to unblock):</p>");
  215. footer.append(list);
  216. makeUnblockList();
  217. // create a link to wipe the blacklist.
  218. var clearAll = $("<p><a href=\"javascript:\">Click here to clear all blocked usernames</a></p>");
  219. clearAll.click(function() {
  220. window.blacklist = [];
  221. updateBlacklist();
  222. $("tbody.thread-post").show();
  223. $("tbody.post").show();
  224. $("td.author").closest("tr").show();
  225. $("blockquote.jkalderash-blocked").detach();
  226. $("strong.quote-title").parent().show();
  227. list.find("a").detach();
  228. alert("Cleared all blocked usernames");
  229. });
  230. footer.append(clearAll);
  231. });