Collapse ZH Premium Comments

Automatically collapses ZeroHedge premium comments.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         Collapse ZH Premium Comments
// @version      1.0.0
// @license MIT
// @description  Automatically collapses ZeroHedge premium comments.
// @match        https://*.zerohedge.com/*
// @grant        none
// @run-at       document-idle
// @namespace https://greasyfork.org/users/1626718
// ==/UserScript==

(() => {
  'use strict';

  const BADGE_SELECTOR = '[class*="SubscriberBadge"]';
  const COMMENT_SELECTOR = '.talk-stream-comment-wrapper';
  const COLLAPSE_BUTTON_SELECTOR = '.talk-plugin-collapse-comment-button';
  const handledComments = new WeakSet();

  function collapsePremiumComments(root = document) {
    root.querySelectorAll(BADGE_SELECTOR).forEach((badge) => {
      const comment = badge.closest(COMMENT_SELECTOR);

      if (!comment || handledComments.has(comment)) return;

      const collapseButton = comment.querySelector(COLLAPSE_BUTTON_SELECTOR);
      if (!collapseButton) return;

      // Prevent script from opening a collapsed comment.
      const icon = collapseButton.querySelector('.material-icons');
      const isExpanded = !icon || icon.textContent.trim() === 'remove';

      handledComments.add(comment);

      if (isExpanded) collapseButton.click();
    });
  }

  collapsePremiumComments();

  const observer = new MutationObserver(() => collapsePremiumComments());
  observer.observe(document.body, {
    childList: true,
    subtree: true,
  });
})();