forked from ahmetsaridogan/pick-dom-element
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
48 lines (43 loc) · 1.18 KB
/
example.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
import { ElementPicker } from "pick-dom-element";
function main() {
const status = document.getElementById("status");
const startButton = document.getElementById("start");
const onlyEmphasisCheckbox = document.getElementById("only-emphasis");
const setElement = (el) => {
const tags = [];
while (el.parentNode) {
tags.push(el.tagName);
el = el.parentNode;
}
status.innerText = tags
.reverse()
.map((t) => t.toLowerCase())
.join(" > ");
};
const picker = new ElementPicker({
background: "rgba(153, 235, 255, 0.5)",
borderColor: "yellow"
});
let onlyEmphasis = onlyEmphasisCheckbox.checked;
onlyEmphasisCheckbox.onchange = (ev) => {
onlyEmphasis = ev.target.checked;
}
const start = () => {
startButton.disabled = true;
picker.start({
onHover: setElement,
onClick: () => {
picker.stop();
startButton.disabled = false;
},
elementFilter: (el) => {
if (!onlyEmphasis) {
return true;
}
return ['I', 'B'].includes(el.tagName);
}
});
};
startButton.addEventListener("click", start);
}
document.addEventListener("DOMContentLoaded", main);