Greasy Fork

Greasy Fork is available in English.

Hide Reply Tweets from Timeline

Hide X.com (Twitter) replies that aren't from the same author. Still shows threads.

当前为 2024-09-19 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Hide Reply Tweets from Timeline
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  Hide X.com (Twitter) replies that aren't from the same author. Still shows threads.
// @author       
// @match        https://x.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    /**
     * Hides reply tweets from other authors in the conversation timeline.
     */
    function hideElements() {
        // Select the conversation container
        const conversationContainer = document.querySelector('div[data-testid="primaryColumn"] section');
        if (!conversationContainer) return;

        // Get all tweet articles within the conversation
        const articles = conversationContainer.querySelectorAll('article');
        if (!articles.length) return;

        // Extract the main author's username from the first tweet
        const mainAuthor = getAuthorUsername(articles[0]);
        if (!mainAuthor) return;

        // Loop through each tweet in the conversation
        articles.forEach((article, index) => {
            if (index === 0) return; // Skip the main tweet

            const author = getAuthorUsername(article);
            if (author !== mainAuthor) {
                article.style.display = 'none'; // Hide tweets from other authors
            }
        });
    }

    /**
     * Extracts the author's username from a tweet article element.
     * @param {Element} article - The tweet article DOM element.
     * @returns {string|null} - The author's username or null if not found.
     */
    function getAuthorUsername(article) {
        // Locate the username element within the tweet
        const userNameDiv = article.querySelector('div[data-testid="User-Name"]');
        if (userNameDiv) {
            const profileLink = userNameDiv.querySelector('a[href^="/"]');
            if (profileLink) {
                const href = profileLink.getAttribute('href');
                const username = href.split('/')[1]; // Extract username from URL
                return username;
            }
        }
        return null;
    }

    // Execute the hideElements function when the DOM is fully loaded
    document.addEventListener('DOMContentLoaded', hideElements);

    // Set up a MutationObserver to monitor dynamic content changes
    const observer = new MutationObserver(hideElements);
    observer.observe(document.body, { childList: true, subtree: true });
})();