Game Comments Enhancer *OLD*

Lets you specify a rating threshold for & adds pagination to comments

  1. // ==UserScript==
  2. // @name Game Comments Enhancer *OLD*
  3. // @namespace ventero.de
  4. // @author Ventero
  5. // @description Lets you specify a rating threshold for & adds pagination to comments
  6. // @include http://www.kongregate.com/games/*/*
  7. // @include http://www.kongregate.com/games/*/*/comments*
  8. // @date 26.09.2011
  9. // @version 1.4
  10. // require http://kong.ventero.de/updates/84216.js
  11. // @license MIT license
  12. // ==/UserScript==
  13.  
  14. // Written by Ventero (http://www.kongregate.com/accounts/Ventero) 08/23/2010
  15. // Licensed under MIT/X11 license
  16. // Copyright (c) 2010 Ventero
  17. // http://www.opensource.org/licenses/mit-license.php
  18.  
  19. if(window != top) return;
  20.  
  21. function runInScope(src, args){
  22. var s = document.createElement("script");
  23. s.textContent = "" +
  24. "if(typeof CommentsEnhancer === 'undefined'){" +
  25. " CommentsEnhancer = {}" +
  26. "};" +
  27. "(function(args){" +
  28. "(" + src.toString() + ").apply(CommentsEnhancer, args);" +
  29. "})(" + (args ? JSON.stringify(args) : "") + ");";
  30. document.body.appendChild(s);
  31. setTimeout(function(){document.body.removeChild(s);}, 100);
  32. }
  33.  
  34. var OPTION = "kong_rating_threshold";
  35. var threshold = GM_getValue(OPTION, "all");
  36.  
  37. GM_registerMenuCommand("Set comments rating threshold", function(){
  38. var r = prompt("Please enter the rating threshold. To show all comments, " +
  39. "don't enter anything (or random text) and click ok")
  40.  
  41. window.setTimeout(function(){
  42. GM_setValue(OPTION, r);
  43. }, 0);
  44. runInScope(function(t){
  45. this.threshold = this.parseThreshold(t);
  46. this.showComments();
  47. }, [r]);
  48. });
  49.  
  50. runInScope(function(threshold){
  51. function parseThreshold(value){
  52. var parsed = parseInt(value, 10);
  53. if(isNaN(parsed))
  54. return -Infinity;
  55. else
  56. return parsed;
  57. }
  58.  
  59. function showComments(target){
  60. var threshold = CommentsEnhancer.threshold;
  61. if(target) {
  62. target = $(target);
  63. } else {
  64. target = $("comments_list") || $("all_comments");
  65. }
  66.  
  67. function hide(a){
  68. target.down("#" + a).hide();
  69. }
  70.  
  71. function show(a){
  72. target.down("#" + a).show();
  73. }
  74.  
  75. function getRating(a){
  76. return parseInt(target.down("#comment_current_rating_" + a).innerHTML, 10);
  77. }
  78.  
  79. var comments = target.getElementsByClassName("user_message comment"), current, id;
  80. for(var i = comments.length - 1; i >= 0; i--){
  81. current = comments[i];
  82. id = current.id.split("_")[1];
  83.  
  84. if(getRating(id) > threshold) {
  85. current.className = current.className.replace("below_threshold", "above_threshold");
  86. hide('show_comment_content_' + id);
  87. show('hide_comment_content_' + id);
  88. show('comment_content_' + id);
  89. } else {
  90. current.className = current.className.replace("above_threshold", "below_threshold");
  91. show('show_comment_content_' + id);
  92. hide('hide_comment_content_' + id);
  93. hide('comment_content_' + id);
  94. }
  95. }
  96. }
  97.  
  98. var recent = $('recent_comments')
  99. if(recent && !recent.down(".user_message.comment")) {
  100. recent.observe("DOMNodeInserted", function(){
  101. if(!recent.down(".user_message.comment")) return;
  102. recent.stopObserving("DOMNodeInserted");
  103. showComments(recent);
  104. });
  105. }
  106.  
  107. this.threshold = parseThreshold(threshold);
  108. this.parseThreshold = parseThreshold;
  109. this.showComments = showComments;
  110.  
  111. showComments();
  112. }, [threshold]);
  113.  
  114. /////////////////////////////////////////////////////////////////////////
  115. if(!document.getElementById("comments_list")) return;
  116.  
  117. runInScope(function(){
  118. var self = this;
  119. var commentsUrl = location.pathname + "/comments";
  120.  
  121. function loadComments(event, target, params){
  122. divs[params.sort].down("div").hide();
  123. $("spinner_" + params.sort).show();
  124.  
  125. var tempDiv = new Element("div");
  126.  
  127. new Ajax.Updater({success: tempDiv}, commentsUrl, {
  128. method: "get",
  129. parameters: params,
  130. requestHeaders: { "Accept" : "text/html, */*" },
  131. onComplete: function(r){
  132. var commentsList = tempDiv.down("div#all_comments").down("div");
  133. var pagination = tempDiv.down(".pagination.simple_pagination");
  134.  
  135. var oldPagination = target.down(".pagination.simple_pagination");
  136. if(oldPagination) oldPagination.remove();
  137.  
  138. self.showComments(commentsList);
  139. target.down("div").update(commentsList.innerHTML);
  140. target.appendChild(pagination);
  141.  
  142. var links = ["next", "last", "prev", "first"];
  143.  
  144. links.forEach(function(type){
  145. var link = pagination.down("li." + type).down("a");
  146. if(link && link.href){
  147. var paramString = link.href.substring(link.href.indexOf("?"));
  148. if(!paramString) return;
  149. var newParams = paramString.toQueryParams();
  150. link.observe("click", function(event){
  151. return loadComments(event, target, newParams);
  152. });
  153. link.href = "#";
  154. }
  155. });
  156.  
  157. $("spinner_" + params.sort).hide();
  158. divs[params.sort].down("div").show();
  159. resetLinks[params.sort].show();
  160. comments_controller._comment_ids = commentsList.select(".user_message.comment").map(function(ele){
  161. return ele.id.split("_")[1];
  162. });
  163. comments_controller.checkUserRatingStatus();
  164. }
  165. /* TODO: onFailure */
  166. });
  167.  
  168. if(event.stop) event.stop();
  169. if(event.preventDefault) event.preventDefault();
  170. return false;
  171. }
  172.  
  173. var fiveBest, fiveRecent, origIds;
  174.  
  175. function loadInitialBest(event){
  176. origIds = comments_controller._comment_ids;
  177. fiveBest = top.down("div").innerHTML;
  178. topMore.hide();
  179.  
  180. return loadComments(event, top, {sort: "best"});
  181. }
  182.  
  183. function loadInitialRecent(event){
  184. fiveRecent = recent.down("div").innerHTML;
  185. recentMore.hide();
  186.  
  187. return loadComments(event, recent, {sort: "newest"});
  188. }
  189.  
  190. function reset(event, target, orig, more, reset){
  191. target.down("div").update(orig);
  192. var pagination = target.down(".pagination.simple_pagination");
  193. if(pagination) pagination.remove();
  194. more.show();
  195. reset.hide();
  196.  
  197. comments_controller._comment_ids = origIds;
  198. comments_controller.checkUserRatingStatus();
  199.  
  200. if(event.stop) event.stop();
  201. if(event.preventDefault) event.preventDefault();
  202.  
  203. return false;
  204. }
  205.  
  206. function resetBest(event){
  207. return reset(event, top, fiveBest, topMore, topReset);
  208. }
  209.  
  210. function resetRecent(event){
  211. return reset(event, recent, fiveRecent, recentMore, recentReset);
  212. }
  213.  
  214. var list = $("comments_list");
  215. var top = list.down(".top_comments");
  216. var recent = $("recent_comments");
  217. var divs = {best: top, newest: recent};
  218.  
  219. var topMore = new Element("a", {href: "#"}).update("(see more)");
  220. var recentMore = new Element("a", {href: "#"}).update("(see more)");
  221. var topReset = new Element("a", {href: "#"}).update("(see less)").hide();
  222. var recentReset = new Element("a", {href: "#"}).update("(see less)").hide();
  223. var resetLinks = {best: topReset, newest: recentReset}
  224.  
  225. top.down(".comments_type").insert(" ").insert(topMore).insert(topReset);
  226. topMore.observe("click", loadInitialBest);
  227. topReset.observe("click", resetBest);
  228.  
  229. recent.down(".comments_type").insert(" ").insert(recentMore).insert(recentReset);
  230. recentMore.observe("click", loadInitialRecent);
  231. recentReset.observe("click", resetRecent);
  232.  
  233. var topSpinner = new Element("span", {"class": "spinner spinner_big", "id": "spinner_best"}).update("loading").hide();
  234. var recentSpinner = new Element("span", {"class": "spinner spinner_big", "id": "spinner_newest"}).update("loading").hide();
  235.  
  236. top.insert(topSpinner);
  237. recent.insert(recentSpinner);
  238. });