StackExchange Hide Comments

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

Verze ze dne 11. 01. 2019. Zobrazit nejnovější verzi.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         StackExchange Hide Comments
// @namespace    https://github.com/walenzack
// @version      0.4
// @description  Hide comments on all Stack Exchange sites (except StackOverflow).
// @author       walen
// @match        https://*.stackoverflow.com/*
// @match        https://*.stackexchange.com/*
// @match        https://superuser.com/*
// @match        https://askubuntu.com/*
// @match        https://serverfault.com/*
// @match        https://stackapps.com/*
// @match        https://mathoverflow.net/*
// @grant        window.setInterval
// @grant        window.setTimeout
// @grant        window.location
// ==/UserScript==

(function() {
  'use strict';
  var CLEAR_INBOX_INTERVAL_MILLIS = 5 * 1000;
  var INBOX_BUTTON = 'a[class*="js-inbox-button"]';
  var INBOX_ITEM = 'li[class^="inbox-item"]';
  var COMMENT = 'div[id^="comments"]';
  var SO = /stackoverflow\.com/;
  var SE = /(\.stackexchange\.com|superuser\.com|askubuntu\.com|serverfault\.com|stackapps\.com|mathoverflow\.net)/;
  var CHAT = /chat\.stackexchange\.com/;

  var inboxButton = document.querySelectorAll(INBOX_BUTTON)[0];
  var inboxUnreadCounter = inboxButton.children[1];

  function hideComments() {
    [].forEach.call(
      document.querySelectorAll(COMMENT),
      function (el) {
        el.style.display="none";
      }
    );
  }

  function forceLoadInbox() {
    // Show and immediately hide inbox to force loading
    inboxButton.click(); // show
    inboxButton.click(); // hide
  }
  
  function hideInboxComments() {
    // TODO Skip forEach if there are no new notifications since last call
    [].forEach.call(
      document.querySelectorAll(INBOX_ITEM),
      function (el) {
        var href = el.children[0].href;
        if (SE.test(href)) {
          if (!CHAT.test(href)) { // Do not hide chat notifications
            var type = el.children[0].children[1].children[0].children[0].innerText; //hacky af
            if ('comment' === type) {
              el.style.display="none";
              var count = Number(inboxUnreadCounter.innerText);
              if (count > 1) {
                inboxUnreadCounter.innerText = --count;
              } else if (count === 1) {
                inboxUnreadCounter.innerText = "";
                inboxUnreadCounter.style.display = 'none';
              }
            }
          }
        }
      }
    );
  };

  /* Main logic */
  // Hide all comments, except on StackOverflow
  if (!SO.test(window.location)) {
    hideComments();
  }
  // Force load of inbox div. Wait a bit for the rest of the page to load.
  window.setTimeout(forceLoadInbox, CLEAR_INBOX_INTERVAL_MILLIS);
  // Periodically check for and hide new comment notifications, everywhere
  window.setInterval(hideInboxComments, CLEAR_INBOX_INTERVAL_MILLIS);
})();