Skip to content

Instantly share code, notes, and snippets.

@quannt
Last active April 28, 2025 03:18
Show Gist options
  • Save quannt/c55a14a594687a1e51643144424e15b9 to your computer and use it in GitHub Desktop.
Save quannt/c55a14a594687a1e51643144424e15b9 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Threads.com Keyword Filter
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Hide posts on Threads.com containing specified keywords
// @author quannt
// @match https://www.threads.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// 1. List your keywords here (case-insensitive)
const keywords = [
"Trump"
];
// 2. Helper: Check if a post contains any keyword
function containsKeyword(text) {
return keywords.some(keyword =>
text.toLowerCase().includes(keyword.toLowerCase())
);
}
// 3. Hide posts containing keywords
function filterPosts() {
// Update this selector to match Threads.com post containers
const columnBody = document.querySelector('div[aria-label="Column body"]');
if (!columnBody) return;
const container = columnBody.querySelector('.x78zum5.xdt5ytf.x1iyjqo2.x1n2onr6')
// Select only direct child divs with the target classes
const posts = container.querySelectorAll('.x78zum5.xdt5ytf');
function exactMatches(el) {
console.log(el.classList)
return el.classList.length === 2
}
Array.from(posts).filter(exactMatches).forEach(post => {
if (!post.dataset.filtered) { // Avoid double-filtering
const postText = post.innerText || post.textContent;
if (containsKeyword(postText)) {
post.style.display = 'none';
//post.style.backgroundColor = 'red';
}
post.dataset.filtered = "true";
}
});
}
// 4. Run filter on page load and whenever new posts are added
function observePosts() {
const observer = new MutationObserver(filterPosts);
observer.observe(document.body, { childList: true, subtree: true });
}
// 5. Initial run
window.addEventListener('load', () => {
filterPosts();
observePosts();
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment