From ca2426a80070fa786126771bf04d36ed87ee6872 Mon Sep 17 00:00:00 2001 From: Chad Curtis Date: Sat, 20 Sep 2025 03:37:20 +0000 Subject: [PATCH] Further cleanup + make errors copiable --- .../_iframe-console-intercept.js | 114 ----------------- .../{_iframe-client.css => core/client.css} | 0 .../{ => core}/console-utils.js | 0 .../{_iframe-client.js => core/main.js} | 120 +++++++++++++++++- iframe-fetch-client/index.html | 6 +- src/components/Shakespeare/PreviewPane.tsx | 58 +++++++-- 6 files changed, 164 insertions(+), 134 deletions(-) delete mode 100644 iframe-fetch-client/_iframe-console-intercept.js rename iframe-fetch-client/{_iframe-client.css => core/client.css} (100%) rename iframe-fetch-client/{ => core}/console-utils.js (100%) rename iframe-fetch-client/{_iframe-client.js => core/main.js} (79%) diff --git a/iframe-fetch-client/_iframe-console-intercept.js b/iframe-fetch-client/_iframe-console-intercept.js deleted file mode 100644 index 0d1bed76..00000000 --- a/iframe-fetch-client/_iframe-console-intercept.js +++ /dev/null @@ -1,114 +0,0 @@ -/** - * Early Console Interception Inject Script - * This script runs as early as possible to capture all console messages and errors - */ - -class ConsoleInterceptor { - constructor() { - this.originalConsole = {}; - this.earlyErrors = []; - this.consoleMethods = [ - 'log', 'warn', 'error', 'info', 'debug', 'exception', 'assert', - 'clear', 'count', 'countReset', 'dir', 'dirxml', 'group', - 'groupCollapsed', 'groupEnd', 'table', 'time', 'timeEnd', - 'timeLog', 'trace' - ]; - - this.init(); - } - - init() { - this.storeOriginalConsole(); - this.overrideConsoleMethods(); - this.setupGlobalErrorHandlers(); - this.exposeToWindow(); - } - - storeOriginalConsole() { - this.consoleMethods.forEach(method => { - if (typeof console[method] === 'function') { - this.originalConsole[method] = console[method]; - } - }); - } - - overrideConsoleMethods() { - this.consoleMethods.forEach(method => { - if (typeof console[method] === 'function') { - console[method] = (...args) => { - this.handleConsoleCall(method, args); - }; - } - }); - } - - handleConsoleCall(method, args) { - // Try to call the original method - try { - this.originalConsole[method](...args); - } catch (e) { - // Original method failed, continue - } - - // Store error for later processing - if (method === 'error' || method === 'exception') { - this.earlyErrors.push({ - level: method, - message: this.serializeArgs(args), - timestamp: Date.now(), - args: args - }); - } - } - - serializeArgs(args) { - return args.map(arg => { - if (typeof arg === 'object') { - try { - return JSON.stringify(arg); - } catch { - return String(arg); - } - } - return String(arg); - }).join(' '); - } - - setupGlobalErrorHandlers() { - // Handle window errors - window.addEventListener('error', (event) => { - this.earlyErrors.push({ - level: 'error', - message: `Global Error: ${event.message}`, - timestamp: Date.now(), - args: [{ - filename: event.filename, - lineno: event.lineno, - colno: event.colno, - error: event.error ? event.error.message : 'No error object' - }] - }); - }); - - // Handle unhandled promise rejections - window.addEventListener('unhandledrejection', (event) => { - this.earlyErrors.push({ - level: 'error', - message: 'Unhandled Promise Rejection', - timestamp: Date.now(), - args: [event.reason] - }); - }); - } - - exposeToWindow() { - // Store early errors for the main script to process - window._earlyConsoleErrors = this.earlyErrors; - - // Store original console reference for the main script - window._originalConsole = this.originalConsole; - } -} - -// Initialize the console interceptor immediately -new ConsoleInterceptor(); \ No newline at end of file diff --git a/iframe-fetch-client/_iframe-client.css b/iframe-fetch-client/core/client.css similarity index 100% rename from iframe-fetch-client/_iframe-client.css rename to iframe-fetch-client/core/client.css diff --git a/iframe-fetch-client/console-utils.js b/iframe-fetch-client/core/console-utils.js similarity index 100% rename from iframe-fetch-client/console-utils.js rename to iframe-fetch-client/core/console-utils.js diff --git a/iframe-fetch-client/_iframe-client.js b/iframe-fetch-client/core/main.js similarity index 79% rename from iframe-fetch-client/_iframe-client.js rename to iframe-fetch-client/core/main.js index 1090ac88..9d3738bf 100644 --- a/iframe-fetch-client/_iframe-client.js +++ b/iframe-fetch-client/core/main.js @@ -1,13 +1,118 @@ /** - * iframe Fetch Client Main Script - * JSON-RPC based communication with parent page + * iframe Fetch Client Combined Entry Point + * Combines console interception and client initialization */ -import { overrideConsoleMethods, setupGlobalErrorHandlers, CONSOLE_METHODS } from './console-utils.js'; +// Import console interception utilities +import { storeOriginalConsole, overrideConsoleMethods, setupGlobalErrorHandlers, CONSOLE_METHODS } from './console-utils.js'; +// Early Console Interception - runs immediately +class EarlyConsoleInterceptor { + constructor() { + this.originalConsole = {}; + this.earlyErrors = []; + this.consoleMethods = CONSOLE_METHODS; + + this.init(); + } + + init() { + this.storeOriginalConsole(); + this.overrideConsoleMethods(); + this.setupGlobalErrorHandlers(); + this.exposeToWindow(); + } + + storeOriginalConsole() { + this.consoleMethods.forEach(method => { + if (typeof console[method] === 'function') { + this.originalConsole[method] = console[method]; + } + }); + } + + overrideConsoleMethods() { + this.consoleMethods.forEach(method => { + if (typeof console[method] === 'function') { + console[method] = (...args) => { + this.handleConsoleCall(method, args); + }; + } + }); + } + + handleConsoleCall(method, args) { + // Try to call the original method + try { + this.originalConsole[method](...args); + } catch (e) { + // Original method failed, continue + } + + // Store error for later processing + if (method === 'error' || method === 'exception') { + this.earlyErrors.push({ + level: method, + message: this.serializeArgs(args), + timestamp: Date.now(), + args: args + }); + } + } + + serializeArgs(args) { + return args.map(arg => { + if (typeof arg === 'object') { + try { + return JSON.stringify(arg); + } catch { + return String(arg); + } + } + return String(arg); + }).join(' '); + } + + setupGlobalErrorHandlers() { + // Handle window errors + window.addEventListener('error', (event) => { + this.earlyErrors.push({ + level: 'error', + message: `Global Error: ${event.message}`, + timestamp: Date.now(), + args: [{ + filename: event.filename, + lineno: event.lineno, + colno: event.colno, + error: event.error ? event.error.message : 'No error object' + }] + }); + }); + + // Handle unhandled promise rejections + window.addEventListener('unhandledrejection', (event) => { + this.earlyErrors.push({ + level: 'error', + message: 'Unhandled Promise Rejection', + timestamp: Date.now(), + args: [event.reason] + }); + }); + } + + exposeToWindow() { + // Store early errors for the main script to process + window._earlyConsoleErrors = this.earlyErrors; + + // Store original console reference for the main script + window._originalConsole = this.originalConsole; + } +} + +// Console Interceptor - handles ongoing console interception class ConsoleInterceptor { constructor() { - // Use the original console stored by the inject script + // Use the original console stored by the early interceptor this.originalConsole = window._originalConsole || console; overrideConsoleMethods(this.originalConsole, this.sendConsoleMessage.bind(this)); setupGlobalErrorHandlers(this.sendConsoleMessage.bind(this)); @@ -120,6 +225,7 @@ class ConsoleInterceptor { } } +// JSON-RPC Client for communication with parent class JSONRPCClient { constructor() { this.requestId = 0; @@ -189,6 +295,7 @@ class JSONRPCClient { } } +// Main Fetch Client class FetchClient { constructor() { this.rpcClient = new JSONRPCClient(); @@ -394,9 +501,12 @@ class FetchClient { } } +// Initialize early console interception immediately +new EarlyConsoleInterceptor(); + // Initialize when DOM is ready if (document.readyState === "loading") { document.addEventListener("DOMContentLoaded", () => new FetchClient()); } else { new FetchClient(); -} +} \ No newline at end of file diff --git a/iframe-fetch-client/index.html b/iframe-fetch-client/index.html index ebfd3b6c..496afd18 100644 --- a/iframe-fetch-client/index.html +++ b/iframe-fetch-client/index.html @@ -4,9 +4,7 @@ Site Preview - - - +
@@ -14,6 +12,6 @@

Loading preview...

Waiting for ServiceWorker and parent communication

- + diff --git a/src/components/Shakespeare/PreviewPane.tsx b/src/components/Shakespeare/PreviewPane.tsx index cbf712fa..98d19306 100644 --- a/src/components/Shakespeare/PreviewPane.tsx +++ b/src/components/Shakespeare/PreviewPane.tsx @@ -6,7 +6,7 @@ import { useFS } from '@/hooks/useFS'; import { Tabs, TabsContent } from '@/components/ui/tabs'; import { ScrollArea, ScrollBar } from '@/components/ui/scroll-area'; import { Button } from '@/components/ui/button'; -import { FolderOpen, ArrowLeft, X, Bug } from 'lucide-react'; +import { FolderOpen, ArrowLeft, X, Bug, Copy, Check } from 'lucide-react'; import { useIsMobile } from '@/hooks/useIsMobile'; import { GitStatusIndicator } from '@/components/GitStatusIndicator'; import { BrowserAddressBar } from '@/components/ui/browser-address-bar'; @@ -48,7 +48,7 @@ interface JSONRPCResponse { statusText: string; headers: Record; body: string | null; - }; + } error?: { code: number; message: string; @@ -77,6 +77,7 @@ export function PreviewPane({ projectId, activeTab }: PreviewPaneProps) { const [navigationHistory, setNavigationHistory] = useState(['/']); const [historyIndex, setHistoryIndex] = useState(0); const [consoleMessages, setConsoleMessages] = useState([]); + const [copiedMessageId, setCopiedMessageId] = useState(null); const iframeRef = useRef(null); const { fs } = useFS(); const projectsManager = useProjectsManager(); @@ -436,6 +437,27 @@ export function PreviewPane({ projectId, activeTab }: PreviewPaneProps) { setConsoleMessages([]); }; + + + const copyMessageToClipboard = async (msg: ConsoleMessage) => { + try { + // Create a formatted string with all message details + const formattedMessage = `[${formatTimestamp(msg.timestamp)}] [${msg.level.toUpperCase()}] ${msg.message}${ + msg.args.length > 0 ? `\nArgs: ${JSON.stringify(msg.args, null, 2)}` : '' + }`; + + await navigator.clipboard.writeText(formattedMessage); + setCopiedMessageId(msg.id); + + // Reset the copied state after 2 seconds + setTimeout(() => { + setCopiedMessageId(null); + }, 2000); + } catch (error) { + console.error('Failed to copy message to clipboard:', error); + } + }; + return ( @@ -446,15 +468,15 @@ export function PreviewPane({ projectId, activeTab }: PreviewPaneProps) { )} - +
Console Output ({consoleMessages.length})
- -
+ +
{consoleMessages.length === 0 ? (
No console messages @@ -463,15 +485,29 @@ export function PreviewPane({ projectId, activeTab }: PreviewPaneProps) { consoleMessages.map((msg) => (
-
- {getLevelIcon(msg.level)} - {formatTimestamp(msg.timestamp)} +
+
+ {getLevelIcon(msg.level)} + {formatTimestamp(msg.timestamp)} +
+
-
{msg.message}
+
{msg.message}
{msg.args.length > 0 && ( -
+
Args: {JSON.stringify(msg.args)}
)}