StackExchange Hide Comments

Hide comments on all Stack Exchange sites (except StackOverflow).

As of 2019-01-11. See the latest version.

  1. // ==UserScript==
  2. // @name StackExchange Hide Comments
  3. // @namespace https://github.com/walenzack
  4. // @version 0.4
  5. // @description Hide comments on all Stack Exchange sites (except StackOverflow).
  6. // @author walen
  7. // @match https://*.stackoverflow.com/*
  8. // @match https://*.stackexchange.com/*
  9. // @match https://superuser.com/*
  10. // @match https://askubuntu.com/*
  11. // @match https://serverfault.com/*
  12. // @match https://stackapps.com/*
  13. // @match https://mathoverflow.net/*
  14. // @grant window.setInterval
  15. // @grant window.setTimeout
  16. // @grant window.location
  17. // ==/UserScript==
  18.  
  19. (function() {
  20. 'use strict';
  21. var CLEAR_INBOX_INTERVAL_MILLIS = 5 * 1000;
  22. var INBOX_BUTTON = 'a[class*="js-inbox-button"]';
  23. var INBOX_ITEM = 'li[class^="inbox-item"]';
  24. var COMMENT = 'div[id^="comments"]';
  25. var SO = /stackoverflow\.com/;
  26. var SE = /(\.stackexchange\.com|superuser\.com|askubuntu\.com|serverfault\.com|stackapps\.com|mathoverflow\.net)/;
  27. var CHAT = /chat\.stackexchange\.com/;
  28.  
  29. var inboxButton = document.querySelectorAll(INBOX_BUTTON)[0];
  30. var inboxUnreadCounter = inboxButton.children[1];
  31.  
  32. function hideComments() {
  33. [].forEach.call(
  34. document.querySelectorAll(COMMENT),
  35. function (el) {
  36. el.style.display="none";
  37. }
  38. );
  39. }
  40.  
  41. function forceLoadInbox() {
  42. // Show and immediately hide inbox to force loading
  43. inboxButton.click(); // show
  44. inboxButton.click(); // hide
  45. }
  46. function hideInboxComments() {
  47. // TODO Skip forEach if there are no new notifications since last call
  48. [].forEach.call(
  49. document.querySelectorAll(INBOX_ITEM),
  50. function (el) {
  51. var href = el.children[0].href;
  52. if (SE.test(href)) {
  53. if (!CHAT.test(href)) { // Do not hide chat notifications
  54. var type = el.children[0].children[1].children[0].children[0].innerText; //hacky af
  55. if ('comment' === type) {
  56. el.style.display="none";
  57. var count = Number(inboxUnreadCounter.innerText);
  58. if (count > 1) {
  59. inboxUnreadCounter.innerText = --count;
  60. } else if (count === 1) {
  61. inboxUnreadCounter.innerText = "";
  62. inboxUnreadCounter.style.display = 'none';
  63. }
  64. }
  65. }
  66. }
  67. }
  68. );
  69. };
  70.  
  71. /* Main logic */
  72. // Hide all comments, except on StackOverflow
  73. if (!SO.test(window.location)) {
  74. hideComments();
  75. }
  76. // Force load of inbox div. Wait a bit for the rest of the page to load.
  77. window.setTimeout(forceLoadInbox, CLEAR_INBOX_INTERVAL_MILLIS);
  78. // Periodically check for and hide new comment notifications, everywhere
  79. window.setInterval(hideInboxComments, CLEAR_INBOX_INTERVAL_MILLIS);
  80. })();