Collapse ZH Premium Comments

Automatically collapses ZeroHedge premium comments.

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램을 설치해야 합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

// ==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,
  });
})();