Survivor Sucks Block Users

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

当前为 2014-05-24 提交的版本,查看 最新版本

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