generated from antfu-collective/vitesse-webext
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweetProcessors.ts
103 lines (86 loc) · 3.93 KB
/
tweetProcessors.ts
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import type { TweetProcessor } from './processTweet'
import { hideTweet } from './hideTweet'
export const hideTweetWithKeyword: TweetProcessor = {
run(element: HTMLElement, { keywords, showPlaceholderForHiddenTweet }) {
const tweetText = element.querySelector('[data-testid="tweetText"]')?.innerText
const tweetHasKeyword
= tweetText
&& keywords.some(
(keyword: string) => keyword && tweetText?.toLowerCase().includes(keyword.toLowerCase()),
)
if (tweetHasKeyword) {
hideTweet(element, showPlaceholderForHiddenTweet, 'Tweet with keyword')
return true
}
return false
},
}
export const hideTwitterAds: TweetProcessor = {
run(element: HTMLElement, { hideTwitterAds, showPlaceholderForHiddenAds }) {
const isAnAd = element.querySelector('[data-testid="tweet"]')?.innerHTML.includes('>Ad</')
if (isAnAd && hideTwitterAds) {
hideTweet(element, showPlaceholderForHiddenAds, 'Ad')
return true
}
const hasPlacementTracking = element.querySelector('[data-testid="placementTracking"]')
const hasSponsoredIcon = element?.innerHTML.includes('M19.498 3h-15c-1.381 0-2.5 1.12-2.5 2.5v13c0 1.38 1.119 2.5 2.5 2.5h15c1.381 0 2.5-1.12 2.5-2.5v-13c0-1.38-1.119-2.5-2.5-2.5zm-3.502 12h-2v-3.59l-5.293 5.3-1.414-1.42L12.581 10H8.996V8h7v7z')
const isAnSponsoredTweet = hasPlacementTracking && hasSponsoredIcon
if (isAnSponsoredTweet && hideTwitterAds) {
hideTweet(element, showPlaceholderForHiddenAds, 'Sponsored Tweet')
return true
}
return false
},
}
export const hidePeopleToFollowSuggestion: TweetProcessor = {
run(element: HTMLElement, { hidePeopleToFollowSuggestion, showPlaceholderForHiddenAds }) {
if (!hidePeopleToFollowSuggestion)
return true
const hasPeopleToFollow = (node: HTMLElement) => {
return (
node?.querySelector('[data-testid$="follow"]')
&& node?.querySelector('[data-testid="UserCell"]')
)
}
const hidePeopleToFollowTitleAndSeeMore = (node: HTMLElement) => {
const nextSibling = node.nextSibling as HTMLElement
const previousSibling = node.previousSibling as HTMLElement | null
const previousTwoSibling = node.previousSibling?.previousSibling as HTMLElement | null
const previousThreeSiblings = node.previousSibling?.previousSibling?.previousSibling as HTMLElement
const connectPeopleLink = nextSibling?.querySelector(
'a[href^=\'/i/connect_people?user_id=\']',
)
if (previousSibling && previousTwoSibling && connectPeopleLink) {
hideTweet(previousThreeSiblings, showPlaceholderForHiddenAds, 'People to follow title')
hideTweet(nextSibling, showPlaceholderForHiddenAds, 'See more link')
}
}
if (hasPeopleToFollow(element)) {
hideTweet(element, showPlaceholderForHiddenAds, 'People to follow')
hidePeopleToFollowTitleAndSeeMore(element)
return true
}
const peopleToFollowSvg
= 'M17.863 13.44c1.477 1.58 2.366 3.8 2.632 6.46l.11 1.1H3.395l.11-1.1c.266-2.66 1.155-4.88 2.632-6.46C7.627 11.85 9.648 11 12 11s4.373.85 5.863 2.44zM12 2C9.791 2 8 3.79 8 6s1.791 4 4 4 4-1.79 4-4-1.791-4-4-4z'
const tweetText = element.querySelector('[data-testid="tweetText"]')?.innerText
const tweetPhoto = element.querySelector('[data-testid="tweetPhoto"]')?.innerHTML
if (element.innerHTML.includes(peopleToFollowSvg) && !(tweetText || tweetPhoto)) {
hideTweet(element, showPlaceholderForHiddenAds, 'People to follow')
return true
}
return false
},
}
export const hideSubscriptionSuggesstion: TweetProcessor = {
run(element: HTMLElement, { hideSubscriptionSuggesstion, showPlaceholderForHiddenAds }) {
if (
hideSubscriptionSuggesstion
&& element.querySelector('[data-testid$="subscribe"]')
&& element.querySelector('[data-testid="UserCell"]')
) {
hideTweet(element, showPlaceholderForHiddenAds, 'Subcribe suggestion')
return true
}
return false
},
}