-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
160 lines (146 loc) · 5 KB
/
Copy pathcontent.js
File metadata and controls
160 lines (146 loc) · 5 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
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
(function () {
// De-duplication guard: if picker is already active, cancel it
if (window.__domPickerActive) {
window.__domPickerActive = false;
const existing = document.getElementById('__domPickerOverlay');
if (existing) existing.remove();
document.body.style.cursor = '';
document.removeEventListener('mousemove', onMousemove);
document.removeEventListener('click', onClick, true);
document.removeEventListener('keydown', onKeydown, true);
chrome.runtime.sendMessage({ type: 'picker-done' });
return;
}
window.__domPickerActive = true;
// --- Overlay ---
const overlay = document.createElement('div');
overlay.id = '__domPickerOverlay';
Object.assign(overlay.style, {
position: 'fixed',
pointerEvents: 'none',
zIndex: '2147483647',
border: '2px solid #4A90E2',
backgroundColor: 'rgba(74, 144, 226, 0.15)',
borderRadius: '2px',
boxSizing: 'border-box',
display: 'none',
});
document.body.appendChild(overlay);
// --- Cursor ---
const prevCursor = document.body.style.cursor;
document.body.style.cursor = 'crosshair';
// --- Element path builder ---
function getElementPath(el) {
const parts = [], els = [];
let current = el;
for (let i = 0; i < 4; i++) {
if (!current || current === document.body || current === document.documentElement) break;
const tag = current.tagName.toLowerCase();
const rawClass = current.classList[0] || '';
const cssModuleClass = rawClass.match(/^_(.+)_[a-z0-9]{5,}_\d+$/i);
const cleanClass = rawClass ? '.' + (cssModuleClass ? cssModuleClass[1] : rawClass) : '';
parts.unshift(tag + cleanClass);
els.unshift(current);
current = current.parentElement;
}
return { parts, els };
}
// --- React component probe (batched) ---
function getReactComponents(els) {
const ids = els.map((_, i) => '__react_probe_' + Date.now() + '_' + i);
els.forEach((el, i) => el.setAttribute('data-probe', ids[i]));
const cleanup = () => els.forEach(el => el.removeAttribute('data-probe'));
return chrome.runtime.sendMessage({ type: 'get-react-components', probeIds: ids })
.then(names => { cleanup(); return names || ids.map(() => null); })
.catch(() => { cleanup(); return ids.map(() => null); });
}
// --- Overlay positioning ---
function positionOverlay(el) {
if (!el || el === document.body || el === document.documentElement) {
overlay.style.display = 'none';
return;
}
const rect = el.getBoundingClientRect();
Object.assign(overlay.style, {
display: 'block',
top: rect.top + 'px',
left: rect.left + 'px',
width: rect.width + 'px',
height: rect.height + 'px',
});
}
// --- Toast ---
function showToast(message) {
const toast = document.createElement('div');
Object.assign(toast.style, {
position: 'fixed',
bottom: '24px',
right: '24px',
background: '#323232',
color: '#fff',
padding: '10px 16px',
borderRadius: '6px',
fontSize: '13px',
fontFamily: 'system-ui, -apple-system, sans-serif',
zIndex: '2147483647',
boxShadow: '0 2px 8px rgba(0,0,0,0.3)',
opacity: '0',
transition: 'opacity 0.2s ease',
maxWidth: '360px',
wordBreak: 'break-all',
lineHeight: '1.4',
});
toast.textContent = message;
document.body.appendChild(toast);
// Double rAF to trigger transition
requestAnimationFrame(() => {
requestAnimationFrame(() => { toast.style.opacity = '1'; });
});
setTimeout(() => {
toast.style.opacity = '0';
setTimeout(() => toast.remove(), 250);
}, 2500);
}
// --- Cleanup ---
function cleanup() {
document.removeEventListener('mousemove', onMousemove);
document.removeEventListener('click', onClick, true);
document.removeEventListener('keydown', onKeydown, true);
overlay.remove();
document.body.style.cursor = prevCursor;
window.__domPickerActive = false;
chrome.runtime.sendMessage({ type: 'picker-done' });
}
// --- Event handlers ---
function onMousemove(e) {
positionOverlay(e.target);
}
function buildText(parts, components) {
let prev;
return parts.map((part, i) => {
const comp = components[i];
const prefix = comp && comp !== prev ? `[${comp}] ` : '';
prev = comp;
return prefix + part;
}).join(' > ');
}
async function onClick(e) {
e.preventDefault();
e.stopPropagation();
const { parts, els } = getElementPath(e.target);
const components = await getReactComponents(els);
const text = buildText(parts, components);
cleanup();
navigator.clipboard.writeText('`' + text + '`')
.then(() => showToast('Copied: ' + text))
.catch(() => showToast('Failed to copy to clipboard'));
}
function onKeydown(e) {
if (e.key === 'Escape') {
cleanup();
}
}
document.addEventListener('mousemove', onMousemove);
document.addEventListener('click', onClick, true);
document.addEventListener('keydown', onKeydown, true);
})();