Kaskus - Hide Deleted VM

Hide deleted VM on your profile page.

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

  1. // ==UserScript==
  2. // @name Kaskus - Hide Deleted VM
  3. // @id kaskus.vm@loucypher
  4. // @namespace http://userscripts.org/users/12
  5. // @description Hide deleted VM on your profile page.
  6. // @version 1.20140219123729
  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://userscripts.org/scripts/show/162010
  13. // @supportURL https://userscripts.org/scripts/discuss/162010
  14. // @resource CSS https://raw.github.com/LouCypher/userscripts/master/kaskus/moderated-vm-fix.css
  15. // @resource CHANGELOG https://raw.github.com/LouCypher/userscripts/master/kaskus/kaskus-hide-deleted-vm.CHANGELOG.txt
  16. // @resource LICENSE https://raw.github.com/LouCypher/userscripts/master/licenses/WTFPL/LICENSE.txt
  17. // @include /^https?:\/\/www\.kaskus\.co\.id\/profile\/[0-9]+\/?$/
  18. // @include /^https?:\/\/www\.kaskus\.co\.id\/profile\/?$/
  19. // @run-at document-start
  20. // @grant unsafeWindow
  21. // @grant GM_getValue
  22. // @grant GM_setValue
  23. // @grant GM_addStyle
  24. // @grant GM_getResourceText
  25. // @grant GM_log
  26. // ==/UserScript==
  27. /* This program is free software. It comes without any warranty, to
  28. * the extent permitted by applicable law. You can redistribute it
  29. * and/or modify it under the terms of the Do What The Fuck You Want
  30. * To Public License, Version 2, as published by Sam Hocevar. See
  31. * http://www.wtfpl.net/ for more details. */
  32.  
  33.  
  34.  
  35. var log = (typeof GM_info == "object") ? "" : "\n";
  36. start(isMyProfile(getUserId())); // Start if current page is user's profile page
  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' +
  90. '-static.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:void(0);' +
  112. '" id="do-see-more-updates" onclick="getVM(\'' +
  113. c.oldest_id + '\'); return false;" class="button' +
  114. ' small white">Load More updates</a></div>')
  115. }
  116. })
  117. })
  118. }
  119.  
  120. // Override 'moderate_vm' function
  121. unsafeWindow.moderate_vm = function moderate_vm(a, c) {
  122. $.get("/visitormessage/moderate/" + a + "/" + c, function(d) {
  123. if (c == "delete") {
  124. $("#vm_" + a + " .m-meta").html('<a href="javascript:void(0);"' +
  125. ' onclick="moderate_vm(' + a +
  126. ',\'undelete\');return false;"' +
  127. ' class="delete"><i class="icon-' +
  128. 'trash"></i>Undelete</a>')
  129. $("#vm_" + a).addClass("deleted");
  130. $("#vm_" + a + " .message").addClass("deleted-vm"); // paint it red
  131. unsafeWindow.hideDeleted && $("#vm_" + a).addClass("hide"); // hide
  132. } else { // undelete
  133. $("#vm_" + a).html(d);
  134. $("#vm_" + a).removeClass("deleted hide"); // unhide
  135. }
  136. })
  137. }
  138. }
  139. }
  140.  
  141. // Run at DOMContentLoaded
  142. function contentLoad() {
  143. // Scriptish doesn't add styles at document-start so we put it here
  144. GM_addStyle(GM_getResourceText("CSS"));
  145.  
  146. if (!("$" in unsafeWindow)) {
  147. var msg = "JavaScript must be enabled for Kaskus - Hide Deleted VM "
  148. + "userscript to work.\nIf you have NoScript extension, "
  149. + "you must allow `googleapis.com`, `kaskus.com` and\n"
  150. + "`kaskus.co.id` from NoScript menu.";
  151. alert(msg);
  152. throw new Error(msg);
  153. }
  154.  
  155. var $ = unsafeWindow.$;
  156.  
  157. // Add button to toggle show/hide deleted VM
  158. $("#say-what .act input").after('<input type="button"' +
  159. ' value="Show deleted VM"' +
  160. ' class="button small white"' +
  161. ' style="float:left"/>');
  162.  
  163. // Button action
  164. $("#say-what .act input[type='button']").click(function(e) {
  165. if ($(".deleted").hasClass("hide")) {
  166. e.target.value = e.target.value.replace(/^Show/, "Hide");
  167. $(".deleted").removeClass("hide");
  168. unsafeWindow.hideDeleted = false;
  169. } else {
  170. e.target.value = e.target.value.replace(/^Hide/, "Show");
  171. $(".deleted").addClass("hide");
  172. unsafeWindow.hideDeleted = true;
  173. }
  174. })
  175. }