Sort Flags

Allows you to sort flags by on your user flag page

  1. // ==UserScript==
  2. // @name Sort Flags
  3. // @author AstroCB
  4. // @description Allows you to sort flags by on your user flag page
  5. // @version 1.0
  6. // @namespace https://github.com/AstroCB
  7. // @include *://*.stackexchange.com/users/flag-summary/*
  8. // @include *://*stackoverflow.com/users/flag-summary/*
  9. // @include *://*serverfault.com/users/flag-summary/*
  10. // @include *://*superuser.com/users/flag-summary/*
  11. // @include *://*askubuntu.com/users/flag-summary/*
  12. // @include *://*stackapps.com/users/flag-summary/*
  13. // ==/UserScript==
  14.  
  15. function showFlagType(type) {
  16. var pn = parseInt($(".pager.fr > a:nth-last-child(2)").attr("href").replace("?page=", ""));
  17. $("#mainbar").text("");
  18. for (var i = 1; i < pn + 2; i++) {
  19. $("#mainbar").append('<div id="mainbar' + i + '"></div>');
  20. if (type === "All") {
  21. $("#mainbar" + i).load(location.href + "?page=" + i + " #mainbar", function () {
  22. $(".pager.fr").remove()
  23. });
  24. } else {
  25. $("#mainbar" + i).load(location.href + "?page=" + i + " #mainbar > .flagged-post:has(span." + type + ")", function () {
  26. $(".pager.fr").remove()
  27. });
  28. }
  29. }
  30. }
  31.  
  32. function showDeclined() {
  33. showFlagType("Declined");
  34.  
  35. // Update button text and hide other buttons
  36. $("#flag-sort-declined").text("declined");
  37. hide(["disputed", "helpful", "all"]);
  38. }
  39.  
  40. function showDisputed() {
  41. showFlagType("Disputed");
  42.  
  43. // Update button text and hide other buttons
  44. $("#flag-sort-disputed").text("disputed");
  45. hide(["declined", "helpful", "all"]);
  46. }
  47.  
  48. function showHelpful() {
  49. showFlagType("Helpful");
  50.  
  51. // Update button text and hide other buttons
  52. $("#flag-sort-helpful").text("helpful");
  53. hide(["declined", "disputed", "all"]);
  54. }
  55.  
  56. function showAll() {
  57. showFlagType("All");
  58.  
  59. // Update button text and hide other buttons
  60. $("#flag-sort-all").text("all");
  61. hide(["declined", "disputed", "helpful"]);
  62. }
  63.  
  64. function hide(buttons) { // Hide other buttons when one is clicked
  65. for (var i = 0; i < buttons.length; i++) {
  66. $("#flag-sort-" + buttons[i]).hide();
  67. }
  68. }
  69.  
  70. function main() {
  71. // Button styling
  72. var style = document.createElement("style");
  73. style.type = "text/css";
  74. style.innerText = ".flag-sort { margin-left: 25px; cursor: pointer; }";
  75. document.getElementsByTagName("head")[0].appendChild(style);
  76.  
  77. // Declined
  78. var declined = document.createElement("span");
  79. declined.innerText = "show declined";
  80. declined.setAttribute("id", "flag-sort-declined");
  81. declined.setAttribute("class", "flag-sort mod-flag-indicator");
  82. declined.style.backgroundColor = "#f00" // Spam red
  83. declined.onclick = showDeclined;
  84.  
  85. // Disputed
  86. var disputed = document.createElement("span");
  87. disputed.innerText = "show disputed";
  88. disputed.setAttribute("id", "flag-sort-disputed");
  89. disputed.setAttribute("class", "flag-sort mod-flag-indicator");
  90. disputed.style.backgroundColor = "#f90" // Supernova orange
  91. disputed.onclick = showDisputed;
  92.  
  93. // Helpful
  94. var helpful = document.createElement("span");
  95. helpful.innerText = "show helpful";
  96. helpful.setAttribute("id", "flag-sort-helpful");
  97. helpful.setAttribute("class", "flag-sort mod-flag-indicator");
  98. helpful.style.backgroundColor = "#33a030" // Rep green
  99. helpful.onclick = showHelpful;
  100.  
  101. // All
  102. var all = document.createElement("span");
  103. all.innerText = "show all";
  104. all.setAttribute("id", "flag-sort-all");
  105. all.setAttribute("class", "flag-sort mod-flag-indicator");
  106. all.style.backgroundColor = "#07d" // Bounty blue
  107. all.onclick = showAll;
  108.  
  109. // Container to be appended for the buttons
  110. var container = document.createElement("div");
  111. container.style.display = "inline-block";
  112. container.appendChild(declined);
  113. container.appendChild(disputed);
  114. container.appendChild(helpful);
  115. container.appendChild(all);
  116.  
  117. document.getElementsByClassName("subheader")[0].children[0].appendChild(container);
  118. }
  119.  
  120. // Inject helper functions
  121. var functions = showFlagType.toString() + "\n\n" + showDeclined.toString() + "\n\n" + showDisputed.toString() + "\n\n" + showHelpful.toString() + "\n\n" + showAll.toString() + "\n\n" + hide.toString();
  122. var functionScript = document.createElement('script');
  123. functionScript.type = "text/javascript";
  124. functionScript.textContent = functions;
  125. document.body.appendChild(functionScript);
  126.  
  127. // Inject the main script
  128. var script = document.createElement('script');
  129. script.type = "text/javascript";
  130. script.textContent = "(" + main.toString() + ")();";
  131. document.body.appendChild(script);