-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdom-utils.js
More file actions
254 lines (218 loc) · 6.21 KB
/
Copy pathdom-utils.js
File metadata and controls
254 lines (218 loc) · 6.21 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
// DOM Utilities - Helper functions for DOM manipulation
/**
* Serialize DOM to a simplified structure for AI analysis
* Removes script tags, styles, and other noise
*/
function serializeDOM() {
const clone = document.body.cloneNode(true);
// Remove noise elements
const selectorsToRemove = [
'script',
'style',
'iframe',
'noscript',
'[aria-hidden="true"]',
'.ad',
'.advertisement',
'[class*="cookie"]',
'[class*="banner"]'
];
selectorsToRemove.forEach(selector => {
clone.querySelectorAll(selector).forEach(el => el.remove());
});
// Extract meaningful structure
const structure = extractStructure(clone);
return structure;
}
/**
* Extract meaningful structure from DOM
*/
function extractStructure(element) {
const items = [];
const important = ['button', 'a', 'input', 'select', 'textarea', 'h1', 'h2', 'h3', 'form'];
function traverse(el, depth = 0) {
if (depth > 10) return; // Limit depth
const tag = el.tagName?.toLowerCase();
if (!tag) return;
// Extract important elements
if (important.includes(tag)) {
const text = el.textContent?.trim().slice(0, 200) || '';
const id = el.id || '';
const classes = Array.from(el.classList).join(' ');
const type = el.type || '';
const href = el.href || '';
if (text || id || type) {
items.push({
tag,
text,
id,
classes,
type,
href,
visible: isVisible(el),
xpath: getXPath(el)
});
}
}
// Recurse
Array.from(el.children).forEach(child => traverse(child, depth + 1));
}
traverse(element);
return items;
}
/**
* Check if element is visible
*/
function isVisible(el) {
if (!el || !(el instanceof Element)) return false;
const style = window.getComputedStyle(el);
return style.display !== 'none' &&
style.visibility !== 'hidden' &&
style.opacity !== '0' &&
el.offsetWidth > 0 &&
el.offsetHeight > 0;
}
/**
* Get XPath for an element
*/
function getXPath(element) {
if (!element) {
return '';
}
if (element.id) {
return `//*[@id="${element.id}"]`;
}
if (element === document.body) {
return '/html/body';
}
// Check for parentNode before recursion
if (!element.parentNode) {
return element.tagName ? '/' + element.tagName.toLowerCase() : '';
}
let ix = 0;
const siblings = element.parentNode.childNodes || [];
for (let i = 0; i < siblings.length; i++) {
const sibling = siblings[i];
if (sibling === element) {
return getXPath(element.parentNode) + '/' + element.tagName.toLowerCase() + '[' + (ix + 1) + ']';
}
if (sibling.nodeType === 1 && sibling.tagName === element.tagName) {
ix++;
}
}
return '';
}
/**
* Get element by XPath
*/
function getElementByXPath(xpath) {
return document.evaluate(
xpath,
document,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null
).singleNodeValue;
}
/**
* Find all text nodes containing specific text
*/
function findTextNodes(searchText, rootElement = document.body) {
const matches = [];
const walker = document.createTreeWalker(
rootElement,
NodeFilter.SHOW_TEXT,
{
acceptNode: (node) => {
if (node.textContent?.toLowerCase().includes(searchText.toLowerCase())) {
return NodeFilter.FILTER_ACCEPT;
}
return NodeFilter.FILTER_SKIP;
}
}
);
let node;
while (node = walker.nextNode()) {
matches.push(node);
}
return matches;
}
/**
* Detect animated elements
*/
function detectAnimations() {
const animated = [];
const allElements = document.querySelectorAll('*');
allElements.forEach(el => {
const style = window.getComputedStyle(el);
const hasAnimation = style.animationName !== 'none' && style.animationName !== '';
const hasTransition = style.transition !== 'all 0s ease 0s' && style.transition !== '';
// Check for problematic animations (fast, flashing)
if (hasAnimation) {
const duration = parseFloat(style.animationDuration);
const iterationCount = style.animationIterationCount;
if (duration < 2 || iterationCount === 'infinite') {
animated.push({
element: el,
type: 'animation',
animation: style.animationName,
duration
});
}
}
// Check for auto-playing videos/gifs
if (el.tagName === 'VIDEO' || el.tagName === 'IFRAME') {
animated.push({
element: el,
type: 'media'
});
}
});
return animated;
}
/**
* Inject CSS safely
*/
function injectCSS(css, id = 'inclusiveread-styles') {
let styleEl = document.getElementById(id);
if (!styleEl) {
styleEl = document.createElement('style');
styleEl.id = id;
document.head.appendChild(styleEl);
}
styleEl.textContent = css;
}
/**
* Remove injected CSS
*/
function removeCSS(id = 'inclusiveread-styles') {
const styleEl = document.getElementById(id);
if (styleEl) {
styleEl.remove();
}
}
/**
* Wrap text node with span for replacement
*/
function wrapTextNode(textNode, className = 'ir-jargon') {
const span = document.createElement('span');
span.className = className;
span.textContent = textNode.textContent;
textNode.parentNode.replaceChild(span, textNode);
return span;
}
// Export for use in content script
if (typeof module !== 'undefined' && module.exports) {
module.exports = {
serializeDOM,
extractStructure,
isVisible,
getXPath,
getElementByXPath,
findTextNodes,
detectAnimations,
injectCSS,
removeCSS,
wrapTextNode
};
}