-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
1027 lines (906 loc) · 39.2 KB
/
Copy pathscript.js
File metadata and controls
1027 lines (906 loc) · 39.2 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ==UserScript==
// @name Element Selector Tool
// @namespace http://tampermonkey.net/
// @version 6.2
// @description Press Ctrl+E to get friendly CSS selectors for any element
// @author jamubc
// @match *://*/*
// @run-at document-end
// @inject-into auto
// @grant GM_registerMenuCommand
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_addElement
// ==/UserScript==
(function() {
'use strict';
let active = false, overlay, tooltip, current;
let initialized = false;
let controller = null; // AbortController for active listeners
let ro = null; // ResizeObserver
let rafToken = null; // rAF throttle for pointermove
// Settings
const SETTINGS_KEY = 'est_settings_v1';
const defaultSettings = {
richCopy: true, // text + JSON + HTML clipboard when available
deepShadow: true, // traverse open ShadowRoots for uniqueness checks
spaAware: true, // patch history to observe SPA navigation
passiveListeners: true // use passive where possible
};
function getSettings() {
try {
const raw = GM_getValue(SETTINGS_KEY, null);
if (!raw) return { ...defaultSettings };
const parsed = JSON.parse(raw);
return { ...defaultSettings, ...parsed };
} catch {
return { ...defaultSettings };
}
}
function saveSettings(s) {
try { GM_setValue(SETTINGS_KEY, JSON.stringify(s)); } catch {}
}
// Small CSS.escape fallback for older engines
try {
if (!(window.CSS && typeof CSS.escape === 'function')) {
const cssEscapeFallback = (s) => String(s).replace(/([!"#$%&'()*+,.\/:;<=>?@\[\]^`{|}~\s])/g, '\\$1');
window.CSS = window.CSS || {};
window.CSS.escape = cssEscapeFallback;
}
} catch {}
// CSP-safe element creation
function createElementSafe(tag, styles) {
let el;
try {
// Try GM_addElement if available (CSP-friendly)
if (typeof GM_addElement !== 'undefined') {
el = GM_addElement(tag, {});
} else {
el = document.createElement(tag);
}
} catch (e) {
el = document.createElement(tag);
}
if (styles) {
el.style.cssText = styles;
}
return el;
}
// Traverse Shadow DOM to find actual target element
function getDeepestElement(x, y) {
let element = document.elementFromPoint(x, y);
if (!element) return null;
// Traverse into shadow roots
while (element && element.shadowRoot) {
const shadowElement = element.shadowRoot.elementFromPoint(x, y);
if (!shadowElement || shadowElement === element) break;
element = shadowElement;
}
return element;
}
// Build shadow path description (outer → inner) and whether any closed roots encountered
function describeShadowPath(el) {
const parts = [];
let node = el;
let crossedClosed = false;
while (node) {
const root = node.getRootNode();
if (root && root.host) {
const host = root.host;
const mode = root.mode || 'closed';
if (mode === 'closed') crossedClosed = true;
parts.unshift(`${host.tagName.toLowerCase()}#${host.id || ''}`.replace(/#$/, ''));
node = host;
} else {
break;
}
}
return { path: parts, crossedClosed };
}
// Hotkey configuration with defaults
const DEFAULT_HOTKEY = {
ctrlKey: true,
shiftKey: false,
altKey: false,
metaKey: false,
key: 'e'
};
// Get stored hotkey or return default
function getHotkey() {
try {
const stored = GM_getValue('hotkey', null);
return stored ? JSON.parse(stored) : DEFAULT_HOTKEY;
} catch (e) {
return DEFAULT_HOTKEY;
}
}
// Save hotkey configuration
function setHotkey(config) {
try {
GM_setValue('hotkey', JSON.stringify(config));
} catch (e) {
console.error('Failed to save hotkey:', e);
}
}
// Format hotkey for display
function formatHotkey(config) {
const parts = [];
if (config.ctrlKey) parts.push('Ctrl');
if (config.shiftKey) parts.push('Shift');
if (config.altKey) parts.push('Alt');
if (config.metaKey) parts.push('Cmd');
parts.push(config.key.toUpperCase());
return parts.join('+');
}
// Check if event matches hotkey config
function matchesHotkey(event, config) {
return event.ctrlKey === config.ctrlKey &&
event.shiftKey === config.shiftKey &&
event.altKey === config.altKey &&
event.metaKey === config.metaKey &&
event.key.toLowerCase() === config.key.toLowerCase();
}
// Show toast notification (fast, subtle)
function showToast(message, type = 'info', duration = 1200) {
const toast = createElementSafe('div');
const icons = { success: '✓', error: '✗', info: 'ⓘ' };
const colors = { success: '#000', error: '#000', info: '#000' };
toast.textContent = `${icons[type] || ''} ${message}`;
toast.style.cssText = `
position: fixed;
top: 16px;
right: 16px;
background: ${colors[type] || colors.info};
color: white;
padding: 8px 16px;
border-radius: 6px;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
font-size: 13px;
font-weight: 500;
box-shadow: 0 2px 8px rgba(0,0,0,0.15);
z-index: 2147483647;
opacity: 0;
transform: translateY(-10px);
transition: opacity 0.2s ease, transform 0.2s ease;
pointer-events: none;
`;
try {
document.body.appendChild(toast);
} catch (e) {
document.documentElement.appendChild(toast);
}
// Trigger animation
requestAnimationFrame(() => {
toast.style.opacity = '1';
toast.style.transform = 'translateY(0)';
});
// Auto-remove with fade out
setTimeout(() => {
toast.style.opacity = '0';
toast.style.transform = 'translateY(-10px)';
setTimeout(() => toast.remove(), 200);
}, duration);
}
// Show hotkey capture overlay with live preview
function showHotkeyPrompt() {
const promptOverlay = createElementSafe('div');
promptOverlay.style.cssText = `
position: fixed;
inset: 0;
background: rgba(0,0,0,0.5);
backdrop-filter: blur(4px);
z-index: 2147483647;
display: flex;
align-items: center;
justify-content: center;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
opacity: 0;
transition: opacity 0.2s ease;
`;
const promptBox = createElementSafe('div');
promptBox.style.cssText = `
background: white;
padding: 24px 32px;
border-radius: 12px;
text-align: center;
max-width: 400px;
box-shadow: 0 8px 32px rgba(0,0,0,0.2);
transform: scale(0.9);
transition: transform 0.2s ease;
`;
const title = createElementSafe('h2');
title.textContent = 'Set Hotkey';
title.style.cssText = 'margin: 0 0 8px 0; color: #000; font-size: 18px; font-weight: 600;';
const instruction = createElementSafe('p');
instruction.textContent = 'Hold modifiers, then press a key';
instruction.style.cssText = 'color: #000; margin: 0 0 20px 0; font-size: 13px;';
const preview = createElementSafe('div');
const currentHotkey = getHotkey();
preview.textContent = formatHotkey(currentHotkey);
preview.style.cssText = `
background: #000;
padding: 16px;
border-radius: 8px;
font-family: 'SF Mono', Monaco, 'Courier New', monospace;
font-size: 18px;
color: white;
margin-bottom: 16px;
min-height: 28px;
font-weight: 500;
letter-spacing: 1px;
`;
const hint = createElementSafe('div');
hint.textContent = 'Current hotkey shown above';
hint.style.cssText = 'font-size: 11px; color: #000; margin-bottom: 16px;';
const cancelBtn = createElementSafe('button');
cancelBtn.textContent = 'Cancel';
cancelBtn.style.cssText = `
background: #f0f0f0;
color: #333;
border: none;
padding: 8px 20px;
border-radius: 6px;
cursor: pointer;
font-size: 13px;
font-weight: 500;
transition: background 0.15s ease;
`;
cancelBtn.onmouseover = () => cancelBtn.style.background = '#e0e0e0';
cancelBtn.onmouseout = () => cancelBtn.style.background = '#f0f0f0';
cancelBtn.onclick = () => {
document.removeEventListener('keydown', captureKey, true);
document.removeEventListener('keyup', updatePreview, true);
promptOverlay.style.opacity = '0';
setTimeout(() => promptOverlay.remove(), 200);
};
promptBox.appendChild(title);
promptBox.appendChild(instruction);
promptBox.appendChild(preview);
promptBox.appendChild(hint);
promptBox.appendChild(cancelBtn);
promptOverlay.appendChild(promptBox);
// Update preview to show currently held modifiers
const updatePreview = (e) => {
const parts = [];
if (e.ctrlKey) parts.push('Ctrl');
if (e.shiftKey) parts.push('Shift');
if (e.altKey) parts.push('Alt');
if (e.metaKey) parts.push('Cmd');
if (parts.length === 0) {
preview.textContent = formatHotkey(currentHotkey);
preview.style.background = '#f5f5f5';
preview.style.color = '#333';
hint.textContent = 'Current hotkey shown above';
} else {
preview.textContent = parts.join('+') + ' + ...';
preview.style.background = '#e3f2fd';
preview.style.color = '#1976d2';
hint.textContent = 'Now press a key to complete combo';
}
};
// Capture keydown event
const captureKey = (e) => {
e.preventDefault();
e.stopPropagation();
// Ignore modifier-only keys - wait for actual key press
if (['Control', 'Shift', 'Alt', 'Meta'].includes(e.key)) {
updatePreview(e);
return;
}
const newHotkey = {
ctrlKey: e.ctrlKey,
shiftKey: e.shiftKey,
altKey: e.altKey,
metaKey: e.metaKey,
key: e.key.toLowerCase()
};
// Save and show confirmation
setHotkey(newHotkey);
preview.textContent = formatHotkey(newHotkey);
preview.style.background = '#4CAF50';
preview.style.color = '#fff';
hint.textContent = '✓ Saved!';
hint.style.color = '#4CAF50';
document.removeEventListener('keydown', captureKey, true);
document.removeEventListener('keyup', updatePreview, true);
setTimeout(() => {
promptOverlay.style.opacity = '0';
setTimeout(() => promptOverlay.remove(), 200);
}, 800);
};
document.addEventListener('keydown', captureKey, true);
document.addEventListener('keyup', updatePreview, true);
// Safari-safe appending
try {
document.body.appendChild(promptOverlay);
} catch (e) {
document.documentElement.appendChild(promptOverlay);
}
// Trigger animation
requestAnimationFrame(() => {
promptOverlay.style.opacity = '1';
promptBox.style.transform = 'scale(1)';
});
}
function getSelector(el) {
// Priority 1: ID (most reliable)
if (el.id) return `#${el.id}`;
// Priority 2: Data attributes (often used for testing/automation)
const dataAttrs = Array.from(el.attributes).filter(a => a.name.startsWith('data-'));
if (dataAttrs.length) {
const key = dataAttrs.find(a => a.name.includes('test') || a.name.includes('id') || a.name.includes('name')) || dataAttrs[0];
return `[${key.name}="${key.value}"]`;
}
// Priority 3: Unique class combination
if (el.className && typeof el.className === 'string') {
const classes = el.className.trim().split(/\s+/).filter(c => c && !c.match(/^(active|hover|focus|disabled)$/));
if (classes.length) {
const selector = `${el.tagName.toLowerCase()}.${classes.join('.')}`;
// Check if selector is unique
if (document.querySelectorAll(selector).length === 1) return selector;
}
}
// Priority 4: Role or aria-label
if (el.getAttribute('role')) return `[role="${el.getAttribute('role')}"]`;
if (el.getAttribute('aria-label')) return `[aria-label="${el.getAttribute('aria-label')}"]`;
// Priority 5: For common elements, use semantic approach
const tag = el.tagName.toLowerCase();
if (['button', 'input', 'select', 'textarea'].includes(tag)) {
if (el.name) return `${tag}[name="${el.name}"]`;
if (el.type) return `${tag}[type="${el.type}"]`;
}
// Last resort: Minimal path from nearest ID
let path = [];
let current = el;
while (current && current !== document.body) {
let selector = current.tagName.toLowerCase();
if (current.id) {
path.unshift(`#${current.id}`);
break;
}
const parent = current.parentElement;
if (parent) {
const index = Array.from(parent.children).indexOf(current) + 1;
selector += `:nth-child(${index})`;
}
path.unshift(selector);
current = parent;
}
return path.join(' > ');
}
// Helper: prefer stable classes (filter hashed/dynamic)
function stableClasses(el) {
if (!el.className || typeof el.className !== 'string') return [];
return el.className.trim().split(/\s+/).filter(c =>
c &&
!/^(active|hover|focus|disabled|selected|open|closed|ng-|css-|sc-|is-|has-)$/.test(c) &&
!/[A-Fa-f0-9]{6,}/.test(c) && // hashed-like
!/^_[A-Za-z0-9]{5,}$/.test(c)
);
}
// Generate multiple selector flavors + rationale
function getSelectors(el) {
const tag = el.tagName.toLowerCase();
// 1) ID
if (el.id) {
return {
css: `#${CSS.escape(el.id)}`,
css_fallback: `#${CSS.escape(el.id)}`,
contextual: null,
rationale: 'id is unique and most stable'
};
}
// 2) Data-* attributes with testing names
const dataCandidates = Array.from(el.attributes)
.filter(a => a.name.startsWith('data-'))
.sort((a, b) => {
const score = (n) => /test|cy|qa|id|name|tid/i.test(n) ? 0 : 1;
return score(a.name) - score(b.name);
});
if (dataCandidates.length) {
const key = dataCandidates[0];
const css = `[${CSS.escape(key.name)}="${CSS.escape(key.value)}"]`;
return {
css,
css_fallback: css,
contextual: null,
rationale: 'preferred data-* attribute for testing'
};
}
// 3) Stable class combination unique in document
const classes = stableClasses(el);
if (classes.length) {
const sel = `${tag}.${classes.map(c => CSS.escape(c)).join('.')}`;
if (document.querySelectorAll(sel).length === 1) {
return {
css: sel,
css_fallback: sel,
contextual: null,
rationale: 'unique stable class combination'
};
}
}
// 4) ARIA/role
if (el.getAttribute('role')) {
const css = `[role="${CSS.escape(el.getAttribute('role'))}"]`;
return { css, css_fallback: css, contextual: null, rationale: 'role attribute' };
}
if (el.getAttribute('aria-label')) {
const css = `[aria-label="${CSS.escape(el.getAttribute('aria-label'))}"]`;
return { css, css_fallback: css, contextual: null, rationale: 'aria-label attribute' };
}
// 5) Semantic inputs
if (['button', 'input', 'select', 'textarea'].includes(tag)) {
if (el.name) {
const css = `${tag}[name="${CSS.escape(el.name)}"]`;
return { css, css_fallback: css, contextual: null, rationale: 'semantic name attribute' };
}
if (el.type) {
const css = `${tag}[type="${CSS.escape(el.type)}"]`;
return { css, css_fallback: css, contextual: null, rationale: 'semantic type attribute' };
}
}
// 6) Contextual selector using :has() with adjacent label if present
try {
const idFor = el.id && document.querySelector(`label[for="${CSS.escape(el.id)}"]`);
if (idFor) {
const contextual = `label[for="${CSS.escape(el.id)}"] + ${tag}`;
const hasSel = `label[for="${CSS.escape(el.id)}"]:has(+ ${tag})`;
return {
css: contextual,
css_fallback: contextual,
contextual: hasSel,
rationale: ':has() anchor via associated label'
};
}
} catch {}
// 7) Minimal path from nearest ID, prefer :nth-of-type and sibling-unique class
const path = [];
let cur = el;
const settings = getSettings();
while (cur && cur !== document.body) {
if (cur.id) {
path.unshift(`#${CSS.escape(cur.id)}`);
break;
}
let seg = cur.tagName.toLowerCase();
const sc = stableClasses(cur);
if (sc.length) {
const sibSel = `${seg}.${sc.map(c => CSS.escape(c)).join('.')}`;
const parent = cur.parentElement;
if (parent && parent.querySelectorAll(`:scope > ${sibSel}`).length === 1) {
seg = sibSel;
}
}
if (!/#|\./.test(seg)) {
const parent = cur.parentElement;
if (parent) {
const same = Array.from(parent.children).filter(n => n.tagName === cur.tagName);
const index = same.indexOf(cur) + 1;
seg += `:nth-of-type(${index})`;
}
}
path.unshift(seg);
if (!settings.deepShadow && cur.getRootNode() instanceof ShadowRoot) {
const host = cur.getRootNode().host;
if (host) {
path.unshift(host.tagName.toLowerCase());
break;
}
}
cur = cur.parentElement || (cur.getRootNode() instanceof ShadowRoot ? cur.getRootNode().host : null);
}
const finalSel = path.join(' > ');
return { css: finalSel, css_fallback: finalSel, contextual: null, rationale: 'structural path from nearest id' };
}
function highlight(el) {
const rect = el.getBoundingClientRect();
const sels = getSelectors(el);
const selector = sels.css;
// Thin precise border
overlay.style.cssText = `position:fixed;left:${rect.left}px;top:${rect.top}px;width:${rect.width}px;height:${rect.height}px;background:transparent;border:1px solid #0088ff;pointer-events:none;z-index:2147483647;display:block;box-sizing:border-box;outline:1px solid rgba(255,255,255,0.5);outline-offset:-2px`;
// Build enhanced tree view
let content = selector;
// Always build tree view to show element hierarchy with attributes
const buildTree = () => {
const elements = [];
let curr = el;
// Collect elements up to root or nearest ID
while (curr && curr !== document.body) {
const attrs = [];
// Build comprehensive element description
const tag = curr.tagName.toLowerCase();
const parts = [];
// Always start with tag
parts.push(`<span style="color:#6db3f2">${tag}</span>`);
// Add ID if present
if (curr.id) {
parts.push(`<span style="color:#86c1b9">#${curr.id}</span>`);
}
// Add classes (first 2-3 meaningful ones)
if (curr.className && typeof curr.className === 'string') {
const classes = curr.className.trim().split(/\s+/)
.filter(c => c && !c.match(/^(active|hover|focus|disabled|selected|open|closed|ng-|css-)/))
.slice(0, 2);
if (classes.length > 0) {
parts.push(`<span style="color:#f0c674">.${classes.join('.')}</span>`);
}
}
// Add key attributes
if (curr.getAttribute('role')) {
parts.push(`<span style="color:#cc99cc">[role=${curr.getAttribute('role')}]</span>`);
}
// Show actual data attributes
const dataAttrs = Array.from(curr.attributes)
.filter(attr => attr.name.startsWith('data-'))
.slice(0, 2); // Show first 2 data attributes
dataAttrs.forEach(attr => {
let value = attr.value;
if (value.length > 12) value = value.substring(0, 10) + '..';
parts.push(`<span style="color:#cc99cc">[${attr.name}="${value}"]</span>`);
});
// Add text content for leaf nodes
if (curr.textContent && curr.textContent.trim() && curr.children.length === 0) {
const text = curr.textContent.trim().substring(0, 15);
parts.push(`<span style="color:#b19cd9">"${text}${curr.textContent.trim().length > 15 ? '...' : ''}"</span>`);
}
attrs.push(parts.join(' '));
elements.unshift({
tag: curr.tagName.toLowerCase(),
attrs: attrs,
element: curr
});
if (curr.id) break; // Stop at ID
curr = curr.parentElement;
}
// Build tree display with proper tree characters
return elements.map((item, i) => {
const isTarget = i === elements.length - 1;
const isRoot = i === 0;
let line = '';
// Build indent with vertical lines
for (let j = 0; j < i; j++) {
line += j < i - 1 ? '│ ' : '';
}
// Add connector
if (!isRoot) {
line += isTarget ? '└─ ' : '├─ ';
}
let display = item.attrs.join(' ') || item.tag;
// Highlight target with better visual distinction
if (isTarget) {
return `<span style="color:#666">${line}</span><span style="background:rgba(0,255,255,0.1);padding:1px 3px;border-radius:2px">${display}</span>`;
}
return `<span style="color:#666">${line}</span>${display}`;
}).join('\n');
};
const tree = buildTree();
const shadowInfo = (function(){ try { return describeShadowPath(el); } catch { return { path: [], crossedClosed: false }; } })();
const shadowLine = shadowInfo.path && shadowInfo.path.length ? `\n<span style=\"color:#888\">shadow:</span> ${shadowInfo.path.join(' ➜ ')}${shadowInfo.crossedClosed ? ' (closed)' : ''}` : '';
// Truncate selector for display
const displaySelector = selector.length > 60 ? selector.substring(0, 57) + '...' : selector;
// Remove text preview - already shown in tree
// Only show the selector if it's different from the last item in tree
const lastTreeItem = tree.split('\n').pop();
const lastTreeText = lastTreeItem.replace(/<[^>]*>/g, '').trim();
const selectorDisplay = lastTreeText.includes(selector) || selector === lastTreeText.replace(/[└─\s]/g, '')
? ''
: `\n<div style="margin-top:8px;padding-top:8px;border-top:1px solid #444"><div style="font-size:11px;color:#0ff">${displaySelector}</div></div>`;
content = `${tree}${shadowLine}${selectorDisplay}`;
tooltip.innerHTML = content;
tooltip.style.cssText = 'position:fixed;background:#000;color:#fff;padding:10px 12px;font:11px monospace;border-radius:4px;pointer-events:none;z-index:2147483647;display:block;max-width:500px;box-shadow:0 2px 8px rgba(0,0,0,0.4);line-height:1.5;white-space:pre-wrap';
// Calculate tooltip dimensions after styling
const tooltipRect = tooltip.getBoundingClientRect();
const gap = 5;
// Find best position (priority: top, bottom, right, left)
let pos = null;
// Try above
if (rect.top - tooltipRect.height - gap > 0) {
pos = {
left: Math.min(Math.max(rect.left, gap), window.innerWidth - tooltipRect.width - gap),
top: rect.top - tooltipRect.height - gap
};
}
// Try below
else if (rect.bottom + tooltipRect.height + gap < window.innerHeight) {
pos = {
left: Math.min(Math.max(rect.left, gap), window.innerWidth - tooltipRect.width - gap),
top: rect.bottom + gap
};
}
// Try right
else if (rect.right + tooltipRect.width + gap < window.innerWidth) {
pos = {
left: rect.right + gap,
top: Math.min(Math.max(rect.top, gap), window.innerHeight - tooltipRect.height - gap)
};
}
// Try left
else if (rect.left - tooltipRect.width - gap > 0) {
pos = {
left: rect.left - tooltipRect.width - gap,
top: Math.min(Math.max(rect.top, gap), window.innerHeight - tooltipRect.height - gap)
};
}
// Fallback: top-right corner of viewport
else {
pos = {
left: window.innerWidth - tooltipRect.width - 20,
top: 20
};
}
tooltip.style.left = pos.left + 'px';
tooltip.style.top = pos.top + 'px';
}
function cleanupActiveListeners() {
if (controller) {
try { controller.abort(); } catch {}
controller = null;
}
if (ro) { try { ro.disconnect(); } catch {} ro = null; }
if (rafToken) { try { cancelAnimationFrame(rafToken); } catch {} rafToken = null; }
}
function toggle() {
active = !active;
document.body.style.cursor = active ? 'crosshair' : '';
const opts = getSettings();
if (!active) {
overlay.style.display = 'none';
tooltip.style.display = 'none';
cleanupActiveListeners();
} else {
controller = new AbortController();
const signal = controller.signal;
const pointerMove = (e) => {
if (rafToken) return; // throttle to 1 per frame
rafToken = requestAnimationFrame(() => {
rafToken = null;
if (!active) return;
let target = null;
const path = typeof e.composedPath === 'function' ? e.composedPath() : null;
if (path && path.length) {
for (const node of path) {
if (node && node.nodeType === 1) { target = node; break; }
}
}
if (!target) target = getDeepestElement(e.clientX, e.clientY);
if (!target || target === overlay || target === tooltip) return;
current = target;
highlight(target);
});
};
const passive = opts.passiveListeners ? { passive: true, signal } : { signal };
document.addEventListener('pointermove', pointerMove, passive);
document.addEventListener('pointerover', pointerMove, passive);
document.addEventListener('scroll', () => { if (active && current) highlight(current); }, { capture: true, ...passive });
window.addEventListener('resize', () => { if (active && current) highlight(current); }, passive);
try {
ro = new ResizeObserver(() => { if (active && current) highlight(current); });
ro.observe(document.documentElement);
if (document.body) ro.observe(document.body);
} catch {}
}
const hotkey = getHotkey();
if (active) {
showToast(`Selector ON • ${formatHotkey(hotkey)} to exit`, 'success', 1000);
} else {
showToast('Selector OFF', 'info', 800);
}
}
// Safari-compatible initialization with CSP handling
function safariCompatibleInit() {
return new Promise((resolve) => {
// Wait for body to be available
const waitForBody = () => {
if (document.body) {
resolve();
} else {
setTimeout(waitForBody, 10);
}
};
waitForBody();
});
}
// Initialize function
function init() {
if (initialized) return;
initialized = true;
// Create elements with CSP-safe approach
overlay = createElementSafe('div');
tooltip = createElementSafe('div');
overlay.style.display = 'none';
tooltip.style.display = 'none';
// Use safer DOM manipulation for Safari
try {
document.body.appendChild(overlay);
document.body.appendChild(tooltip);
} catch (e) {
// Fallback for CSP issues
document.documentElement.appendChild(overlay);
document.documentElement.appendChild(tooltip);
}
// Register shortcuts with native keyboard events
document.addEventListener('keydown', (e) => {
const hotkey = getHotkey();
// Check if custom hotkey matches
if (matchesHotkey(e, hotkey)) {
e.preventDefault();
toggle();
}
// Escape to exit
if (e.key === 'Escape' && active) {
e.preventDefault();
toggle();
}
});
// Active-mode listeners are attached in toggle() using pointer events
document.addEventListener('click', e => {
if (active && current) {
e.preventDefault();
e.stopPropagation();
const sels = getSelectors(current);
const selector = sels.css;
const meta = buildMeta(current, sels);
const plain = `${selector}\n---\n${JSON.stringify(meta)}`;
const copyToClipboard = async () => {
const settings = getSettings();
if (navigator.clipboard) {
try {
if (settings.richCopy && navigator.clipboard.write && typeof ClipboardItem !== 'undefined') {
const html = `<pre>${escapeHtml(JSON.stringify(meta, null, 2))}</pre>`;
const item = new ClipboardItem({
'text/plain': new Blob([plain], { type: 'text/plain' }),
'text/html': new Blob([html], { type: 'text/html' })
});
await navigator.clipboard.write([item]);
return true;
} else if (navigator.clipboard.writeText) {
await navigator.clipboard.writeText(plain);
return true;
}
} catch (err) {
// will fallback
}
}
// Legacy fallback
const textarea = createElementSafe('textarea');
textarea.value = plain;
textarea.style.position = 'fixed';
textarea.style.opacity = '0';
textarea.style.pointerEvents = 'none';
document.body.appendChild(textarea);
textarea.select();
textarea.setSelectionRange(0, 99999);
let ok = false;
try { ok = document.execCommand('copy'); } catch {}
try { document.body.removeChild(textarea); } catch {}
return ok;
};
copyToClipboard().then(success => {
if (success) {
const displaySel = selector.length > 40 ? selector.substring(0, 37) + '...' : selector;
showToast(`Copied: ${displaySel}`, 'success', 1500);
} else {
showToast('Copy denied by browser policy', 'error', 1500);
}
});
}
});
// Register Tampermonkey menu commands
const hotkey = getHotkey();
GM_registerMenuCommand('Toggle Selector Tool', () => {
toggle();
}, {
accessKey: 't',
autoClose: true,
title: `Toggle selector mode (Hotkey: ${formatHotkey(hotkey)})`
});
GM_registerMenuCommand('Change hotkey...', () => {
showHotkeyPrompt();
}, {
accessKey: 'h',
autoClose: true,
title: 'Change the keyboard shortcut for toggling selector mode'
});
// Settings toggles for quick control
GM_registerMenuCommand('Toggle rich copy (text+HTML+JSON)', () => {
const s = getSettings();
s.richCopy = !s.richCopy; saveSettings(s);
showToast(`Rich copy ${s.richCopy ? 'enabled' : 'disabled'}`);
}, { accessKey: 'r', autoClose: true, title: 'Write both text and HTML to clipboard when possible' });
GM_registerMenuCommand('Toggle deep shadow traversal', () => {
const s = getSettings();
s.deepShadow = !s.deepShadow; saveSettings(s);
showToast(`Deep shadow ${s.deepShadow ? 'enabled' : 'disabled'}`);
}, { accessKey: 'd', autoClose: true, title: 'Traverse open shadow roots when generating selectors' });
GM_registerMenuCommand('Toggle SPA-aware navigation hooks', () => {
const s = getSettings();
s.spaAware = !s.spaAware; saveSettings(s);
showToast(`SPA hooks ${s.spaAware ? 'enabled' : 'disabled'}`);
}, { accessKey: 's', autoClose: true, title: 'Reinitialize on pushState/replaceState/popstate/hashchange' });
}
// Safari-compatible initialization with better CSP handling
function safariInit() {
// For Safari, we need to be more careful about timing and CSP
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => {
safariCompatibleInit().then(init);
});
} else {
// DOM already loaded, but wait for body to be ready
safariCompatibleInit().then(init);
}
}
// Check if we're in Safari and use appropriate initialization
const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
if (isSafari) {
// Safari-specific initialization
safariInit();
} else {
// Standard initialization for other browsers
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
}
// SPA-aware navigation hooks: patch history and listen for events
(function setupSpaHooksOnce() {
if (window.__est_spa_hooks_installed__) return; // idempotent
window.__est_spa_hooks_installed__ = true;
const dispatchNav = () => window.dispatchEvent(new Event('est:navigation'));
const origPush = history.pushState;
const origReplace = history.replaceState;
history.pushState = function() { const r = origPush.apply(this, arguments); dispatchNav(); return r; };
history.replaceState = function() { const r = origReplace.apply(this, arguments); dispatchNav(); return r; };
window.addEventListener('popstate', dispatchNav);
window.addEventListener('hashchange', dispatchNav);
})();
const onNav = () => {
const s = getSettings();
if (!s.spaAware) return;
setTimeout(() => {
if (!initialized || !document.body.contains(overlay)) {
initialized = false;
init();
}
}, 300);
};
window.addEventListener('est:navigation', onNav);
// Helpers inside IIFE
function escapeHtml(s) {
return String(s).replace(/[&<>\"]/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"' }[c]));
}
function stableClassList(el) {
if (!el.className || typeof el.className !== 'string') return [];
return el.className.trim().split(/\s+/).filter(Boolean).slice(0, 6);
}
function buildMeta(el, sels) {
const getAttr = (n) => el.getAttribute(n);
const dataAttrs = {};