mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-21 02:04:32 +00:00
Fix up context check
This commit is contained in:
parent
8a96741441
commit
63bfe95c18
5 changed files with 19 additions and 29 deletions
|
|
@ -559,14 +559,18 @@ ${runtimeFunctions.join("\n")}
|
||||||
}
|
}
|
||||||
}, true);
|
}, true);
|
||||||
|
|
||||||
// Prevent window.location changes
|
// Prevent window.location changes (only if not already redefined)
|
||||||
const originalLocation = window.location;
|
try {
|
||||||
Object.defineProperty(window, 'location', {
|
const originalLocation = window.location;
|
||||||
get: function() { return originalLocation; },
|
Object.defineProperty(window, 'location', {
|
||||||
set: function(url) {
|
get: function() { return originalLocation; },
|
||||||
window.parent.postMessage({ type: 'open-external-url', url: url.toString() }, '*');
|
set: function(url) {
|
||||||
}
|
window.parent.postMessage({ type: 'open-external-url', url: url.toString() }, '*');
|
||||||
});
|
}
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
// Already defined, skip
|
||||||
|
}
|
||||||
})();
|
})();
|
||||||
</script>`;
|
</script>`;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,7 @@ export class ArtifactsRuntimeProvider implements SandboxRuntimeProvider {
|
||||||
|
|
||||||
(window as any).listArtifacts = async (): Promise<string[]> => {
|
(window as any).listArtifacts = async (): Promise<string[]> => {
|
||||||
// Online: ask extension
|
// Online: ask extension
|
||||||
if ((window as any).__isExtensionContext?.()) {
|
if ((window as any).sendRuntimeMessage) {
|
||||||
const response = await (window as any).sendRuntimeMessage({
|
const response = await (window as any).sendRuntimeMessage({
|
||||||
type: "artifact-operation",
|
type: "artifact-operation",
|
||||||
action: "list",
|
action: "list",
|
||||||
|
|
@ -61,7 +61,7 @@ export class ArtifactsRuntimeProvider implements SandboxRuntimeProvider {
|
||||||
let content: string;
|
let content: string;
|
||||||
|
|
||||||
// Online: ask extension
|
// Online: ask extension
|
||||||
if ((window as any).__isExtensionContext?.()) {
|
if ((window as any).sendRuntimeMessage) {
|
||||||
const response = await (window as any).sendRuntimeMessage({
|
const response = await (window as any).sendRuntimeMessage({
|
||||||
type: "artifact-operation",
|
type: "artifact-operation",
|
||||||
action: "get",
|
action: "get",
|
||||||
|
|
@ -94,7 +94,7 @@ export class ArtifactsRuntimeProvider implements SandboxRuntimeProvider {
|
||||||
content: any,
|
content: any,
|
||||||
mimeType?: string,
|
mimeType?: string,
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
if (!(window as any).__isExtensionContext?.()) {
|
if (!(window as any).sendRuntimeMessage) {
|
||||||
throw new Error("Cannot create/update artifacts in offline mode (read-only)");
|
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> => {
|
(window as any).deleteArtifact = async (filename: string): Promise<void> => {
|
||||||
if (!(window as any).__isExtensionContext?.()) {
|
if (!(window as any).sendRuntimeMessage) {
|
||||||
throw new Error("Cannot delete artifacts in offline mode (read-only)");
|
throw new Error("Cannot delete artifacts in offline mode (read-only)");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -52,7 +52,7 @@ export class ConsoleRuntimeProvider implements SandboxRuntimeProvider {
|
||||||
(originalConsole as any)[method].apply(console, args);
|
(originalConsole as any)[method].apply(console, args);
|
||||||
|
|
||||||
// Send immediately and track the promise (only in extension context)
|
// Send immediately and track the promise (only in extension context)
|
||||||
if ((window as any).__isExtensionContext?.()) {
|
if ((window as any).sendRuntimeMessage) {
|
||||||
const sendPromise = (window as any)
|
const sendPromise = (window as any)
|
||||||
.sendRuntimeMessage({
|
.sendRuntimeMessage({
|
||||||
type: "console",
|
type: "console",
|
||||||
|
|
@ -108,7 +108,7 @@ export class ConsoleRuntimeProvider implements SandboxRuntimeProvider {
|
||||||
|
|
||||||
const finalError = error || lastError;
|
const finalError = error || lastError;
|
||||||
|
|
||||||
if ((window as any).__isExtensionContext?.()) {
|
if ((window as any).sendRuntimeMessage) {
|
||||||
if (finalError) {
|
if (finalError) {
|
||||||
await (window as any).sendRuntimeMessage({
|
await (window as any).sendRuntimeMessage({
|
||||||
type: "execution-error",
|
type: "execution-error",
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,7 @@ export class FileDownloadRuntimeProvider implements SandboxRuntimeProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send to extension if in extension context (online mode)
|
// Send to extension if in extension context (online mode)
|
||||||
if ((window as any).__isExtensionContext?.()) {
|
if ((window as any).sendRuntimeMessage) {
|
||||||
const response = await (window as any).sendRuntimeMessage({
|
const response = await (window as any).sendRuntimeMessage({
|
||||||
type: "file-returned",
|
type: "file-returned",
|
||||||
fileName,
|
fileName,
|
||||||
|
|
|
||||||
|
|
@ -28,13 +28,6 @@ export class RuntimeMessageBridge {
|
||||||
// Returns stringified function that uses window.parent.postMessage
|
// Returns stringified function that uses window.parent.postMessage
|
||||||
return `
|
return `
|
||||||
window.__completionCallbacks = [];
|
window.__completionCallbacks = [];
|
||||||
// Check if we're in an extension context by examining the URL
|
|
||||||
window.__isExtensionContext = () => {
|
|
||||||
const url = window.location.href;
|
|
||||||
return url.startsWith('chrome-extension://') ||
|
|
||||||
url.startsWith('moz-extension://') ||
|
|
||||||
url === 'about:srcdoc';
|
|
||||||
};
|
|
||||||
window.sendRuntimeMessage = async (message) => {
|
window.sendRuntimeMessage = async (message) => {
|
||||||
const messageId = 'msg_' + Date.now() + '_' + Math.random().toString(36).substring(2, 9);
|
const messageId = 'msg_' + Date.now() + '_' + Math.random().toString(36).substring(2, 9);
|
||||||
|
|
||||||
|
|
@ -75,13 +68,6 @@ window.onCompleted = (callback) => {
|
||||||
// Returns stringified function that uses chrome.runtime.sendMessage
|
// Returns stringified function that uses chrome.runtime.sendMessage
|
||||||
return `
|
return `
|
||||||
window.__completionCallbacks = [];
|
window.__completionCallbacks = [];
|
||||||
// Check if we're in an extension context by examining the URL
|
|
||||||
window.__isExtensionContext = () => {
|
|
||||||
const url = window.location.href;
|
|
||||||
return url.startsWith('chrome-extension://') ||
|
|
||||||
url.startsWith('moz-extension://') ||
|
|
||||||
url === 'about:srcdoc';
|
|
||||||
};
|
|
||||||
window.sendRuntimeMessage = async (message) => {
|
window.sendRuntimeMessage = async (message) => {
|
||||||
return await chrome.runtime.sendMessage({
|
return await chrome.runtime.sendMessage({
|
||||||
...message,
|
...message,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue