forked from netconstructor/xPath-Viewer
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.js
264 lines (229 loc) · 6.54 KB
/
main.js
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
/*
* Copyright (C) 2011 Thomas Dvornik
* All rights reserved.
*
*/
var xpathBox = new XPathBox();
document.body.appendChild(xpathBox.element);
document.oncontextmenu = handleContextMenu;
document.onmousedown = handleMouseDown;
document.onmouseup = handleMouseUp;
document.onmousemove = handleMouseMove;
function handleContextMenu(e) {
// We don't want the context menu when xPath box is open
// except on the input so the user can copy and paste with
// right click.
if (xpathBox.visable && e.target != xpathBox.input) {
e.preventDefault();
e.stopPropagation();
// Only display the path if the context is not on the
// xPath box. The user shouldn't want the path of a component
// we added.
if (!xpathBox.isParent(e.target)) {
var path;
if (document.getElementById('cssselectorBox').checked) {
console.log('\nCSS for selected element:');
path = getElementCSSPath(e.target);
} else {
console.log('\nxPath for selected element:');
path = getElementXPath(e.target);
}
console.log(path);
xpathBox.clearOutput();
xpathBox.clearError();
xpathBox.appendOutput(path);
}
return false;
}
return true;
}
function handleMouseDown(e) {
if (xpathBox.visable && xpathBox.isXPathBoxSelected(e.target)) {
// Don't want to move on right click
if (e.which == 3) {
e.preventDefault();
e.stopPropagation();
return false;
} else {
xpathBox.setOffset(e.clientX, e.clientY);
xpathBox.movable = true;
e.preventDefault();
e.stopPropagation();
}
}
}
function handleMouseUp() {
xpathBox.movable = false;
}
function handleMouseMove(e) {
if (xpathBox.visable && xpathBox.movable) {
xpathBox.move(e.clientX, e.clientY);
e.preventDefault();
e.stopPropagation();
return false;
}
}
function cssSearch(cssstring) {
xpathBox.clearOutput();
xpathBox.clearError();
var nodes = document.querySelectorAll(cssstring);
for (var i=0; i<nodes.length; i++) {
console.log(nodes[i]);
xpathBox.appendOutput(elementToString(nodes[i]), nodes[i]);
if (i > 9) {
xpathBox.setError("Only 10 results displayed, but "+count+" were found. Please refine your search");
break;
}
}
if (nodes.length == 0) {
xpathBox.setError("No results found");
}
return nodes.length;
}
// Refactor everything below this mark
function xpathSearch(xpath) {
try {
xpathBox.clearOutput();
xpathBox.clearError();
var nodes = document.evaluate(xpath, document, null,
XPathResult.ANY_TYPE, null);
//xpathBox.appendOutput(nodes.resultType);
//return 1;
// Result types defined at https://developer.mozilla.org/en/introduction_to_using_xpath_in_javascript
// 1 is a number
if (nodes.resultType == '1') {
xpathBox.appendOutput(nodes.numberValue);
return 1;
}
// 3 is a boolean
if (nodes.resultType == '3') {
xpathBox.appendOutput(nodes.booleanValue);
return 1;
}
var node = nodes.iterateNext();
var count = 0, res = new Array();
try {
while (node) {
var parent = xpathBox.isParent(node);
if (!parent && count < 10) {
res.push(node);
}
if (!parent) {
count++;
}
node = nodes.iterateNext();
}
} catch (e) {
console.log(e);
xpathBox.setError(e);
}
for (var i = 0; i < res.length; i++) {
console.log(res[i]);
xpathBox.appendOutput(elementToString(res[i]), res[i]);
//TODO need to check for other object. For example, a query
// with '/text()' will display '[object Text]' in the output panel
}
if (count > 9) {
xpathBox.setError("Only 10 results displayed, but "+count+" were found. Please refine your search");
}
if (count == 0) {
xpathBox.setError("No results found");
}
return count;
}
catch (ex) {
xpathBox.clearOutput();
xpathBox.clearError();
/*var s = '';
for (var key in ex) {
s += key + ' ';
}
s = 'Name: '+ex.name +'; Message: '+ex.message+ '; Code: '+ex.code +'; Type: '+ex.TYPE_ERR;*/
if (ex.name == 'INVALID_EXPRESSION_ERR') {
var errMsg = 'Invalid xPath Expression.';
xpathBox.setError(errMsg);
console.log(errMsg);
} else {
xpathBox.setError(ex.message);
console.log(ex.message);
}
return 0;
}
}
function elementToString(element) {
if (element instanceof Text) {
var s = '';
for (var key in element) {
s += key + ' ';
}
return element.textContent;
}
if (!(element instanceof HTMLElement))
return element;
var string = '<' + element.tagName.toLowerCase();
var i = 0, attNum = 0;
var atts = element.attributes;
for (; i < atts.length; i++) {
var nname = atts[i].nodeName;
string += ' ' + atts[i].nodeName + '="' + atts[i].nodeValue + '"';
}
string += '>';
return string;
}
/*
* I want to display the html of the element, but right now the formatting gets all messed up.
* Going to put this function on hold until I get the extension done, then revisit this.
*/
function elementDisplayString(element) {
if (!(element instanceof HTMLElement))
return element;
var string = '<' + element.tagName.toLowerCase();
var attNum = 0;
var atts = element.attributes;
var i;
for (i = 0; i < atts.length; i++) {
var nname = atts[i].nodeName;
if (nname != "id" && nname != "style" && nname != "class") {
string += ' ' + atts[i].nodeName + '="' + atts[i].nodeValue + '"';
}
}
string += '>' + elementToString(element.innerHTML) + '</' + element.tagName + '>';
//alert(string);
return string;
}
function getElementXPath(elt) {
var path = "";
for (; elt && elt.nodeType == 1; elt = elt.parentNode) {
idx = getElementIdx(elt);
xname = elt.tagName.toLowerCase();
if (idx > 1)
xname += "[" + idx + "]";
path = "/" + xname + path;
}
return path;
}
function getElementCSSPath(elem) {
var tagName = elem.tagName.toLowerCase();
// id is unique, so we can stop here
if (elem.id) {
return "#" + elem.id;
}
// reached the top level element, so we can return
if (elem.tagName == "BODY") {
return '';
}
var path = getElementCSSPath(elem.parentNode);
if (elem.className) {
// concatenate all the classes together
return path + " " + tagName + "." + elem.className.split(' ').join('.');
}
return path + " " + tagName;
}
function getElementIdx(elt) {
var count = 1;
for ( var sib = elt.previousSibling; sib; sib = sib.previousSibling) {
if (sib.nodeType == 1 && sib.tagName == elt.tagName)
count++;
}
return count;
}