-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebug-entry.js
More file actions
120 lines (110 loc) · 3.62 KB
/
Copy pathdebug-entry.js
File metadata and controls
120 lines (110 loc) · 3.62 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
import './src/main.js';
import { announce } from './src/chat-accessibility.js';
import { t } from './src/settings-state.js';
import {
isStatusTransitionDiagnosticActive,
startStatusTransitionDiagnostic,
stopStatusTransitionDiagnostic
} from './src/status-transition-diagnostics.js';
function announceDebug(message) {
const liveRegion = document.getElementById('wa-plus-live-region');
if (!liveRegion) {
console.info(message);
return;
}
liveRegion.textContent = '';
setTimeout(() => {
liveRegion.textContent = message;
setTimeout(() => {
if (liveRegion.textContent === message) liveRegion.textContent = '';
}, 3500);
}, 0);
}
function copyTextFallback(text) {
const previouslyFocused = document.activeElement;
const textarea = document.createElement('textarea');
textarea.value = text;
textarea.setAttribute('readonly', '');
Object.assign(textarea.style, {
position: 'fixed',
opacity: '0',
pointerEvents: 'none'
});
(document.body || document.documentElement).appendChild(textarea);
try {
textarea.focus();
textarea.select();
return document.execCommand('copy');
} catch {
return false;
} finally {
textarea.remove();
try {
previouslyFocused?.focus?.({ preventScroll: true });
} catch {}
}
}
async function copyText(text, allowFallback = true) {
const previousFocus = document.activeElement;
try {
if (typeof navigator.clipboard?.writeText === 'function') {
await navigator.clipboard.writeText(text);
return true;
}
} catch {}
return allowFallback && document.activeElement === previousFocus && copyTextFallback(text);
}
async function copyDebugHtml() {
const doctype = document.doctype
? new XMLSerializer().serializeToString(document.doctype)
: '';
const debugData = `${doctype ? `${doctype}\n` : ''}${document.documentElement.outerHTML}`;
if (await copyText(debugData)) {
announceDebug('Debug HTML copied. Sensitive chat and contact data included; redact before sharing.');
} else {
announceDebug('Failed to copy debug HTML');
}
}
let statusTransitionCopyPending = false;
let pendingStatusTransitionReport = '';
async function toggleStatusTransitionDiagnostic() {
if (statusTransitionCopyPending) return;
if (pendingStatusTransitionReport) {
statusTransitionCopyPending = true;
const copied = await copyText(pendingStatusTransitionReport, false);
statusTransitionCopyPending = false;
if (copied) pendingStatusTransitionReport = '';
announce(t(copied
? 'statusTransitionDiagnosticCopied'
: 'statusTransitionDiagnosticCopyFailed'));
return;
}
if (!isStatusTransitionDiagnosticActive()) {
startStatusTransitionDiagnostic();
announce(t('statusTransitionDiagnosticStarted'));
return;
}
const diagnostic = stopStatusTransitionDiagnostic();
pendingStatusTransitionReport = diagnostic;
statusTransitionCopyPending = true;
const copied = await copyText(diagnostic, false);
statusTransitionCopyPending = false;
if (copied) pendingStatusTransitionReport = '';
announce(t(copied
? 'statusTransitionDiagnosticCopied'
: 'statusTransitionDiagnosticCopyFailed'));
}
window.addEventListener('keydown', event => {
if (event.repeat || event.isComposing || event.defaultPrevented ||
event.getModifierState?.('AltGraph') || !event.altKey || !event.shiftKey ||
event.ctrlKey || event.metaKey) return;
const action = event.code === 'Digit0'
? copyDebugHtml
: event.code === 'Digit7'
? toggleStatusTransitionDiagnostic
: null;
if (!action) return;
event.preventDefault();
event.stopImmediatePropagation();
action();
}, true);