-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathretrieveCssOrXpathSelectorFromTextOrNode.js
executable file
·120 lines (107 loc) · 4.13 KB
/
retrieveCssOrXpathSelectorFromTextOrNode.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
// ==UserScript==
// @name retrieveCssOrXpathSelectorFromTextOrNode
// @namespace gilles<dot>quenot<at>sputnick<dot>fr
// @version 0.5
// @description retrieve CSS or Xpath Selector from text or node for chrome dev tools/Firefox dev tools
// @author Gilles Quenot
// @include https://*
// @include http://*
// @include file://*
// @exclude https://mail.google.com/*
// @grant none
// ==/UserScript==
var xpathNamespaceResolver = {
svg: 'http://www.w3.org/2000/svg',
mathml: 'http://www.w3.org/1998/Math/MathML'
};
var getElementByXPath = function getElementByXPath(expression) {
var a = document.evaluate(expression, document.body, xpathNamespaceResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
if (a.snapshotLength > 0) {
return a.snapshotItem(0);
}
};
window.retrieveCssOrXpathSelectorFromTextOrNode = function(arg, type) {
var root = [], node;
var nodeType = type.toLowerCase();
function retrieveNodeNameAndAttributes(node) {
var output = '';
try {
var nodeName = node.nodeName.toLowerCase();
} catch(e) {
console.error('ERROR no matching node');
return;
}
if (node.hasAttributes()) {
var attrs = node.attributes;
for (var i = 0; i < attrs.length; i++) {
if (nodeType === 'xpath') {
if (attrs[i].value) {
output += '[@' + attrs[i].name + '="' + attrs[i].value + '"]';
}
else {
output += '[@' + attrs[i].name + ']';
}
}
else if (nodeType === 'css') {
if (attrs[i].value) {
if (attrs[i].name === 'id') {
if (/:/.test(attrs[i].value)) {
output += '[id="' + attrs[i].value + '"]'; // new Ex: [id="foo:bar"]
}
else {
output += "#" + attrs[i].value;
}
} else if (attrs[i].name === 'class') {
var classes = attrs[i].value.split(/\s+\b/).join('.');
output += '.' + classes;
} else {
output += "[" + attrs[i].name + '="' + attrs[i].value + '"]';
}
}
else {
output += "[" + attrs[i].name + "]";
}
}
}
}
var txt = '';
if (nodeName === 'a' && nodeType === 'xpath') {
txt = '[text()="' + node.innerText + '"]';
}
root.push({ 'name': nodeName, 'attrs': output, txt });
if (nodeName === 'body' || nodeName === 'html') return;
else retrieveNodeNameAndAttributes(node.parentNode); // recursive function
}
if (typeof arg === 'string') { // text from within the page
var selector = '//*[text()[contains(.,"' + arg + '")]]';
node = getElementByXPath(selector);
} else if (typeof arg === 'object') { // node argument, let's do some 'duck typing'
if (arg && arg.nodeType) {
node = arg;
}
else {
console.error("ERROR expected node, get object");
return;
}
} else {
console.error("ERROR expected node or string argumument");
return;
}
retrieveNodeNameAndAttributes(node);
var output = '';
if (nodeType === 'css') {
output = root.reverse().map(elt => elt.name + elt.attrs ).join(' > ');
}
else if (nodeType === 'xpath') {
output = '//' + root.reverse().map(elt => elt.name + elt.txt + elt.attrs ).join('/');
}
else {
console.error('ERROR unknown type ' + type);
}
return output;
//console.log(output);
};
window.x = function(arg) {
console.log("CSS\n" + window.retrieveCssOrXpathSelectorFromTextOrNode(arg, 'css'));
console.log("XPath\n" + window.retrieveCssOrXpathSelectorFromTextOrNode(arg, 'xpath'));
};