-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtwitter_hide_promoted.user.js
70 lines (64 loc) · 2.97 KB
/
twitter_hide_promoted.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// ==UserScript==
// @name Twitter: hide promoted tweets
// @name:fr Twitter : masque les tweets sponsorisés
// @namespace http://tampermonkey.net/
// @version 0.3.1
// @description Hide promoted tweets
// @description:fr Masque les tweets sponsorisés
// @author Darth Obvious
// @match *://twitter.com/*
// @match *://*.twitter.com/*
// @match *://x.com/*
// @match *://*.x.com/*
// @license MIT
// @grant none
// ==/UserScript==
(function() {
"use strict";
let count_hidden = 0;
const promoted_texts = ["Ad", "Promoted", "Gesponsert", "Promocionado", "Sponsorisé", "Sponsorizzato", "Promowane", "Promovido", "Реклама", "Uitgelicht", "Sponsorlu", "Promotert", "Promoveret", "Sponsrad", "Mainostettu", "Sponzorováno", "Promovat", "Ajánlott", "Προωθημένο", "Dipromosikan", "Được quảng bá", "推廣", "推广", "推薦", "推荐", "プロモーション", "프로모션", "ประชาสัมพันธ์", "प्रचारित", "বিজ্ঞাপিত", "تشہیر شدہ", "مُروَّج", "تبلیغی", "מקודם"];
function hide_promoted(){
// Select all <article> not processed
const unprocessedArticles = document.querySelectorAll('article:not([data-processed])');
// Loop on all articles
unprocessedArticles.forEach(article => {
article.setAttribute('data-processed', 'true');
// Search all divs
const divs = article.querySelectorAll('div');
// Test if a div contains only the "promoted" text
let containsAd = false;
divs.forEach(div => {
const spans = div.querySelectorAll('span');
spans.forEach(span => {
if (promoted_texts.includes(span.textContent.trim())) {
containsAd = true;
}
});
});
// If it looks like a promoted tweet, it's hidden
if (containsAd) {
article.style.display = 'none';
count_hidden += 1;
console.log("Promoted tweet hidden (" + count_hidden + ")");
}
});
}
// MutationObserver to observe changes in <body>
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
mutation.addedNodes.forEach(function(node) {
// Is this node a <div>?
if (node instanceof HTMLDivElement) {
// Check all children of the <div> to find <article> tags
const articlesInDiv = node.querySelectorAll('article');
if (articlesInDiv.length > 0) {
hide_promoted();
}
}
});
});
});
// Observer for changes in <body>
const observerConfig = { childList: true, subtree: true };
observer.observe(document.body, observerConfig);
})();