-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_h1_headings.js
51 lines (43 loc) · 1.72 KB
/
get_h1_headings.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
/**
* @author Christan Hänsel
*
* gets the h1 heading(s) and XPath and displays them in the extended table
*/
function createXPathFromElement(elm) {
var allNodes = document.getElementsByTagName('*');
for (var segs = []; elm && elm.nodeType == 1; elm = elm.parentNode) {
if (elm.hasAttribute('id')) {
var uniqueIdCount = 0;
for (var n = 0; n < allNodes.length; n++) {
if (allNodes[n].hasAttribute('id') && allNodes[n].id == elm.id) uniqueIdCount++;
if (uniqueIdCount > 1) break;
};
if (uniqueIdCount == 1) {
segs.unshift('id("' + elm.getAttribute('id') + '")');
return segs.join('/');
} else {
segs.unshift(elm.localName.toLowerCase() + '[@id="' + elm.getAttribute('id') + '"]');
}
} else if (elm.hasAttribute('class')) {
segs.unshift(elm.localName.toLowerCase() + '[@class="' + elm.getAttribute('class') + '"]');
} else {
for (i = 1, sib = elm.previousSibling; sib; sib = sib.previousSibling) {
if (sib.localName == elm.localName) i++;
};
segs.unshift(elm.localName.toLowerCase() + '[' + i + ']');
};
};
return segs.length ? '/' + segs.join('/') : null;
};
/* Retrieve some information from the page */
const data = document.querySelectorAll('h1')
/* Map it into an array */
const extended = [...data].map(e => [createXPathFromElement(e), e.innerText])
/* Add the header row */
extended.unshift(['XPath', 'Content'])
const ret = {
cell: data.length,
/* The data to be displayed in the cell */
extended: extended /* The explore table when the cell is clicked */
}
return ret