Deduplicate console interception code

- Extract shared console utilities to console-utils.js
- Remove duplicated console methods array
- Consolidate original console storage logic
- Extract console override and error handler patterns
- Reduced code duplication by ~90 lines
This commit is contained in:
Chad Curtis committed 2025-09-20 02:16:40 +00:00
1 parent 696cbca55a
commit d64fa0e0ec
3 files changed
+95 -112

No files matched your search

+5 -86
View File
@@ -3,97 +3,16 @@
* JSON-RPC based communication with parent page
*/
import { storeOriginalConsole, overrideConsoleMethods, setupGlobalErrorHandlers, CONSOLE_METHODS } from './console-utils.js';
class ConsoleInterceptor {
constructor() {
this.originalConsole = {
log: console.log,
warn: console.warn,
error: console.error,
info: console.info,
debug: console.debug,
exception: console.exception,
assert: console.assert,
clear: console.clear,
count: console.count,
countReset: console.countReset,
dir: console.dir,
dirxml: console.dirxml,
group: console.group,
groupCollapsed: console.groupCollapsed,
groupEnd: console.groupEnd,
table: console.table,
time: console.time,
timeEnd: console.timeEnd,
timeLog: console.timeLog,
trace: console.trace,
};
this.overrideConsoleMethods();
this.overrideErrorHandlers();
this.originalConsole = storeOriginalConsole();
overrideConsoleMethods(this.originalConsole, this.sendConsoleMessage.bind(this));
setupGlobalErrorHandlers(this.sendConsoleMessage.bind(this));
this.overrideWindowError();
}
overrideConsoleMethods() {
const methods = ['log', 'warn', 'error', 'info', 'debug', 'exception', 'assert', 'clear', 'count', 'countReset', 'dir', 'dirxml', 'group', 'groupCollapsed', 'groupEnd', 'table', 'time', 'timeEnd', 'timeLog', 'trace'];
methods.forEach(method => {
if (typeof console[method] === 'function') {
console[method] = (...args) => {
// Call original method first (for local debugging)
try {
this.originalConsole[method](...args);
} catch (e) {
// If original method fails, at least try to send to parent
this.sendConsoleMessage(method, ...args);
return;
}
// Send to parent via JSON-RPC
this.sendConsoleMessage(method, ...args);
};
}
});
}
overrideErrorHandlers() {
// Override window.onerror to catch unhandled errors
const originalOnError = window.onerror;
window.onerror = (message, source, lineno, colno, error) => {
// Call original handler first
if (originalOnError) {
originalOnError(message, source, lineno, colno, error);
}
// Send to parent
this.sendConsoleMessage('error', `Uncaught Error: ${message}`, {
source,
lineno,
colno,
error: error ? error.message : 'No error object',
stack: error ? error.stack : 'No stack trace'
});
return false; // Don't prevent default
};
// Override window.onunhandledrejection for Promise rejections
const originalOnUnhandledRejection = window.onunhandledrejection;
window.onunhandledrejection = (event) => {
// Call original handler first
if (originalOnUnhandledRejection) {
originalOnUnhandledRejection(event);
}
// Send to parent
this.sendConsoleMessage('error', 'Unhandled Promise Rejection', {
reason: event.reason,
promise: event.promise ? 'Promise object' : 'No promise object'
});
event.preventDefault(); // Prevent default console warning
};
}
overrideWindowError() {
// Also try to catch errors that might occur during early execution
setTimeout(() => {
+83
View File
@@ -0,0 +1,83 @@
/**
* Shared console interception utilities
*/
// List of all console methods to intercept
export const CONSOLE_METHODS = [
'log', 'warn', 'error', 'info', 'debug',
'exception', 'assert', 'clear', 'count', 'countReset',
'dir', 'dirxml', 'group', 'groupCollapsed', 'groupEnd',
'table', 'time', 'timeEnd', 'timeLog', 'trace'
];
// Store original console methods
export function storeOriginalConsole() {
const original = {};
CONSOLE_METHODS.forEach(method => {
if (typeof console[method] === 'function') {
original[method] = console[method];
}
});
return original;
}
// Override console methods with custom handler
export function overrideConsoleMethods(originalConsole, messageHandler) {
CONSOLE_METHODS.forEach(method => {
if (typeof console[method] === 'function') {
console[method] = (...args) => {
// Call original method first
try {
originalConsole[method](...args);
} catch (e) {
// If original fails, at least send to parent
messageHandler(method, ...args);
return;
}
// Send to parent
messageHandler(method, ...args);
};
}
});
}
// Set up global error handlers
export function setupGlobalErrorHandlers(messageHandler) {
// Override window.onerror to catch unhandled errors
const originalOnError = window.onerror;
window.onerror = (message, source, lineno, colno, error) => {
// Call original handler first
if (originalOnError) {
originalOnError(message, source, lineno, colno, error);
}
// Send to parent
messageHandler('error', `Uncaught Error: ${message}`, {
source,
lineno,
colno,
error: error ? error.message : 'No error object',
stack: error ? error.stack : 'No stack trace'
});
return false; // Don't prevent default
};
// Override window.onunhandledrejection for Promise rejections
const originalOnUnhandledRejection = window.onunhandledrejection;
window.onunhandledrejection = (event) => {
// Call original handler first
if (originalOnUnhandledRejection) {
originalOnUnhandledRejection(event);
}
// Send to parent
messageHandler('error', 'Unhandled Promise Rejection', {
reason: event.reason,
promise: event.promise ? 'Promise object' : 'No promise object'
});
event.preventDefault(); // Prevent default console warning
};
}
+7 -26
View File
@@ -10,39 +10,20 @@
// Set up console interception as early as possible to catch all errors
(function() {
// Store original console methods before any other script runs
const originalConsole = {
log: console.log,
warn: console.warn,
error: console.error,
info: console.info,
debug: console.debug,
exception: console.exception,
assert: console.assert,
clear: console.clear,
count: console.count,
countReset: console.countReset,
dir: console.dir,
dirxml: console.dirxml,
group: console.group,
groupCollapsed: console.groupCollapsed,
groupEnd: console.groupEnd,
table: console.table,
time: console.time,
timeEnd: console.timeEnd,
timeLog: console.timeLog,
trace: console.trace,
};
const originalConsole = {};
const methods = ['log', 'warn', 'error', 'info', 'debug', 'exception', 'assert', 'clear', 'count', 'countReset', 'dir', 'dirxml', 'group', 'groupCollapsed', 'groupEnd', 'table', 'time', 'timeEnd', 'timeLog', 'trace'];
methods.forEach(method => {
if (typeof console[method] === 'function') {
originalConsole[method] = console[method];
}
});
// Early error handlers to catch setup errors
const earlyErrors = [];
// Override console methods immediately
const methods = ['log', 'warn', 'error', 'info', 'debug', 'exception', 'assert', 'clear', 'count', 'countReset', 'dir', 'dirxml', 'group', 'groupCollapsed', 'groupEnd', 'table', 'time', 'timeEnd', 'timeLog', 'trace'];
methods.forEach(method => {
if (typeof console[method] === 'function') {
console[method] = function(...args) {
// Call original method
try {
originalConsole[method](...args);
} catch (e) {