Fix extension context detection in runtime providers

Replace window.sendRuntimeMessage existence check with URL-based detection.
The sendRuntimeMessage function exists in both extension and non-extension
contexts (e.g., downloaded HTML artifacts), causing incorrect behavior.

Changes:
- Add window.__isExtensionContext() helper to RuntimeMessageBridge that checks:
  - chrome-extension:// URLs (Chrome)
  - moz-extension:// URLs (Firefox)
  - about:srcdoc (sandbox iframes)
- Update all runtime providers to use __isExtensionContext() instead:
  - FileDownloadRuntimeProvider: Correctly falls back to browser download
  - ConsoleRuntimeProvider: Only sends messages in extension context
  - ArtifactsRuntimeProvider: Properly detects offline/read-only mode

This fixes the issue where downloaded HTML artifacts incorrectly try to
communicate with the extension when opened from disk.
This commit is contained in:
Mario Zechner 2025-10-11 18:39:45 +02:00
parent 46c1da9826
commit b5648eaabd
4 changed files with 23 additions and 9 deletions

View file

@ -43,7 +43,7 @@ export class ArtifactsRuntimeProvider implements SandboxRuntimeProvider {
(window as any).listArtifacts = async (): Promise<string[]> => {
// Online: ask extension
if ((window as any).sendRuntimeMessage) {
if ((window as any).__isExtensionContext?.()) {
const response = await (window as any).sendRuntimeMessage({
type: "artifact-operation",
action: "list",
@ -61,7 +61,7 @@ export class ArtifactsRuntimeProvider implements SandboxRuntimeProvider {
let content: string;
// Online: ask extension
if ((window as any).sendRuntimeMessage) {
if ((window as any).__isExtensionContext?.()) {
const response = await (window as any).sendRuntimeMessage({
type: "artifact-operation",
action: "get",
@ -94,7 +94,7 @@ export class ArtifactsRuntimeProvider implements SandboxRuntimeProvider {
content: any,
mimeType?: string,
): Promise<void> => {
if (!(window as any).sendRuntimeMessage) {
if (!(window as any).__isExtensionContext?.()) {
throw new Error("Cannot create/update artifacts in offline mode (read-only)");
}
@ -117,7 +117,7 @@ export class ArtifactsRuntimeProvider implements SandboxRuntimeProvider {
};
(window as any).deleteArtifact = async (filename: string): Promise<void> => {
if (!(window as any).sendRuntimeMessage) {
if (!(window as any).__isExtensionContext?.()) {
throw new Error("Cannot delete artifacts in offline mode (read-only)");
}