-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
130 lines (108 loc) · 3.19 KB
/
Copy pathapp.js
File metadata and controls
130 lines (108 loc) · 3.19 KB
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
/* eslint-disable @typescript-eslint/no-this-alias */
/* eslint-disable no-undef */
const HIGHLIGHTER_ID = 'selector-grab-highlighter';
let lastHighlightTarget;
function terminate() {
// The `click` listener is automatically removed after it has been called once
window.removeEventListener('mousemove', throttle(updateHighlight));
window.removeEventListener('keydown', checkTerminateKeys);
removeHighlight();
};
function addHighlight() {
const div = document.createElement('div');
div.id = HIGHLIGHTER_ID;
const { style } = div;
style.backgroundColor = '#1d234280';
style.boxSizing = 'border-box';
style.border = 'solid 4px #f0bb8980';
style.position = 'fixed';
style.zIndex = '9999';
style.pointerEvents = 'none';
document.body.appendChild(div);
};
function updateHighlight({ target }) {
if (!(target instanceof HTMLElement) || target === lastHighlightTarget) {
return;
}
lastHighlightTarget = target;
const { top, left, width, height } = target.getBoundingClientRect();
const highlighter = document.getElementById(HIGHLIGHTER_ID);
if (!highlighter) return;
const { style } = highlighter;
style.top = top - 4 + 'px';
style.left = left - 4 + 'px';
style.width = width + 8 + 'px';
style.height = height + 8 + 'px';
};
function removeHighlight() {
const highlighter = document.getElementById(HIGHLIGHTER_ID);
if (highlighter) {
document.body.removeChild(highlighter);
}
};
async function grabSelector(event) {
event.preventDefault();
const { target } = event;
if (!(target instanceof HTMLElement)) {
terminate();
return;
}
try {
await navigator?.clipboard?.writeText(generateSelector(target))
} catch (err) {
console.error('Could not write selector to clipboard\nError: ' + err)
}
terminate();
};
function generateSelector(element) {
if (!element) return '';
let selector = getSelector(element);
while(!isUnique(selector) && element) {
element = element.parentElement;
const newSelector = getSelector(element);
if(newSelector) selector = newSelector + '>' + selector;
}
return selector;
};
function getSelector(element) {
if (!element) return '';
const { tagName, id } = element;
const tag = tagName.toLowerCase();
if (tag === 'body' || tag === 'html') return tag;
let selector = tag;
if(!isUnique(selector)) selector += id ? '#' + id : '';
return appendPseudoSelector(element, selector);
};
function appendPseudoSelector(element, selector) {
return isUnique(selector) ? selector : `${selector}:nth-child(${getChildIndex(element)})`;
};
function getChildIndex({ previousElementSibling: sibling }) {
return sibling ? getChildIndex(sibling) + 1 : 1;
};
function getQueryLength(selector) {
return document.querySelectorAll(selector).length;
};
function isUnique(selector) {
return getQueryLength(selector) <= 1;
};
function checkTerminateKeys(event) {
const { key } = event;
if (key === 'Escape' || key === 'Esc') {
event.preventDefault();
terminate();
}
};
function throttle(func, limit = 100) {
let inThrottle;
let lastResult;
return function () {
const args = arguments;
const context = this
if (!inThrottle) {
inThrottle = true;
setTimeout(() => (inThrottle = false), limit);
lastResult = func.apply(context, args);
}
return lastResult;
};
};