StackExchange Hide Comments

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

  1. // ==UserScript==
  2. // @name StackExchange Hide Comments
  3. // @namespace https://github.com/walenzack
  4. // @version 0.5
  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 INBOX_BUTTON = 'a[class*="js-inbox-button"]';
  22. var INBOX_ITEM = 'li[class^="inbox-item"]';
  23. var COMMENT = 'div[id^="comments"]';
  24. var SO = /stackoverflow\.com/;
  25. var SE = /(\.stackexchange\.com|superuser\.com|askubuntu\.com|serverfault\.com|stackapps\.com|mathoverflow\.net)/;
  26. var CHAT = /chat\.stackexchange\.com/;
  27.  
  28. var inboxButton = document.querySelectorAll(INBOX_BUTTON)[0];
  29. var inboxUnreadCounter = inboxButton.children[1];
  30.  
  31. function hideComments() {
  32. if (!SO.test(window.location)) {
  33. [].forEach.call(
  34. document.querySelectorAll(COMMENT),
  35. function (el) {
  36. el.style.display="none";
  37. }
  38. );
  39. }
  40. }
  41.  
  42. function hideInboxComments() {
  43. // Currently this only works if the inbox has already been initialized (i.e. clicked)
  44. // TODO Load inbox items async. on page load without clearing initial unread status
  45. // TODO Skip forEach if there are no new notifications since last call
  46. [].forEach.call(
  47. document.querySelectorAll(INBOX_ITEM),
  48. function (el) {
  49. var href = el.children[0].href;
  50. if (SE.test(href)) { // Do not hide notifications for comments on SO posts
  51. if (!CHAT.test(href)) { // Do not hide chat notifications
  52. var type = el.children[0].children[1].children[0].children[0].innerText; //hacky af
  53. if ('comment' === type) {
  54. el.style.display="none";
  55. var count = Number(inboxUnreadCounter.innerText);
  56. if (count > 1) {
  57. inboxUnreadCounter.innerText = --count;
  58. } else if (count === 1) {
  59. inboxUnreadCounter.innerText = "";
  60. inboxUnreadCounter.style.display = 'none';
  61. }
  62. }
  63. }
  64. }
  65. }
  66. );
  67. };
  68.  
  69. /* Main logic */
  70. window.setInterval(function() {
  71. // Hide comments
  72. hideComments();
  73. // Hide comment notifications
  74. hideInboxComments();
  75. }, 2*1000);
  76. })();