Kaskus - Hide Deleted VM

Hide deleted VM on your profile page.

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

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