-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
197 lines (170 loc) · 5.02 KB
/
content.js
File metadata and controls
197 lines (170 loc) · 5.02 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
let clickedElement = null;
document.addEventListener('contextmenu', (e) => {
clickedElement = e.target;
}, true);
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "ping") {
sendResponse({ pong: true });
return true;
} else if (request.action === "getXPathFromClick") {
if (clickedElement) {
const xpath = getAbsoluteXPath(clickedElement);
const texts = extractByXPath(xpath, 'text');
chrome.runtime.sendMessage({
action: "openPopupWithData",
data: {
xpath: xpath,
data: texts,
url: window.location.href
}
});
}
} else if (request.action === "evaluateXPath") {
const data = extractByXPath(request.xpath, request.mode || 'text');
sendResponse({ data: data });
return true;
} else if (request.action === "highlightElements") {
highlightElements(request.xpath);
}
});
function getAbsoluteXPath(element) {
if (element.id !== '') {
return `//*[@id="${element.id}"]`;
}
if (element === document.body) {
return '/html/body';
}
let path = '';
let current = element;
while (current && current.nodeType === Node.ELEMENT_NODE) {
let index = 1;
let sibling = current.previousSibling;
while (sibling) {
if (sibling.nodeType === Node.ELEMENT_NODE &&
sibling.nodeName === current.nodeName) {
index++;
}
sibling = sibling.previousSibling;
}
const tagName = current.nodeName.toLowerCase();
const pathIndex = `[${index}]`;
path = `/${tagName}${pathIndex}${path}`;
current = current.parentNode;
if (current === document.documentElement) {
path = '/html' + path;
break;
}
}
return path;
}
function extractByXPath(xpath, mode = 'text') {
const data = [];
if (xpath.includes('[*]')) {
return extractWithWildcard(xpath, mode);
}
try {
const result = document.evaluate(
xpath,
document,
null,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
null
);
for (let i = 0; i < result.snapshotLength; i++) {
const node = result.snapshotItem(i);
if (mode === 'text') {
const text = node.innerText || node.textContent || '';
if (text.trim()) {
data.push(text.trim());
}
} else if (mode === 'attributes') {
const attrs = {};
for (let i = 0; i < node.attributes.length; i++) {
const attr = node.attributes[i];
attrs[attr.name] = attr.value;
}
data.push(JSON.stringify(attrs));
}
}
if (data.length === 0) {
data.push('No elements matching this XPath were found.');
}
} catch (e) {
data.push(`❌ XPath Error: ${e.message}`);
}
return data;
}
function extractWithWildcard(xpathTemplate, mode = 'text') {
const allData = [];
const maxAttempts = 50;
for (let i = 1; i <= maxAttempts; i++) {
const xpath = xpathTemplate.replace(/\[\*\]/g, `[${i}]`);
try {
const result = document.evaluate(
xpath,
document,
null,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
null
);
if (result.snapshotLength > 0) {
for (let j = 0; j < result.snapshotLength; j++) {
const node = result.snapshotItem(j);
if (mode === 'text') {
const text = node.innerText || node.textContent || '';
if (text.trim()) {
allData.push(text.trim());
}
} else if (mode === 'attributes') {
const attrs = {};
for (let i = 0; i < node.attributes.length; i++) {
const attr = node.attributes[i];
attrs[attr.name] = attr.value;
}
allData.push(JSON.stringify(attrs));
}
}
} else {
if (i > 3 && allData.length > 0) {
let emptyCount = 0;
for (let k = i - 3; k < i; k++) {
const testXpath = xpathTemplate.replace(/\[\*\]/g, `[${k}]`);
const testResult = document.evaluate(testXpath, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
if (testResult.snapshotLength === 0) emptyCount++;
}
if (emptyCount === 3) break;
}
}
} catch (e) {
continue;
}
}
if (allData.length === 0) {
allData.push('No elements matching this XPath were found.');
}
return allData;
}
function highlightElements(xpath) {
document.querySelectorAll('.xpath-highlight').forEach(el => {
el.classList.remove('xpath-highlight');
el.style.outline = '';
});
try {
const result = document.evaluate(
xpath,
document,
null,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
null
);
for (let i = 0; i < result.snapshotLength; i++) {
const node = result.snapshotItem(i);
if (node.nodeType === Node.ELEMENT_NODE) {
node.style.outline = '2px solid red';
node.classList.add('xpath-highlight');
}
}
} catch (e) {
console.error('Highlight error:', e);
}
}