-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
291 lines (253 loc) · 7.08 KB
/
content.js
File metadata and controls
291 lines (253 loc) · 7.08 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
const SELECTORS = {
TV_SOCIEDAD: '[data-flex-name="portada/flex-list-ad-2"]',
PRENSA: '[data-flex-name="portada/flex-list-ad-bt"]',
APUESTAS: '.ue-l-cover-grid.flex-territorio-apuestas',
MARCA_TV: '.ue-l-cover-grid.flex-video-marca-tv',
SPONSORED: '[aria-roledescription="Publicidad"]',
SPONSORED_ARTICLE: '.ue-c-cover-content--is-branded',
EMBEDDED_VIDEOS: '.ue-c-cover-content__video-duration-icon',
AUTOPLAY_IFRAMES: 'iframe[allow*="autoplay"]',
VIDEO_AUTOPLAY: '.ue-c-video-player-frame',
ARTICLE_SECONDARY_COLUMN: '.ue-l-article__secondary-column',
}
const DEFAULTS = {
enabled: true,
sectionToggles: {
TV_SOCIEDAD: true,
PRENSA: true,
APUESTAS: true,
MARCA_TV: true,
SPONSORED: true,
EMBEDDED_VIDEOS: true,
AUTOPLAY_IFRAMES: true,
VIDEO_AUTOPLAY: true,
ARTICLE_SECONDARY_COLUMN: true,
},
authors: [],
keywords: [],
kickers: [],
}
let settingsCache = null
let observer = null
let styleTag = null
let debounceTimer = null
const normalizeString = (string) => (string || '').trim().toLowerCase()
const buildCssFromToggles = (s) => {
if (!s.enabled) {
return ''
}
const hideList = []
for (const [key, on] of Object.entries(s.sectionToggles)) {
if (key === 'EMBEDDED_VIDEOS' || key === 'AUTOPLAY_IFRAMES') {
continue
}
if (on && SELECTORS[key]) {
hideList.push(SELECTORS[key])
}
}
return hideList.length
? `${hideList.join(', ')} { display: none !important; }`
: ''
}
const ensureStyle = (css) => {
if (!styleTag) {
styleTag = document.createElement('style')
styleTag.id = 'marca-block-style'
;(document.head || document.documentElement).appendChild(styleTag)
}
if (styleTag.textContent !== css) {
styleTag.textContent = css
}
}
const getArticleUnit = (el) => {
if (!el) {
return null
}
const gridUnit = el.closest('.ue-l-cover-grid__unit')
if (gridUnit) {
return gridUnit
}
const mediaWrapper = el.closest('.ue-c-article__media')
if (mediaWrapper) {
return mediaWrapper
}
return null
}
const hideElement = (el) => {
if (!el) {
return 0
}
if (el.style.display === 'none') {
return 0
}
el.style.setProperty('display', 'none', 'important')
return 1
}
const blockByList = (units, checkFn, list) => {
let count = 0
if (!list || list.length === 0) {
return count
}
const normList = list.map(normalizeString).filter(Boolean)
units.forEach((unit) => {
if (unit.style.display !== 'none' && checkFn(unit, normList)) {
count += hideElement(unit)
}
})
return count
}
//to block by authors
const checkAuthor = (unit, normList) => {
const byline = unit.querySelector('.ue-c-cover-content__byline-name')
if (byline && byline.textContent) {
if (normList.some((a) => normalizeString(byline.textContent).includes(a))) {
return true
}
}
const spans = unit.querySelectorAll('article span')
for (const sp of spans) {
if (normList.some((a) => normalizeString(sp.textContent).includes(a))) {
return true
}
}
return false
}
//to block by headline keywords
const checkWordsInHeadline = (unit, normList) => {
const headline = unit.querySelector('.ue-c-cover-content__headline')
return (
headline &&
normList.some((k) => normalizeString(headline.textContent).includes(k))
)
}
//to block by headline kicker keywords
const checkWordsInKicker = (unit, normList) => {
const kicker = unit.querySelector('.ue-c-cover-content__kicker')
return (
kicker &&
normList.some((k) => normalizeString(kicker.textContent).includes(k))
)
}
//to block embedded videos
const blockEmbeddedVideos = () => {
let count = 0
document.querySelectorAll(SELECTORS.EMBEDDED_VIDEOS).forEach((icon) => {
const unit = getArticleUnit(icon)
if (unit && unit.style.display !== 'none') {
count += hideElement(unit)
}
})
return count
}
//to block extra sponsored articles and content
const blockSponsoredContent = () => {
let count = 0
const combinedSelector = `${SELECTORS.SPONSORED_ARTICLE}, .publicidad, .sm-it-main-container, .teads-inread, .teads-ui-components-adchoices, .trc_spotlight_widget, .GoogleActiveViewInnerContainer, .commercial`
document.querySelectorAll(combinedSelector).forEach((element) => {
let unitToHide = getArticleUnit(element)
if (!unitToHide) {
unitToHide = element
}
if (unitToHide && unitToHide.style.display !== 'none') {
count += hideElement(unitToHide)
}
})
return count
}
//to block autoplay iFrames
const blockAutoplayIframes = () => {
let count = 0
document.querySelectorAll(SELECTORS.AUTOPLAY_IFRAMES).forEach((iframe) => {
const unit = getArticleUnit(iframe)
if (unit && unit.style.display !== 'none') {
count += hideElement(unit)
}
})
return count
}
const countCssHiddenElements = (settings) => {
let count = 0
for (const [key, on] of Object.entries(settings.sectionToggles)) {
if (key === 'EMBEDDED_VIDEOS' || key === 'AUTOPLAY_IFRAMES') {
continue
}
if (on && SELECTORS[key]) {
const elements = document.querySelectorAll(SELECTORS[key])
count += elements.length
}
}
return count
}
const loadSettings = async () => {
const { settings } = await chrome.storage.local.get('settings')
if (!settings) {
return DEFAULTS
}
return {
...DEFAULTS,
...settings,
sectionToggles: {
...DEFAULTS.sectionToggles,
...(settings.sectionToggles || {}),
},
}
}
const applyAll = () => {
if (!settingsCache || !settingsCache.enabled) {
ensureStyle('')
chrome.runtime
.sendMessage({ type: 'blocked-count', count: 0 })
.catch(() => {})
return
}
let blockedCount = 0
const css = buildCssFromToggles(settingsCache)
ensureStyle(css)
blockedCount += countCssHiddenElements(settingsCache)
const units = Array.from(document.querySelectorAll('.ue-l-cover-grid__unit'))
blockedCount += blockByList(units, checkAuthor, settingsCache.authors)
blockedCount += blockByList(
units,
checkWordsInHeadline,
settingsCache.keywords
)
blockedCount += blockByList(units, checkWordsInKicker, settingsCache.kickers)
if (settingsCache.sectionToggles.SPONSORED) {
blockedCount += blockSponsoredContent()
}
if (settingsCache.sectionToggles.EMBEDDED_VIDEOS) {
blockedCount += blockEmbeddedVideos()
}
if (settingsCache.sectionToggles.AUTOPLAY_IFRAMES) {
blockedCount += blockAutoplayIframes()
}
chrome.runtime
.sendMessage({ type: 'blocked-count', count: blockedCount })
.catch(() => {})
}
const startObserver = () => {
if (observer) {
observer.disconnect()
}
observer = new MutationObserver(() => {
if (settingsCache?.enabled) {
clearTimeout(debounceTimer)
debounceTimer = setTimeout(applyAll, 200)
}
})
observer.observe(document.documentElement, { subtree: true, childList: true })
}
const init = async () => {
settingsCache = await loadSettings()
applyAll()
startObserver()
chrome.runtime.onMessage.addListener((msg) => {
if (msg?.type === 'settings-updated') {
loadSettings().then((s) => {
settingsCache = s
applyAll()
})
}
})
}
init()