StackExchange Hide Comments

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

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

  1. // ==UserScript==
  2. // @name StackExchange Hide Comments
  3. // @namespace https://github.com/walenzack
  4. // @version 0.2
  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.clearInterval
  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 inboxUnreadCounter = document.querySelectorAll(INBOX_BUTTON)[0].children[1];
  30.  
  31. function hideComments() {
  32. [].forEach.call(
  33. document.querySelectorAll(COMMENT),
  34. function (el) {
  35. el.style.display="none";
  36. }
  37. );
  38. }
  39.  
  40. function hideInboxComments() {
  41. // TODO Skip forEach if there are no new notifications since last call
  42. [].forEach.call(
  43. document.querySelectorAll(INBOX_ITEM),
  44. function (el) {
  45. var href = el.children[0].href;
  46. if (SE.test(href)) {
  47. if (!CHAT.test(href)) { // Do not hide chat notifications
  48. var type = el.children[0].children[1].children[0].children[0].innerText; //hacky af
  49. if ('comment' === type) {
  50. el.style.display="none";
  51. var count = Number(inboxUnreadCounter.innerText);
  52. if (count > 1) {
  53. inboxUnreadCounter.innerText = --count;
  54. } else if (count === 1) {
  55. inboxUnreadCounter.innerText = "";
  56. inboxUnreadCounter.style.display = 'none';
  57. }
  58. }
  59. }
  60. }
  61. }
  62. );
  63. };
  64.  
  65. // Main logic
  66. if (!SO.test(window.location)) { // Do not hide SO comments
  67. hideComments();
  68. }
  69. var timerClearInbox = window.setInterval(
  70. hideInboxComments,
  71. CLEAR_INBOX_INTERVAL_MILLIS
  72. );
  73. })();