-
Notifications
You must be signed in to change notification settings - Fork 0
/
ElementHelper.js
161 lines (143 loc) · 4.84 KB
/
ElementHelper.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
// Author Knighttower
// MIT License
// Copyright (c) [2022] [Knighttower] https://github.com/knighttower
import domObserver from './domObserver.js';
/**
* @class Adds some extra functionality to interact with a DOM element
* @param {String|Object} selector Class or ID or DOM element
* @param {String} scope The scope to search in, window, document, dom element. Defaults to document
* @return {Object}
* @example new ElementHelper('elementSelector')
* @example new ElementHelper('elementSelector', domElement|window|document)
*
*/
class ElementHelper {
/**
* Constructor
* @param {String|Object} selector
* @return {Object}
*/
constructor(selector, scope = document) {
domObserver.start();
this.selector = selector;
if (typeof selector === 'object') {
this.domElement = selector;
} else if (String(selector).includes('//')) {
this.domElement = this.getElementByXpath(selector);
} else {
this.domElement = scope.querySelector(selector);
}
}
// =========================================
// --> Public
// --------------------------
/**
* Check if the element exists or is visible. It will keep querying
* @return {Boolean}
*/
isInDom() {
return Boolean(this.domElement?.outerHTML);
}
/**
* Wait for element exists or is visible. It will keep querying
* @function whenInDom
* @return {Promise}
*/
whenInDom() {
let $this = this;
let callbackId = Date.now() + Math.floor(Math.random() * 1000);
return new Promise(function (resolveThis) {
if (!$this.isInDom()) {
domObserver.addOnNodeChange(callbackId, () => {
let element = new ElementHelper($this.selector);
if (element.isInDom()) {
$this = element;
resolveThis($this);
domObserver.removeOnNodeChange(callbackId);
}
});
} else {
resolveThis($this);
}
});
}
/**
* Find element by Xpath string
* @param {String} xpath
* @example getElementByXpath("//html[1]/body[1]/div[1]")
* @return {Object} DOM element
*/
getElementByXpath(xpath) {
return document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null)
.singleNodeValue;
}
/**
* Get the element xpath string
* @author Based on https://stackoverflow.com/questions/2631820/how-do-i-ensure-saved-click-coordinates-can-be-reload-to-the-same-place-even-if/2631931#2631931
* @return {String}
*/
getXpathTo() {
let element = this.domElement;
if (element.id) {
return `//*[@id='${element.id}']`;
}
if (element === document.body) {
return '//' + element.tagName;
}
let ix = 0;
let siblings = element.parentNode.childNodes;
for (let i = 0; i < siblings.length; i++) {
let sibling = siblings[i];
if (sibling === element) {
return (
new ElementHelper(element.parentNode).getXpathTo() +
'/' +
element.tagName +
'[' +
(ix + 1) +
']'
);
}
if (sibling.nodeType === 1 && sibling.tagName === element.tagName) {
ix++;
}
}
}
/**
* Get the element attribute, but parse it if it is an object or array
* @param {String} attr Atrribute name
* @return {String|Array|Object|Null}
*/
getAttribute(attr) {
return this.domElement.getAttribute(attr) || null;
}
/**
* Create a unique has for the element derived from its xpath
* @author Based on https://www.geeksforgeeks.org/how-to-create-hash-from-string-in-javascript/
* @return {String}
*/
getHash() {
let string = String(this.getXpathTo());
let hash = 0;
if (string.length === 0) {
return hash;
}
for (let i = 0; i < string.length; i++) {
let char = string.charCodeAt(i);
hash = (hash << 5) - hash + char;
hash = hash & hash;
}
return hash;
}
}
// =========================================
// --> Utilities
// --------------------------
const selectElement = (selector, scope = document) => new ElementHelper(selector, scope);
const elementHelper = (selector, scope = document) => new ElementHelper(selector, scope);
/**
* Future
* @private
* @todo enhance to extend the prototype like https://stackoverflow.com/questions/779880/in-javascript-can-you-extend-the-dom
*/
export { ElementHelper as default, ElementHelper, selectElement, elementHelper };