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