fix: gate content-script debug logging behind dev mode

Selection text substrings no longer reach the host page's console in
production builds; logs only fire under 'wxt' dev serve.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
john kevin asprec
2026-07-14 21:26:21 +08:00
parent f7fcd41341
commit a5608e7b09
2 changed files with 19 additions and 10 deletions

View File

@@ -3,7 +3,16 @@ import { defineContentScript } from 'wxt/utils/define-content-script';
export default defineContentScript({
matches: ['<all_urls>'],
main() {
console.log('LexAI content script loaded');
// Debug logging is stripped in production builds — selection text must never
// reach the host page's console outside dev.
const isDev = import.meta.env.COMMAND === 'serve';
const debugLog = (...args: unknown[]) => {
if (isDev) console.log(...args);
};
const debugWarn = (...args: unknown[]) => {
if (isDev) console.warn(...args);
};
debugLog('LexAI content script loaded');
// ─── State ───────────────────────────────────────────────────────────────
let toolbar: HTMLElement | null = null;
@@ -83,7 +92,7 @@ export default defineContentScript({
storedElement = activeEl;
storedRange = null;
selectedText = activeEl.value.substring(start, end);
console.log('[LexAI captureForButton] textarea captured', { start, end, text: selectedText.substring(0, 40) });
debugLog('[LexAI captureForButton] textarea captured', { start, end, text: selectedText.substring(0, 40) });
}
} else {
const sel = window.getSelection();
@@ -93,7 +102,7 @@ export default defineContentScript({
storedStart = -1;
storedEnd = -1;
selectedText = sel.toString().trim();
console.log('[LexAI captureForButton] DOM range captured', { text: selectedText.substring(0, 40) });
debugLog('[LexAI captureForButton] DOM range captured', { text: selectedText.substring(0, 40) });
}
}
}
@@ -586,7 +595,7 @@ export default defineContentScript({
const snapElement = storedElement;
const snapRange = storedRange ? storedRange.cloneRange() : null;
console.log('[LexAI runAction] snapshot captured', {
debugLog('[LexAI runAction] snapshot captured', {
snapStart,
snapEnd,
snapElement: snapElement?.tagName,
@@ -868,7 +877,7 @@ export default defineContentScript({
replaceBtn.addEventListener('mousedown', (e) => e.preventDefault());
replaceBtn.addEventListener('click', () => {
// Debug log — confirms exactly what state we have at replace time
console.log('[LexAI Replace]', {
debugLog('[LexAI Replace]', {
snapStart,
snapEnd,
snapElement: snapElement?.tagName ?? 'null',
@@ -881,8 +890,8 @@ export default defineContentScript({
// textarea / input path — use the snapshot indices directly
const before = snapElement.value.substring(0, snapStart);
const after = snapElement.value.substring(snapEnd);
console.log('[LexAI Replace] before:', JSON.stringify(before.substring(0, 30)));
console.log('[LexAI Replace] after:', JSON.stringify(after.substring(0, 30)));
debugLog('[LexAI Replace] before:', JSON.stringify(before.substring(0, 30)));
debugLog('[LexAI Replace] after:', JSON.stringify(after.substring(0, 30)));
snapElement.value = before + resultText + after;
snapElement.setSelectionRange(snapStart, snapStart + resultText.length);
snapElement.dispatchEvent(new Event('input', { bubbles: true }));
@@ -893,7 +902,7 @@ export default defineContentScript({
// Strategy: restore the original selection, then just "type" the new text.
// The browser automatically replaces selected text on insert — no manual
// deleteContents() needed (and safer since stale range deletes can go wrong).
console.log('[LexAI Replace] using DOM range — restoring selection + inserting');
debugLog('[LexAI Replace] using DOM range — restoring selection + inserting');
try {
const sel = window.getSelection();
if (sel) {
@@ -902,10 +911,10 @@ export default defineContentScript({
}
document.execCommand('insertText', false, resultText); // replaces selection
} catch (err) {
console.warn('[LexAI Replace] execCommand failed:', err);
debugWarn('[LexAI Replace] execCommand failed:', err);
}
} else {
console.warn('[LexAI Replace] NO VALID SNAP STATE — snapStart:', snapStart, 'snapEnd:', snapEnd, 'snapElement:', snapElement, 'snapRange:', snapRange);
debugWarn('[LexAI Replace] NO VALID SNAP STATE — snapStart:', snapStart, 'snapEnd:', snapEnd, 'snapElement:', snapElement, 'snapRange:', snapRange);
}
selectionLocked = false;

Binary file not shown.