-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathtrusted-click-element.ts
574 lines (515 loc) · 20.3 KB
/
trusted-click-element.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
573
574
import {
hit,
toRegExp,
parseCookieString,
throttle,
logMessage,
parseMatchArg,
queryShadowSelector,
doesElementContainText,
findElementWithText,
} from '../helpers';
import { type Source } from './scriptlets';
/* eslint-disable max-len */
/**
* @trustedScriptlet trusted-click-element
*
* @description
* Clicks selected elements in a strict sequence, ordered by selectors passed,
* and waiting for them to render in the DOM first.
* First matched element is clicked unless `containsText` is specified.
* If `containsText` is specified, then it searches for all given selectors and clicks
* the first element containing the specified text.
* Deactivates after all elements have been clicked or by 10s timeout.
*
* ### Syntax
*
* ```text
* example.com#%#//scriptlet('trusted-click-element', selectors[, extraMatch[, delay[, reload]]])
* ```
* <!-- markdownlint-disable-next-line line-length -->
* - `selectors` — required, string with query selectors delimited by comma. The scriptlet supports `>>>` combinator to select elements inside open shadow DOM. For usage, see example below.
* - `extraMatch` — optional, extra condition to check on a page;
* allows to match `cookie`, `localStorage` and specified text;
* can be set as `name:key[=value]` where `value` is optional.
* If `cookie`/`localStorage` starts with `!` then the element will only be clicked
* if specified `cookie`/`localStorage` item does not exist.
* Multiple conditions are allowed inside one `extraMatch` but they should be delimited by comma
* and each of them should match the syntax. Possible `names`:
* - `cookie` — test string or regex against cookies on a page
* - `localStorage` — check if localStorage item is present
* - `containsText` — check if clicked element contains specified text
* - `delay` — optional, time in ms to delay scriptlet execution, defaults to instant execution.
* Must be a number less than 10000 ms (10s)
* - `reload` — optional, string with reloadAfterClick marker and optional value. Possible values:
* - `reloadAfterClick` - reloads the page after all elements have been clicked,
* with default delay — 500ms
* - colon-separated pair `reloadAfterClick:value` where
* - `value` — time delay in milliseconds before reloading the page, after all elements
* have been clicked. Must be a number less than 10000 ms (10s)
*
* <!-- markdownlint-disable line-length -->
*
* ### Examples
*
* 1. Click single element by selector
*
* ```adblock
* example.com#%#//scriptlet('trusted-click-element', 'button[name="agree"]')
* ```
*
* 1. Delay click execution by 500ms
*
* ```adblock
* example.com#%#//scriptlet('trusted-click-element', 'button[name="agree"]', '', '500')
* ```
*
* 1. Click multiple elements by selector with a delay
*
* <!-- markdownlint-disable line-length -->
*
* ```adblock
* example.com#%#//scriptlet('trusted-click-element', 'button[name="agree"], button[name="check"], input[type="submit"][value="akkoord"]', '', '500')
* ```
*
* 1. Match cookies by keys using regex and string
*
* ```adblock
* example.com#%#//scriptlet('trusted-click-element', 'button[name="agree"]', 'cookie:userConsentCommunity, cookie:/cmpconsent|cmp/')
* ```
*
* 1. Match by cookie key=value pairs using regex and string
*
* ```adblock
* example.com#%#//scriptlet('trusted-click-element', 'button[name="agree"]', 'cookie:userConsentCommunity=true, cookie:/cmpconsent|cmp/=/[a-z]{1,5}/')
* ```
*
* 1. Match by localStorage item 'promo' key
*
* ```adblock
* example.com#%#//scriptlet('trusted-click-element', 'button[name="agree"]', 'localStorage:promo')
* ```
*
* 1. Click multiple elements with delay and matching by both cookie string and localStorage item
*
* ```adblock
* example.com#%#//scriptlet('trusted-click-element', 'button[name="agree"], input[type="submit"][value="akkoord"]', 'cookie:cmpconsent, localStorage:promo', '250')
* ```
*
* 1. Click element only if clicked element contains text `Accept cookie`
*
* ```adblock
* example.com#%#//scriptlet('trusted-click-element', 'button', 'containsText:Accept cookie')
* ```
*
* 1. Click element only if cookie with name `cmpconsent` does not exist
*
* ```adblock
* example.com#%#//scriptlet('trusted-click-element', 'button[name="agree"]', '!cookie:cmpconsent')
* ```
*
* 1. Click element only if specified cookie string and localStorage item does not exist
*
* ```adblock
* example.com#%#//scriptlet('trusted-click-element', 'button[name="agree"]', '!cookie:consent, !localStorage:promo')
* ```
*
* 1. Click element inside open shadow DOM, which could be selected by `div > button`, but is inside shadow host element with host element selected by `article .container`
*
* ```adblock
* example.com#%#//scriptlet('trusted-click-element', 'article .container > div#host >>> div > button')
* ```
*
* 1. Click elements after 1000ms delay and reload page after all elements have been clicked with 200ms delay
*
* ```adblock
* example.com#%#//scriptlet('trusted-click-element', 'button[name="agree"], button[name="check"], input[type="submit"][value="akkoord"]', '', '1000', 'reloadAfterClick:200')
* ```
*
* <!-- markdownlint-enable line-length -->
*
* @added v1.7.3.
*/
/* eslint-enable max-len */
/**
* Object that contains information about element that can be clicked
*/
interface ElementObject {
/**
* HTML element to be clicked, or null if not found
*/
element: HTMLElement | null;
/**
* Indicates whether the element has been clicked
*/
clicked: boolean;
/**
* CSS selector text used to find the element
*/
selectorText: string | null;
}
export function trustedClickElement(
source: Source,
selectors: string,
extraMatch = '',
delay = NaN,
reload = '',
) {
if (!selectors) {
return;
}
const SHADOW_COMBINATOR = ' >>> ';
const OBSERVER_TIMEOUT_MS = 10000;
const THROTTLE_DELAY_MS = 20;
const STATIC_CLICK_DELAY_MS = 150;
const STATIC_RELOAD_DELAY_MS = 500;
const COOKIE_MATCH_MARKER = 'cookie:';
const LOCAL_STORAGE_MATCH_MARKER = 'localStorage:';
const TEXT_MATCH_MARKER = 'containsText:';
const RELOAD_ON_FINAL_CLICK_MARKER = 'reloadAfterClick';
const SELECTORS_DELIMITER = ',';
const COOKIE_STRING_DELIMITER = ';';
const COLON = ':';
// Regex to split match pairs by commas, avoiding the ones included in regexes
const EXTRA_MATCH_DELIMITER = /(,\s*){1}(?=!?cookie:|!?localStorage:|containsText:)/;
const sleep = (delayMs: number) => {
return new Promise((resolve) => { setTimeout(resolve, delayMs); });
};
// If shadow combinator is present in selector, then override attachShadow and set mode to 'open'
if (selectors.includes(SHADOW_COMBINATOR)) {
const attachShadowWrapper = (
target: typeof Element.prototype.attachShadow,
thisArg: Element,
argumentsList: any[],
) => {
const mode = argumentsList[0]?.mode;
if (mode === 'closed') {
argumentsList[0].mode = 'open';
}
return Reflect.apply(target, thisArg, argumentsList);
};
const attachShadowHandler = {
apply: attachShadowWrapper,
};
window.Element.prototype.attachShadow = new Proxy(window.Element.prototype.attachShadow, attachShadowHandler);
}
let parsedDelay;
if (delay) {
parsedDelay = parseInt(String(delay), 10);
const isValidDelay = !Number.isNaN(parsedDelay) || parsedDelay < OBSERVER_TIMEOUT_MS;
if (!isValidDelay) {
// eslint-disable-next-line max-len
const message = `Passed delay '${delay}' is invalid or bigger than ${OBSERVER_TIMEOUT_MS} ms`;
logMessage(source, message);
return;
}
}
let canClick = !parsedDelay;
const cookieMatches: string[] = [];
const localStorageMatches: string[] = [];
let textMatches = '';
let isInvertedMatchCookie = false;
let isInvertedMatchLocalStorage = false;
if (extraMatch) {
// Get all match marker:value pairs from argument
const parsedExtraMatch = extraMatch
.split(EXTRA_MATCH_DELIMITER)
.map((matchStr) => matchStr.trim());
// Filter match pairs by marker
parsedExtraMatch.forEach((matchStr) => {
if (matchStr.includes(COOKIE_MATCH_MARKER)) {
const { isInvertedMatch, matchValue } = parseMatchArg(matchStr);
isInvertedMatchCookie = isInvertedMatch;
const cookieMatch = matchValue.replace(COOKIE_MATCH_MARKER, '');
cookieMatches.push(cookieMatch);
}
if (matchStr.includes(LOCAL_STORAGE_MATCH_MARKER)) {
const { isInvertedMatch, matchValue } = parseMatchArg(matchStr);
isInvertedMatchLocalStorage = isInvertedMatch;
const localStorageMatch = matchValue.replace(LOCAL_STORAGE_MATCH_MARKER, '');
localStorageMatches.push(localStorageMatch);
}
if (matchStr.includes(TEXT_MATCH_MARKER)) {
const { matchValue } = parseMatchArg(matchStr);
const textMatch = matchValue.replace(TEXT_MATCH_MARKER, '');
textMatches = textMatch;
}
});
}
if (cookieMatches.length > 0) {
const parsedCookieMatches = parseCookieString(cookieMatches.join(COOKIE_STRING_DELIMITER));
const parsedCookies = parseCookieString(document.cookie);
const cookieKeys = Object.keys(parsedCookies);
if (cookieKeys.length === 0) {
return;
}
const cookiesMatched = Object.keys(parsedCookieMatches).every((key) => {
// Avoid getting /.?/ result from toRegExp on undefined
// as cookie may be set without value,
// on which cookie parsing will return cookieKey:undefined pair
const valueMatch = parsedCookieMatches[key] ? toRegExp(parsedCookieMatches[key]) : null;
const keyMatch = toRegExp(key);
return cookieKeys.some((cookieKey) => {
const keysMatched = keyMatch.test(cookieKey);
if (!keysMatched) {
return false;
}
// Key matching is enough if cookie value match is not specified
if (!valueMatch) {
return true;
}
const parsedCookieValue = parsedCookies[cookieKey];
if (!parsedCookieValue) {
return false;
}
return valueMatch.test(parsedCookieValue);
});
});
const shouldRun = cookiesMatched !== isInvertedMatchCookie;
if (!shouldRun) {
return;
}
}
if (localStorageMatches.length > 0) {
const localStorageMatched = localStorageMatches
.every((str) => {
const itemValue = window.localStorage.getItem(str);
return itemValue || itemValue === '';
});
const shouldRun = localStorageMatched !== isInvertedMatchLocalStorage;
if (!shouldRun) {
return;
}
}
const textMatchRegexp = textMatches ? toRegExp(textMatches) : null;
/**
* Create selectors array and swap selectors to null on finding it's element
*
* Selectors / nulls should not be (re)moved from array to:
* - keep track of selectors order
* - always know on what index corresponding element should be put
* - prevent selectors from being queried multiple times
*/
let selectorsSequence: Array<string | null> = selectors
.split(SELECTORS_DELIMITER)
.map((selector) => selector.trim());
const createElementObj = (element: any, selector?: string | null): Object => {
return {
element: element || null,
clicked: false,
selectorText: selector || null,
};
};
const elementsSequence = Array(selectorsSequence.length).fill(createElementObj(null));
/**
* Attempts to find and click an element based on the provided selector data.
*
* @param elementObj - Object containing element selector information
*
*/
const findAndClickElement = (elementObj: ElementObject): void => {
try {
if (!elementObj.selectorText) {
return;
}
const element = queryShadowSelector(elementObj.selectorText) as HTMLElement;
if (!element) {
logMessage(source, `Could not find element: '${elementObj.selectorText}'`);
return;
}
element.click();
elementObj.clicked = true;
} catch (error) {
logMessage(source, `Could not click element: '${elementObj.selectorText}'`);
}
};
// Flag indicating if the reload is set
let shouldReloadAfterClick: boolean = false;
// Value used for reload timing
let reloadDelayMs: number = STATIC_RELOAD_DELAY_MS;
if (reload) {
// split reload option by colon
const reloadSplit = reload.split(COLON);
const reloadMarker = reloadSplit[0];
const reloadValue = reloadSplit[1];
if (reloadMarker !== RELOAD_ON_FINAL_CLICK_MARKER) {
logMessage(source, `Passed reload option '${reload}' is invalid`);
return;
}
// if reload value is set, will be used as a delay
// if reload value is not set, default value will be used
if (reloadValue) {
const passedReload = Number(reloadValue);
// check if passed reload value is a number
if (Number.isNaN(passedReload)) {
logMessage(source, `Passed reload delay value '${passedReload}' is invalid`);
return;
}
// check if passed reload value is less than 10s
if (passedReload > OBSERVER_TIMEOUT_MS) {
// eslint-disable-next-line max-len
logMessage(source, `Passed reload delay value '${passedReload}' is bigger than maximum ${OBSERVER_TIMEOUT_MS} ms`);
return;
}
reloadDelayMs = passedReload;
}
shouldReloadAfterClick = true;
}
/**
* Go through elementsSequence from left to right, clicking on found elements
*
* Element should not be clicked if it is already clicked,
* or a previous element is not found or clicked yet
*/
let canReload = true;
const clickElementsBySequence = async () => {
for (let i = 0; i < elementsSequence.length; i += 1) {
const elementObj = elementsSequence[i];
// Add a delay between clicks to every element except the first one
// https://github.com/AdguardTeam/Scriptlets/issues/284
if (i >= 1) {
await sleep(STATIC_CLICK_DELAY_MS);
}
// Stop clicking if that pos element is not found yet
if (!elementObj.element) {
break;
}
// Skip already clicked elements
if (!elementObj.clicked) {
// Checks if node is connected to a Document object,
// if not, try to find the element again
// https://github.com/AdguardTeam/Scriptlets/issues/391
if (elementObj.element.isConnected) {
elementObj.element.click();
elementObj.clicked = true;
} else {
findAndClickElement(elementObj);
}
}
}
const allElementsClicked = elementsSequence
.every((elementObj) => elementObj.clicked === true);
if (allElementsClicked) {
if (shouldReloadAfterClick && canReload) {
canReload = false;
setTimeout(() => {
window.location.reload();
}, reloadDelayMs);
}
hit(source);
}
};
const handleElement = (element: Element, i: number, selector: string) => {
const elementObj = createElementObj(element, selector);
elementsSequence[i] = elementObj;
if (canClick) {
clickElementsBySequence();
}
};
/**
* Processes a sequence of selectors, handling elements found in DOM (and shadow DOM),
* and updates the sequence.
*
* @returns {string[]} The updated selectors sequence, with fulfilled selectors set to null.
*/
const fulfillAndHandleSelectors = () => {
const fulfilledSelectors: string[] = [];
selectorsSequence.forEach((selector, i) => {
if (!selector) {
return;
}
const element = queryShadowSelector(selector, document.documentElement, textMatchRegexp);
if (!element) {
return;
}
handleElement(element, i, selector);
fulfilledSelectors.push(selector);
});
// selectorsSequence should be modified after the loop to not break loop indexation
selectorsSequence = selectorsSequence.map((selector) => {
return selector && fulfilledSelectors.includes(selector)
? null
: selector;
});
return selectorsSequence;
};
/**
* Queries all selectors from queue on each mutation
*
* We start looking for elements before possible delay is over, to avoid cases
* when delay is getting off after the last mutation took place.
*
*/
const findElements = (mutations: MutationRecord[], observer: MutationObserver) => {
// TODO: try to make the function cleaner — avoid usage of selectorsSequence from the outer scope
selectorsSequence = fulfillAndHandleSelectors();
// Disconnect observer after finding all elements
const allSelectorsFulfilled = selectorsSequence.every((selector) => selector === null);
if (allSelectorsFulfilled) {
observer.disconnect();
}
};
/**
* Initializes a `MutationObserver` to watch for changes in the DOM.
* The observer is set up to monitor changes in attributes, child nodes, and subtree.
* A timeout is set to disconnect the observer if no elements are found within the specified time.
*/
const initializeMutationObserver = () => {
const observer = new MutationObserver(throttle(findElements, THROTTLE_DELAY_MS));
observer.observe(document.documentElement, {
attributes: true,
childList: true,
subtree: true,
});
// Set timeout to disconnect observer if elements are not found within the specified time
setTimeout(() => observer.disconnect(), OBSERVER_TIMEOUT_MS);
};
/**
* Checks if elements are already present in the DOM.
* If elements are found, they are clicked.
* If elements are not found, the observer is initialized.
*/
const checkInitialElements = () => {
const foundElements = selectorsSequence.every((selector) => {
if (!selector) {
return false;
}
const element = queryShadowSelector(selector, document.documentElement, textMatchRegexp);
return !!element;
});
if (foundElements) {
// Click previously collected elements
fulfillAndHandleSelectors();
} else {
// Initialize MutationObserver if elements were not found initially
initializeMutationObserver();
}
};
// Run the initial check
checkInitialElements();
// If there's a delay before clicking elements, use a timeout
if (parsedDelay) {
setTimeout(() => {
// Click previously collected elements
clickElementsBySequence();
canClick = true;
}, parsedDelay);
}
}
export const trustedClickElementNames = [
'trusted-click-element',
// trusted scriptlets support no aliases
];
// eslint-disable-next-line prefer-destructuring
trustedClickElement.primaryName = trustedClickElementNames[0];
trustedClickElement.injections = [
hit,
toRegExp,
parseCookieString,
throttle,
logMessage,
parseMatchArg,
queryShadowSelector,
doesElementContainText,
findElementWithText,
];