-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathjs_extractor.jinja2
47 lines (38 loc) · 1.27 KB
/
js_extractor.jinja2
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
() => {
var data = {};
//isHidden helper function
// See https://stackoverflow.com/questions/19669786/check-if-element-is-visible-in-dom
function isHidden(el) {
//if(el == null) return true;
//return (el.offsetParent === null)
//Slower
var style = window.getComputedStyle(el);
return (style.display === 'none')
}
//XPath wrapper function
function getElementByXPath(path) {
return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
//XPath wrapper function returing an iterator
function getElementsByXPath(path) {
return document.evaluate(path, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
}
{%for key, value in settings.items() %}
//{{key}}
{% if key == "coverage" %}
xpaths = getElementsByXPath('{{value}}');
for (let i = 0, length = xpaths.snapshotLength; i < length; ++i) {
if(isHidden(xpaths.snapshotItem(i)) == false){
data["coverage"] = xpaths.snapshotItem(i);
}
}
{% else %}
data["{{key}}"] = getElementByXPath('{{value}}');
{%endif %}
{% endfor %}
//loop twice to avoid bug with url_index and url_not_index
{%for key, value in settings.items() %}
data["{{key}}"] = data["{{key}}"] && data["{{key}}"].textContent;
{%endfor %}
return data;
}