-
Notifications
You must be signed in to change notification settings - Fork 21
/
dom-actions.ts
156 lines (140 loc) · 5.6 KB
/
dom-actions.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
import { ElementSelector, HideMethod, VisibilityCheck } from './rules';
import { DomActionsProvider } from './types';
import { getStyleElement, hideElements, isElementVisible, waitFor } from './utils';
import AutoConsent from './web';
export class DomActions implements DomActionsProvider {
// eslint-disable-next-line no-useless-constructor
constructor(public autoconsentInstance: AutoConsent) {}
click(selector: ElementSelector, all = false): boolean {
const elem = this.elementSelector(selector);
this.autoconsentInstance.config.logs.rulesteps && console.log('[click]', selector, all, elem);
if (elem.length > 0) {
if (all) {
elem.forEach((e) => e.click());
} else {
elem[0].click();
}
}
return elem.length > 0;
}
elementExists(selector: ElementSelector): boolean {
const exists = this.elementSelector(selector).length > 0;
return exists;
}
elementVisible(selector: ElementSelector, check: VisibilityCheck): boolean {
const elem = this.elementSelector(selector);
const results = new Array(elem.length);
elem.forEach((e, i) => {
// check for display: none
results[i] = isElementVisible(e);
});
if (check === 'none') {
return results.every((r) => !r);
} else if (results.length === 0) {
return false;
} else if (check === 'any') {
return results.some((r) => r);
}
// all
return results.every((r) => r);
}
waitForElement(selector: ElementSelector, timeout = 10000): Promise<boolean> {
const interval = 200;
const times = Math.ceil(timeout / interval);
this.autoconsentInstance.config.logs.rulesteps && console.log('[waitForElement]', selector);
return waitFor(() => this.elementSelector(selector).length > 0, times, interval);
}
waitForVisible(selector: ElementSelector, timeout = 10000, check: VisibilityCheck = 'any'): Promise<boolean> {
const interval = 200;
const times = Math.ceil(timeout / interval);
this.autoconsentInstance.config.logs.rulesteps && console.log('[waitForVisible]', selector);
return waitFor(() => this.elementVisible(selector, check), times, interval);
}
async waitForThenClick(selector: ElementSelector, timeout = 10000, all = false): Promise<boolean> {
await this.waitForElement(selector, timeout);
return this.click(selector, all);
}
wait(ms: number): Promise<true> {
this.autoconsentInstance.config.logs.rulesteps && console.log('[wait]', ms);
return new Promise((resolve) => {
setTimeout(() => {
resolve(true);
}, ms);
});
}
hide(selector: string, method: HideMethod): boolean {
this.autoconsentInstance.config.logs.rulesteps && console.log('[hide]', selector);
const styleEl = getStyleElement();
return hideElements(styleEl, selector, method);
}
prehide(selector: string): boolean {
const styleEl = getStyleElement('autoconsent-prehide');
this.autoconsentInstance.config.logs.lifecycle && console.log('[prehide]', styleEl, location.href);
return hideElements(styleEl, selector, 'opacity');
}
undoPrehide(): boolean {
const existingElement = getStyleElement('autoconsent-prehide');
this.autoconsentInstance.config.logs.lifecycle && console.log('[undoprehide]', existingElement, location.href);
if (existingElement) {
existingElement.remove();
}
return !!existingElement;
}
async createOrUpdateStyleSheet(cssText: string, styleSheet?: CSSStyleSheet) {
if (!styleSheet) {
styleSheet = new CSSStyleSheet();
}
styleSheet = await styleSheet.replace(cssText);
return styleSheet;
}
removeStyleSheet(styleSheet?: CSSStyleSheet): boolean {
if (styleSheet) {
styleSheet.replace('');
return true;
}
return false;
}
querySingleReplySelector(selector: string, parent: any = document): HTMLElement[] {
if (selector.startsWith('aria/')) {
return [];
}
if (selector.startsWith('xpath/')) {
const xpath = selector.slice(6);
const result = document.evaluate(xpath, parent, null, XPathResult.ANY_TYPE, null);
let node: Node = null;
const elements: HTMLElement[] = [];
while ((node = result.iterateNext())) {
elements.push(node as HTMLElement);
}
return elements;
}
if (selector.startsWith('text/')) {
return [];
}
if (selector.startsWith('pierce/')) {
return [];
}
if (parent.shadowRoot) {
return Array.from(parent.shadowRoot.querySelectorAll(selector));
}
return Array.from(parent.querySelectorAll(selector));
}
querySelectorChain(selectors: string[]): HTMLElement[] {
let parent: ParentNode = document;
let matches: HTMLElement[];
for (const selector of selectors) {
matches = this.querySingleReplySelector(selector, parent);
if (matches.length === 0) {
return [];
}
parent = matches[0];
}
return matches;
}
elementSelector(selector: ElementSelector): HTMLElement[] {
if (typeof selector === 'string') {
return this.querySingleReplySelector(selector);
}
return this.querySelectorChain(selector);
}
}