mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 08:37:22 +00:00
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:
1 parent
696cbca55a
commit
d64fa0e0ec
3 files changed
+95
-112
No files matched your search
@@ -3,97 +3,16 @@
|
|||||||
* JSON-RPC based communication with parent page
|
* JSON-RPC based communication with parent page
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { storeOriginalConsole, overrideConsoleMethods, setupGlobalErrorHandlers, CONSOLE_METHODS } from './console-utils.js';
|
||||||
|
|
||||||
class ConsoleInterceptor {
|
class ConsoleInterceptor {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.originalConsole = {
|
this.originalConsole = storeOriginalConsole();
|
||||||
log: console.log,
|
overrideConsoleMethods(this.originalConsole, this.sendConsoleMessage.bind(this));
|
||||||
warn: console.warn,
|
setupGlobalErrorHandlers(this.sendConsoleMessage.bind(this));
|
||||||
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.overrideWindowError();
|
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() {
|
overrideWindowError() {
|
||||||
// Also try to catch errors that might occur during early execution
|
// Also try to catch errors that might occur during early execution
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
|||||||
@@ -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
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -10,39 +10,20 @@
|
|||||||
// Set up console interception as early as possible to catch all errors
|
// Set up console interception as early as possible to catch all errors
|
||||||
(function() {
|
(function() {
|
||||||
// Store original console methods before any other script runs
|
// Store original console methods before any other script runs
|
||||||
const originalConsole = {
|
const originalConsole = {};
|
||||||
log: console.log,
|
const methods = ['log', 'warn', 'error', 'info', 'debug', 'exception', 'assert', 'clear', 'count', 'countReset', 'dir', 'dirxml', 'group', 'groupCollapsed', 'groupEnd', 'table', 'time', 'timeEnd', 'timeLog', 'trace'];
|
||||||
warn: console.warn,
|
methods.forEach(method => {
|
||||||
error: console.error,
|
if (typeof console[method] === 'function') {
|
||||||
info: console.info,
|
originalConsole[method] = console[method];
|
||||||
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,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Early error handlers to catch setup errors
|
// Early error handlers to catch setup errors
|
||||||
const earlyErrors = [];
|
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 => {
|
methods.forEach(method => {
|
||||||
if (typeof console[method] === 'function') {
|
if (typeof console[method] === 'function') {
|
||||||
console[method] = function(...args) {
|
console[method] = function(...args) {
|
||||||
// Call original method
|
|
||||||
try {
|
try {
|
||||||
originalConsole[method](...args);
|
originalConsole[method](...args);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
Reference in new issue
Block a user