Greasy Fork is available in English.
Hide X.com (Twitter) replies that aren't from the same author. Still shows threads.
当前为
// ==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 });
})();