-
Notifications
You must be signed in to change notification settings - Fork 813
/
content_cosmetic.ts
572 lines (518 loc) · 18.3 KB
/
content_cosmetic.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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
// Copyright (c) 2020 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// you can obtain one at http://mozilla.org/MPL/2.0/.
// Notify the background script as soon as the content script has loaded.
// chrome.tabs.insertCSS may sometimes fail to inject CSS in a newly navigated
// page when using the chrome.webNavigation API.
// See: https://bugs.chromium.org/p/chromium/issues/detail?id=331654#c15
// The RenderView should always be ready when the content script begins, so
// this message is used to trigger CSS insertion instead.
chrome.runtime.sendMessage({
type: 'contentScriptsLoaded',
location: window.location
})
const parseDomain = require('parse-domain')
// Start looking for things to unhide before at most this long after
// the backend script is up and connected (eg backgroundReady = true),
// or sooner if the thread is idle.
const maxTimeMSBeforeStart = 2500
// The cutoff for text ads. If something has only text in it, it needs to have
// this many, or more, characters. Similarly, require it to have a non-trivial
// number of words in it, to look like an actual text ad.
const minAdTextChars = 30
const minAdTextWords = 5
const queriedIds = new Set<string>()
const queriedClasses = new Set<string>()
// Each of these get setup once the mutation observer starts running.
let notYetQueriedClasses: string[]
let notYetQueriedIds: string[]
let cosmeticObserver: MutationObserver | undefined = undefined
const allSelectorsToRules = new Map<string, number>()
const cosmeticStyleSheet = new CSSStyleSheet()
function injectScriptlet (text: string) {
let script
try {
script = document.createElement('script')
const textnode: Text = document.createTextNode(text)
script.appendChild(textnode);
(document.head || document.documentElement).appendChild(script)
} catch (ex) {
/* Unused catch */
}
if (script) {
if (script.parentNode) {
script.parentNode.removeChild(script)
}
script.textContent = ''
}
}
/**
* Provides a new function which can only be scheduled once at a time.
*
* @param onIdle function to run when the thread is less busy
* @param timeout max time to wait. at or after this time the function will be run regardless of thread noise
*/
function idleize (onIdle: Function, timeout: number) {
let idleId: number | undefined = undefined
return function WillRunOnIdle () {
if (idleId !== undefined) {
return
}
idleId = window.requestIdleCallback(() => {
idleId = undefined
onIdle()
}, { timeout })
}
}
function isRelativeUrl (url: string): boolean {
return (
!url.startsWith('//') &&
!url.startsWith('http://') &&
!url.startsWith('https://')
)
}
function isElement (node: Node): boolean {
return (node.nodeType === 1)
}
function asElement (node: Node): Element | null {
return isElement(node) ? node as Element : null
}
function isHTMLElement (node: Node): boolean {
return ('innerText' in node)
}
function asHTMLElement (node: Node): HTMLElement | null {
return isHTMLElement(node) ? node as HTMLElement : null
}
const fetchNewClassIdRules = function () {
if ((!notYetQueriedClasses || notYetQueriedClasses.length === 0) &&
(!notYetQueriedIds || notYetQueriedIds.length === 0)) {
return
}
chrome.runtime.sendMessage({
type: 'hiddenClassIdSelectors',
classes: notYetQueriedClasses || [],
ids: notYetQueriedIds || []
})
notYetQueriedClasses = []
notYetQueriedIds = []
}
const handleMutations: MutationCallback = function (mutations: MutationRecord[]) {
for (const aMutation of mutations) {
if (aMutation.type === 'attributes') {
// Since we're filtering for attribute modifications, we can be certain
// that the targets are always HTMLElements, and never TextNode.
const changedElm = aMutation.target as Element
switch (aMutation.attributeName) {
case 'class':
for (const aClassName of changedElm.classList.values()) {
if (queriedClasses.has(aClassName) === false) {
notYetQueriedClasses.push(aClassName)
queriedClasses.add(aClassName)
}
}
break
case 'id':
const mutatedId = changedElm.id
if (queriedIds.has(mutatedId) === false) {
notYetQueriedIds.push(mutatedId)
queriedIds.add(mutatedId)
}
break
}
} else if (aMutation.addedNodes.length > 0) {
for (const node of aMutation.addedNodes) {
const element = asElement(node)
if (!element) {
continue
}
const id = element.id
if (id && !queriedIds.has(id)) {
notYetQueriedIds.push(id)
queriedIds.add(id)
}
const classList = element.classList
if (classList) {
for (const className of classList.values()) {
if (className && !queriedClasses.has(className)) {
notYetQueriedClasses.push(className)
queriedClasses.add(className)
}
}
}
}
}
}
fetchNewClassIdRules()
}
const _parseDomainCache = Object.create(null)
const getParsedDomain = (aDomain: string) => {
const cacheResult = _parseDomainCache[aDomain]
if (cacheResult !== undefined) {
return cacheResult
}
const newResult = parseDomain(aDomain)
_parseDomainCache[aDomain] = newResult
return newResult
}
const _parsedCurrentDomain = getParsedDomain(window.location.host)
const isFirstPartyUrl = (url: string): boolean => {
if (isRelativeUrl(url)) {
return true
}
const parsedTargetDomain = getParsedDomain(url)
if (!parsedTargetDomain) {
// If we cannot determine the party-ness of the resource,
// consider it first-party.
console.debug(`Cosmetic filtering: Unable to determine party-ness of "${url}"`)
return false
}
return (
_parsedCurrentDomain.tld === parsedTargetDomain.tld &&
_parsedCurrentDomain.domain === parsedTargetDomain.domain
)
}
const isAdText = (text: string): boolean => {
const trimmedText = text.trim()
if (trimmedText.length < minAdTextChars) {
return false
}
if (trimmedText.split(' ').length < minAdTextWords) {
return false
}
return true
}
interface IsFirstPartyQueryResult {
foundFirstPartyResource: boolean,
foundThirdPartyResource: boolean,
foundKnownThirdPartyAd: boolean
}
/**
* Determine whether a subtree should be considered as "first party" content.
*
* Uses the following process in making this determination.
* - If the subtree contains any first party resources, the subtree is 1p.
* - If the subtree contains no remote resources, the subtree is first party.
* - Otherwise, its 3rd party.
*
* Note that any instances of "url(" or escape characters in style attributes
* are automatically treated as third-party URLs. These patterns and special
* cases were generated from looking at patterns in ads with resources in the
* style attribute.
*
* Similarly, an empty srcdoc attribute is also considered 3p, since many
* third party ads clear this attribute in practice.
*
* Finally, special case some ids we know are used only for third party ads.
*/
const isSubTreeFirstParty = (elm: Element, possibleQueryResult?: IsFirstPartyQueryResult): boolean => {
let queryResult: IsFirstPartyQueryResult
let isTopLevel: boolean
if (possibleQueryResult) {
queryResult = possibleQueryResult
isTopLevel = false
} else {
queryResult = {
foundFirstPartyResource: false,
foundThirdPartyResource: false,
foundKnownThirdPartyAd: false
}
isTopLevel = true
}
if (elm.getAttribute) {
if (elm.hasAttribute('id')) {
const elmId = elm.getAttribute('id') as string
if (elmId.startsWith('google_ads_iframe_') ||
elmId.startsWith('div-gpt-ad') ||
elmId.startsWith('adfox_')) {
queryResult.foundKnownThirdPartyAd = true
return false
}
}
if (elm.hasAttribute('src')) {
const elmSrc = elm.getAttribute('src') as string
const elmSrcIsFirstParty = isFirstPartyUrl(elmSrc)
if (elmSrcIsFirstParty === true) {
queryResult.foundFirstPartyResource = true
return true
}
queryResult.foundThirdPartyResource = true
}
if (elm.hasAttribute('style')) {
const elmStyle = elm.getAttribute('style') as string
if (elmStyle.includes('url(') ||
elmStyle.includes('//')) {
queryResult.foundThirdPartyResource = true
}
}
if (elm.hasAttribute('srcdoc')) {
const elmSrcDoc = elm.getAttribute('srcdoc') as string
if (elmSrcDoc.trim() === '') {
queryResult.foundThirdPartyResource = true
}
}
}
if (elm.firstChild) {
isSubTreeFirstParty(elm.firstChild as Element, queryResult)
if (queryResult.foundKnownThirdPartyAd === true) {
return false
}
if (queryResult.foundFirstPartyResource === true) {
return true
}
}
if (elm.nextSibling) {
isSubTreeFirstParty(elm.nextSibling as Element, queryResult)
if (queryResult.foundKnownThirdPartyAd === true) {
return false
}
if (queryResult.foundFirstPartyResource === true) {
return true
}
}
if (isTopLevel === false) {
return (queryResult.foundThirdPartyResource === false)
}
if (queryResult.foundThirdPartyResource) {
return false
}
const htmlElement = asHTMLElement(elm)
if (!htmlElement || isAdText(htmlElement.innerText) === false) {
return false
}
return true
}
const unhideSelectors = (selectors: Set<string>) => {
if (selectors.size === 0) {
return
}
// Find selectors we have a rule index for
const rulesToRemove = Array.from(selectors)
.map(selector =>
allSelectorsToRules.get(selector)
)
.filter(i => i !== undefined)
.sort()
.reverse()
// Delete the rules
let lastIdx: number = allSelectorsToRules.size - 1
for (const ruleIdx of rulesToRemove) {
cosmeticStyleSheet.deleteRule(ruleIdx)
}
// Re-sync the indexes
// TODO: Sync is hard, just re-build by iterating through the StyleSheet rules.
const ruleLookup = Array.from(allSelectorsToRules.entries())
let countAtLastHighest = rulesToRemove.length
for (let i = lastIdx; i > 0; i--) {
const [selector, oldIdx] = ruleLookup[i]
// Is this one we removed?
if (rulesToRemove.includes(i)) {
allSelectorsToRules.delete(selector)
countAtLastHighest--
if (countAtLastHighest === 0) {
break
}
continue
}
if (oldIdx !== i) {
// Probably out of sync
console.error('Cosmetic Filters: old index did not match lookup index', { selector, oldIdx, i })
}
allSelectorsToRules.set(selector, oldIdx - countAtLastHighest)
}
}
const alreadyUnhiddenSelectors = new Set<string>()
const alreadyKnownFirstPartySubtrees = new WeakSet()
// All new selectors go in `firstRunQueue`
const firstRunQueue = new Set<string>()
// Third party matches go in the second and third queues.
const secondRunQueue = new Set<string>()
// Once a selector gets in to this queue, it's only evaluated for 1p content one
// more time.
const finalRunQueue = new Set<string>()
const allQueues = [firstRunQueue, secondRunQueue, finalRunQueue]
const numQueues = allQueues.length
const pumpIntervalMinMs = 40
const pumpIntervalMaxMs = 1000
const maxWorkSize = 60
let queueIsSleeping = false
/**
* Go through each of the 3 queues, only take 50 items from each one
* 1. Take 50 selects from the first queue with any items
* 2. Determine partyness of matched element:
* - If any are 3rd party, keep 'hide' rule and check again later in next queue.
* - If any are 1st party, remove 'hide' rule and never check selector again.
* 3. If we're looking at the 3rd queue, don't requeue any selectors.
*/
const pumpCosmeticFilterQueues = () => {
if (queueIsSleeping === true) {
return
}
let didPumpAnything = false
// For each "pump", walk through each queue until we find selectors
// to evaluate. This means that nothing in queue N+1 will be evaluated
// until queue N is completely empty.
for (let queueIndex = 0; queueIndex < numQueues; queueIndex += 1) {
const currentQueue = allQueues[queueIndex]
const nextQueue = allQueues[queueIndex + 1]
if (currentQueue.size === 0) {
continue
}
const currentWorkLoad = Array.from(currentQueue.values()).slice(0, maxWorkSize)
const comboSelector = currentWorkLoad.join(',')
const matchingElms = document.querySelectorAll(comboSelector)
// Will hold selectors identified by _this_ queue pumping, that were
// newly identified to be matching 1p content. Will be sent to
// the background script to do the un-hiding.
const newlyIdentifiedFirstPartySelectors = new Set<string>()
for (const aMatchingElm of matchingElms) {
// Don't recheck elements / subtrees we already know are first party.
// Once we know something is third party, we never need to evaluate it
// again.
if (alreadyKnownFirstPartySubtrees.has(aMatchingElm)) {
continue
}
const elmSubtreeIsFirstParty = isSubTreeFirstParty(aMatchingElm)
// If we find that a subtree is third party, then no need to change
// anything, leave the selector as "hiding" and move on.
// This element will likely be checked again on the next 'pump'
// as long as another element from the selector does not match 1st party.
if (elmSubtreeIsFirstParty === false) {
continue
}
// Otherwise, we know that the given subtree was evaluated to be
// first party, so we need to figure out which selector from the combo
// selector did the matching.
for (const selector of currentWorkLoad) {
if (aMatchingElm.matches(selector) === false) {
continue
}
// Similarly, if we already know a selector matches 1p content,
// there is no need to notify the background script again, so
// we don't need to consider further.
if (alreadyUnhiddenSelectors.has(selector) === true) {
continue
}
newlyIdentifiedFirstPartySelectors.add(selector)
alreadyUnhiddenSelectors.add(selector)
}
alreadyKnownFirstPartySubtrees.add(aMatchingElm)
}
unhideSelectors(newlyIdentifiedFirstPartySelectors)
for (const aUsedSelector of currentWorkLoad) {
currentQueue.delete(aUsedSelector)
// Don't requeue selectors we know identify first party content.
const selectorMatchedFirstParty = newlyIdentifiedFirstPartySelectors.has(aUsedSelector)
if (nextQueue && selectorMatchedFirstParty === false) {
nextQueue.add(aUsedSelector)
}
}
didPumpAnything = true
// If we did something, process the next queue, save it for next time.
break
}
if (didPumpAnything) {
queueIsSleeping = true
window.setTimeout(() => {
// Set this to false now even though there's a gap in time between now and
// idle since all other calls to `pumpCosmeticFilterQueuesOnIdle` that occur during this time
// will be ignored (and nothing else should be calling `pumpCosmeticFilterQueues` straight).
queueIsSleeping = false
// tslint:disable-next-line:no-use-before-declare
pumpCosmeticFilterQueuesOnIdle()
}, pumpIntervalMinMs)
}
}
const pumpCosmeticFilterQueuesOnIdle = idleize(
pumpCosmeticFilterQueues,
pumpIntervalMaxMs
)
const startObserving = () => {
// First queue up any classes and ids that exist before the mutation observer
// starts running.
const elmWithClassOrId = document.querySelectorAll('[class],[id]')
for (const elm of elmWithClassOrId) {
for (const aClassName of elm.classList.values()) {
queriedClasses.add(aClassName)
}
const elmId = elm.getAttribute('id')
if (elmId) {
queriedIds.add(elmId)
}
}
notYetQueriedClasses = Array.from(queriedClasses)
notYetQueriedIds = Array.from(queriedIds)
fetchNewClassIdRules()
// Second, set up a mutation observer to handle any new ids or classes
// that are added to the document.
cosmeticObserver = new MutationObserver(handleMutations)
let observerConfig = {
subtree: true,
childList: true,
attributeFilter: ['id', 'class']
}
cosmeticObserver.observe(document.documentElement, observerConfig)
}
let _hasDelayOcurred: boolean = false
let _startCheckingId: number | undefined = undefined
const scheduleQueuePump = (hide1pContent: boolean, generichide: boolean) => {
// Three states possible here. First, the delay has already occurred. If so,
// pass through to pumpCosmeticFilterQueues immediately.
if (_hasDelayOcurred === true) {
pumpCosmeticFilterQueuesOnIdle()
return
}
// Second possibility is that we're already waiting for the delay to pass /
// occur. In this case, do nothing.
if (_startCheckingId !== undefined) {
return
}
// Third / final possibility, this is this the first time this has been
// called, in which case set up a timmer and quit
_startCheckingId = window.requestIdleCallback(function ({ didTimeout }) {
_hasDelayOcurred = true
if (!generichide) {
startObserving()
}
if (!hide1pContent) {
pumpCosmeticFilterQueuesOnIdle()
}
}, { timeout: maxTimeMSBeforeStart })
}
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
const action = typeof msg === 'string' ? msg : msg.type
switch (action) {
case 'cosmeticFilteringBackgroundReady': {
if (msg.hideOptions !== undefined) {
scheduleQueuePump(msg.hideOptions.hide1pContent, msg.hideOptions.generichide)
}
injectScriptlet(msg.scriptlet)
break
}
case 'cosmeticFilterConsiderNewSelectors': {
const { selectors } = msg
let nextIndex = cosmeticStyleSheet.rules.length
for (const selector of selectors) {
if (allSelectorsToRules.has(selector)) {
continue
}
// insertRule always adds to index 0,
// so we always add to end of list manually.
cosmeticStyleSheet.insertRule(
`${selector}{display:none !important;}`,
nextIndex
)
allSelectorsToRules.set(selector, nextIndex)
nextIndex++
firstRunQueue.add(selector)
}
// @ts-ignore
if (!document.adoptedStyleSheets.includes(cosmeticStyleSheet)) {
// @ts-ignore
document.adoptedStyleSheets = [cosmeticStyleSheet]
}
scheduleQueuePump(false, false)
break
}
}
})