Kaskus - Hide Deleted VM

Hide deleted VM on your profile page.

  1. /* This program is free software. It comes without any warranty, to
  2. * the extent permitted by applicable law. You can redistribute it
  3. * and/or modify it under the terms of the Do What The Fuck You Want
  4. * To Public License, Version 2, as published by Sam Hocevar. See
  5. * http://www.wtfpl.net/ for more details. */
  6.  
  7. // ==UserScript==
  8. // @name Kaskus - Hide Deleted VM
  9. // @id kaskus.vm@loucypher
  10. // @namespace http://userscripts.org/users/12
  11. // @description Hide deleted VM on your profile page.
  12. // @version 7.5
  13. // @author LouCypher
  14. // @license WTFPL
  15. // @icon http://loucypher.github.io/userscripts/kaskus/kaskus-48.png
  16. // @icon64URL http://loucypher.github.io/userscripts/kaskus/kaskus-64.png
  17. // @contributionURL http://loucypher.github.io/userscripts/donate.html?Kaskus+-+Hide+Deleted+VM
  18. // @homepageURL https://greasyfork.org/scripts/10
  19. // @supportURL https://greasyfork.org/scripts/10/feedback
  20. // @resource CSS https://raw.github.com/LouCypher/userscripts/master/kaskus/moderated-vm-fix.css
  21. // @resource CHANGELOG https://raw.github.com/LouCypher/userscripts/master/kaskus/kaskus-hide-deleted-vm.CHANGELOG.txt
  22. // @resource LICENSE https://raw.github.com/LouCypher/userscripts/master/licenses/WTFPL/LICENSE.txt
  23. // @include /^http://www\.kaskus\.co\.id/profile(/[0-9]+)?/?$/
  24. // @run-at document-start
  25. // @grant unsafeWindow
  26. // @grant GM_getValue
  27. // @grant GM_setValue
  28. // @grant GM_addStyle
  29. // @grant GM_getResourceText
  30. // @grant GM_log
  31. // ==/UserScript==
  32.  
  33. var log = (typeof GM_info == "object") ? "" : "\n";
  34.  
  35. // Get user's numeric id from cookie if user is logged in
  36. function getUserId() {
  37. var userid = "";
  38. document.cookie.split(";").forEach(function(cookie) {
  39. if (/userid/.test(cookie)) {
  40. userid = cookie.match(/\d+/).toString();
  41. }
  42. })
  43. log += "* You" + (userid ? " have" : "'re NOT") + " logged in.";
  44. return userid;
  45. }
  46.  
  47. // Check if current page is user's profile page
  48. function isMyProfile(aUserId) {
  49. var mine = false;
  50. if ((location.href.match(/\d+/) == aUserId) ||
  51. (/\/profile\/?$/.test(location.pathname))) {
  52. mine = true;
  53. }
  54. if (aUserId) {
  55. log += "\n* This is" + (mine ? " " : " NOT ") + "your profile page.";
  56. }
  57. return mine;
  58. }
  59.  
  60. // Run on startup if argument is true
  61. function start(aOK) {
  62. if (aOK) {
  63. window.addEventListener("afterscriptexecute", process, true);
  64. document.addEventListener("DOMContentLoaded", contentLoad, false);
  65. }
  66. log += "\n* The userscript is" + (aOK ? " " : " NOT ") + "running.\n";
  67. GM_getValue("debug", false) && GM_log(log);
  68. GM_setValue("debug", GM_getValue("debug", false));
  69. }
  70.  
  71. // Run after each script is executed
  72. function process(aEvent) {
  73. if (/profile.js$/.test(aEvent.target.src)) {
  74. window.removeEventListener(aEvent.type, arguments.callee, true);
  75.  
  76. unsafeWindow.hideDeleted = true; // 'hideDeleted' is variable that
  77. // will be used by 'Show/Hide' button
  78.  
  79. var $ = unsafeWindow.$;
  80.  
  81. // Override 'getVM' function
  82. unsafeWindow.getVM = function getVM(b) {
  83. b && $("#do-see-more-updates").remove();
  84. var profile = $("#profile-content");
  85. profile.append('<div class="item" style="text-align:center"' +
  86. ' id="ajax_loader_html"><img src="http://kkcdn-static.' +
  87. 'kaskus.co.id/img/ajax-loader.gif"/></div>');
  88. $.getJSON("/profile/stream_activity_vm/all/" + (b ? b : "0") + "/" +
  89. $("#userid").val(), function(c) {
  90. $("#ajax_loader_html").remove("");
  91. $.each(c.stream_activity, function(e, f) {
  92. var deleted = /deleted\-vm/.test(f.content);
  93. var hideDeleted = unsafeWindow.hideDeleted;
  94. var html = '<div class="item' +
  95. (deleted ? ' deleted' : '') +
  96. (deleted && hideDeleted ? ' hide' : '') +
  97. '" id="vm_' + f.vmid + '"><div class="item-content">' +
  98. '<a href="#vm_' + f.vmid + '" class="entry-head">' +
  99. '<i class="icon-star"></i></a>' + f.profilepic +
  100. '<div class="message"><div class="vcard">' + f.username +
  101. f.date + '</div>' + f.content + '</div></div>';
  102. if (f.button_action != "") {
  103. html += '<div class="m-meta">' + f.button_action + "</div>"
  104. }
  105. html += "</div>";
  106. profile.append(html);
  107. if (c.stream_activity.length - 1 == e && f.username != "") {
  108. profile.append('<div class="load-more"><a href="javascript:' +
  109. 'void(0);" id="do-see-more-updates" onclick="' +
  110. 'getVM(\'' + c.oldest_id + '\'); return false;"' +
  111. ' class="button small white">Load More updates' +
  112. '</a></div>')
  113. }
  114. })
  115. })
  116. }
  117.  
  118. // Override 'moderate_vm' function
  119. unsafeWindow.moderate_vm = function moderate_vm(a, c) {
  120. $.get("/visitormessage/moderate/" + a + "/" + c, function(d) {
  121. if (c == "delete") {
  122. $("#vm_" + a + " .m-meta").html('<a href="javascript:void(0);"' +
  123. ' onclick="moderate_vm(' + a +
  124. ',\'undelete\');return false;"' +
  125. ' class="delete"><i class="icon-' +
  126. 'trash"></i>Undelete</a>')
  127. $("#vm_" + a).addClass("deleted");
  128. $("#vm_" + a + " .message").addClass("deleted-vm"); // paint it red
  129. unsafeWindow.hideDeleted && $("#vm_" + a).addClass("hide"); // hide
  130. } else { // undelete
  131. $("#vm_" + a).html(d);
  132. $("#vm_" + a).removeClass("deleted hide"); // unhide
  133. }
  134. })
  135. }
  136. }
  137. }
  138.  
  139. // Run at DOMContentLoaded
  140. function contentLoad() {
  141. // Scriptish doesn't add styles at document-start so we put it here
  142. GM_addStyle(GM_getResourceText("CSS"));
  143.  
  144. if (document.querySelector('div[class^="wrap op"]') || // over posting
  145. document.querySelector("div.pong-wrapper")) // main tenis
  146. return; // Don't run if Kaskus is over posting or main tenis
  147.  
  148. if (!("$" in unsafeWindow)) {
  149. var msg = "JavaScript must be enabled for Kaskus - Hide Deleted VM "
  150. + "user script to work.\nIf you have NoScript extension, "
  151. + "you must allow `googleapis.com`, `kaskus.com` and\n"
  152. + "`kaskus.co.id` from NoScript menu.";
  153. alert(msg);
  154. throw new Error(msg);
  155. }
  156.  
  157. var $ = unsafeWindow.$;
  158.  
  159. // Add button to toggle show/hide deleted VM
  160. $("#say-what .act input").after('<input type="button" value="Show deleted' +
  161. ' VM" class="button small white" style="' +
  162. 'float:left"/>');
  163.  
  164. // Button action
  165. $("#say-what .act input[type='button']").click(function(e) {
  166. if ($(".deleted").hasClass("hide")) {
  167. e.target.value = e.target.value.replace(/^Show/, "Hide");
  168. $(".deleted").removeClass("hide");
  169. unsafeWindow.hideDeleted = false;
  170. }
  171. else {
  172. e.target.value = e.target.value.replace(/^Hide/, "Show");
  173. $(".deleted").addClass("hide");
  174. unsafeWindow.hideDeleted = true;
  175. }
  176. })
  177. }
  178.  
  179. // Start if current page is user's profile page
  180. start(isMyProfile(getUserId()));