From d64fa0e0ec24a573dc4a504039c47dfb7494aed5 Mon Sep 17 00:00:00 2001 From: Chad Curtis Date: Thu, 18 Sep 2025 06:49:16 +0000 Subject: [PATCH] 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 --- iframe-fetch-client/_iframe-client.js | 91 ++------------------------- iframe-fetch-client/console-utils.js | 83 ++++++++++++++++++++++++ iframe-fetch-client/index.html | 33 +++------- 3 files changed, 95 insertions(+), 112 deletions(-) create mode 100644 iframe-fetch-client/console-utils.js diff --git a/iframe-fetch-client/_iframe-client.js b/iframe-fetch-client/_iframe-client.js index 759d0033..78cceb72 100644 --- a/iframe-fetch-client/_iframe-client.js +++ b/iframe-fetch-client/_iframe-client.js @@ -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(() => { diff --git a/iframe-fetch-client/console-utils.js b/iframe-fetch-client/console-utils.js new file mode 100644 index 00000000..8c7a41b9 --- /dev/null +++ b/iframe-fetch-client/console-utils.js @@ -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 + }; +} \ No newline at end of file diff --git a/iframe-fetch-client/index.html b/iframe-fetch-client/index.html index 7bc770de..fc05b899 100644 --- a/iframe-fetch-client/index.html +++ b/iframe-fetch-client/index.html @@ -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) {