Skip to content

Open source CapCut alternative.

TypeScript

feat(chatgpt): the card opens links and saves files for the editor it frames

David Le852a5f2ab45e
diff --git a/site/src/clients/chatgpt/ui/PreviewApp.tsx b/site/src/clients/chatgpt/ui/PreviewApp.tsx
index 3f9718d0..5f047f40 100644
--- a/site/src/clients/chatgpt/ui/PreviewApp.tsx
+++ b/site/src/clients/chatgpt/ui/PreviewApp.tsx
@@ -4,7 +4,7 @@ import { useProjectApp } from "./useProjectApp";
import "./preview.css";
export function PreviewApp() {
- const { ready, view, playback, download, editor, fullscreen, hostInset, error, busy, visible, run, playbackFailed, openDonkey, openDownload, requestFullscreen } = useProjectApp();
+ const { ready, view, playback, download, editor, fullscreen, hostInset, error, busy, visible, run, playbackFailed, openDonkey, openDownload, openFromFrame, saveFromFrame, requestFullscreen } = useProjectApp();
const project = view?.project;
const preview = view?.preview;
const rendering = preview?.status === "queued" || preview?.status === "running";
@@ -13,7 +13,7 @@ export function PreviewApp() {
// The editor is the card: its own chrome carries every control. Inline, one
// control asks the host for the whole window.
if (editor && project) return <main className="editing" data-mode={fullscreen ? "fullscreen" : "inline"}>
- <EditorFrame url={editor.url} name={project.name} inset={hostInset} fullscreen={fullscreen} onFullscreen={requestFullscreen} />
+ <EditorFrame url={editor.url} name={project.name} inset={hostInset} fullscreen={fullscreen} onFullscreen={requestFullscreen} onOpen={openFromFrame} onSave={saveFromFrame} />
</main>;
return <main aria-busy={busy}>
<header><span className="brand">Donkey Cut</span><span>Cloud projects</span></header>
@@ -54,8 +54,9 @@ export function PreviewApp() {
/** The editor, framed. It learns the host's display mode and how tall the
* bottom overlay is when it loads, whenever that changes, and whenever it
- * asks; its own toolbar asks for fullscreen. */
-function EditorFrame({ url, name, inset, fullscreen, onFullscreen }: { url: string; name: string; inset: number; fullscreen: boolean; onFullscreen: () => void }) {
+ * asks; its own toolbar asks for fullscreen, for a tab on donkeycut.com, and
+ * for the saves the sandbox blocks inside the frame. */
+function EditorFrame({ url, name, inset, fullscreen, onFullscreen, onOpen, onSave }: { url: string; name: string; inset: number; fullscreen: boolean; onFullscreen: () => void; onOpen: (url: string) => void; onSave: (text: string, name: string, mimeType: string) => void }) {
const frame = useRef<HTMLIFrameElement>(null);
const origin = new URL(url).origin;
const tell = () => frame.current?.contentWindow?.postMessage({ type: "donkeycut:host", insetBottom: inset, displayMode: fullscreen ? "fullscreen" : "inline" }, origin);
@@ -65,6 +66,17 @@ function EditorFrame({ url, name, inset, fullscreen, onFullscreen }: { url: stri
if (event.origin !== origin || event.source !== frame.current?.contentWindow) return;
if (event.data?.type === "donkeycut:host?") tell();
if (event.data?.type === "donkeycut:fullscreen") onFullscreen();
+ // The frame is the editor on this origin, so what it asks to open is
+ // the editor's own link — a project page, an export, the post a clip
+ // came from. Only the scheme is worth checking.
+ if (event.data?.type === "donkeycut:open" && typeof event.data.url === "string") {
+ const target = URL.parse(event.data.url);
+ if (target?.protocol === "https:" || target?.protocol === "http:") onOpen(target.toString());
+ }
+ if (event.data?.type === "donkeycut:save" && typeof event.data.text === "string") {
+ const { text, name: file, mimeType } = event.data as { text: string; name?: unknown; mimeType?: unknown };
+ if (typeof file === "string" && typeof mimeType === "string") onSave(text, file, mimeType);
+ }
};
window.addEventListener("message", onMessage);
return () => window.removeEventListener("message", onMessage);
diff --git a/site/src/clients/chatgpt/ui/useProjectApp.ts b/site/src/clients/chatgpt/ui/useProjectApp.ts
index 0a53adfd..4e4ec992 100644
--- a/site/src/clients/chatgpt/ui/useProjectApp.ts
+++ b/site/src/clients/chatgpt/ui/useProjectApp.ts
@@ -135,6 +135,23 @@ export function useProjectApp() {
};
const openDonkey = () => { if (view?.project) void openLink(view.project.url, "Could not open Donkey Cut."); };
const openDownload = () => { if (download) void openLink(download.url, "Could not open the download."); };
+ /** The framed editor asking for a tab: opening the project on donkeycut.com,
+ * and saving an export, which the card's sandbox blocks in the frame. */
+ const openFromFrame = useCallback((url: string) => {
+ void openLink(url, "Could not open Donkey Cut.");
+ }, []);
+ /** A captions file made inside the frame: its bytes go with the request, so
+ * the host needs nothing from this origin to save it. */
+ const saveFromFrame = useCallback(async (text: string, name: string, mimeType: string) => {
+ const app = appRef.current;
+ if (!app) return;
+ try {
+ const result = await app.downloadFile({
+ contents: [{ type: "resource", resource: { uri: `file:///${name}`, mimeType, text } }],
+ });
+ if (result.isError) setError(`Could not save ${name}.`);
+ } catch { setError(`Could not save ${name}.`); }
+ }, []);
useEffect(() => {
if (!ready || !editorRequest || editor || requested.current === editorRequest) return;
@@ -154,5 +171,5 @@ export function useProjectApp() {
} catch { /* the inline card still works */ }
}, []);
useEffect(() => { if (editor) void requestFullscreen(); }, [editor, requestFullscreen]);
- return { ready, view, playback, download, editor, fullscreen, hostInset, error, busy, visible, run, playbackFailed, openDonkey, openDownload, requestFullscreen };
+ return { ready, view, playback, download, editor, fullscreen, hostInset, error, busy, visible, run, playbackFailed, openDonkey, openDownload, openFromFrame, saveFromFrame, requestFullscreen };
}
diff --git a/site/src/cut/components/LibraryCard.tsx b/site/src/cut/components/LibraryCard.tsx
index 5519089f..07f91e90 100644
--- a/site/src/cut/components/LibraryCard.tsx
+++ b/site/src/cut/components/LibraryCard.tsx
@@ -14,6 +14,7 @@ import { useMediaFileSize } from "@/cut/hooks/useMediaFileSize";
import { MEDIA_CORS } from "@/cut/lib/mediaCors";
import { setLibraryDragData } from "@/cut/lib/assetDrag";
import { refFromLibrary } from "@/cut/lib/assetRef";
+import { openExternal } from "@/cut/lib/hostBridge";
import { fileKind } from "@/cut/lib/media";
import { libraryMediaUrl, libraryPosterUrl, downloadLibraryAsset, type LibraryAsset } from "@/cut/lib/library";
import { lightboxItemFromLibrary, type LightboxItem } from "@/cut/lib/lightbox";
@@ -393,11 +394,7 @@ export function LibraryCard({
{a.source?.url && (
// An imported clip keeps the link it came from, so the post it was
// cut out of is one click away.
- <DropdownMenuItem
- onClick={() =>
- window.open(a.source!.url, "_blank", "noopener,noreferrer")
- }
- >
+ <DropdownMenuItem onClick={() => openExternal(a.source!.url)}>
<ExternalLink /> Open original
</DropdownMenuItem>
)}
diff --git a/site/src/cut/components/RequireSession.tsx b/site/src/cut/components/RequireSession.tsx
index d7f27c4d..582b7910 100644
--- a/site/src/cut/components/RequireSession.tsx
+++ b/site/src/cut/components/RequireSession.tsx
@@ -3,6 +3,7 @@ import { bindChatRuntime } from "@/cut/lib/chatRuntime";
import { useEffect, type ReactNode } from "react";
import { useEnvironment } from "@/cut/lib/environment";
+import { openExternal, projectPageUrl } from "@/cut/lib/hostBridge";
import { authHrefFor } from "@/app/_components/landing/useAppEntryHref";
import {
@@ -79,14 +80,13 @@ function EmbeddedSignInFallback() {
<p className="text-sm text-muted-foreground">
This browser keeps the editor signed out inside ChatGPT.
</p>
- <a
+ <button
+ type="button"
className="inline-block rounded-md border px-3 py-2 text-sm"
- href={window.location.pathname}
- target="_blank"
- rel="noopener"
+ onClick={() => openExternal(projectPageUrl())}
>
Open in Donkey Cut
- </a>
+ </button>
</div>
</div>
);
diff --git a/site/src/cut/lib/download.ts b/site/src/cut/lib/download.ts
index d207a44e..665ebb30 100644
--- a/site/src/cut/lib/download.ts
+++ b/site/src/cut/lib/download.ts
@@ -1,5 +1,7 @@
"use client";
+import { openThroughHost, saveThroughHost } from "./hostBridge";
+
/**
* Save a served media file to the user's Downloads folder.
*
@@ -13,34 +15,25 @@ export function downloadFromUrl(url: string, name: string) {
downloadFile(`${url}${url.includes("?") ? "&" : "?"}download=1`, name);
}
-/** Save a URL that already answers as an attachment. */
+/**
+ * Save a URL that already answers as an attachment. Framed inside the ChatGPT
+ * card the page sits under a sandbox without `allow-downloads`, which blocks
+ * every download a frame starts — so the host opens the link in a tab of its
+ * own and the browser saves it there.
+ */
export function downloadFile(href: string, name: string) {
- const scope = downloadScope();
- clickAnchor(scope.window, href, name);
- scope.done();
+ if (openThroughHost(href)) return;
+ clickAnchor(window, href, name);
}
/** Save text made in this tab — a captions file — to the user's Downloads
- * folder. The blob is same-origin, so the anchor's own name is honored. */
+ * folder. The blob is same-origin, so the anchor's own name is honored; inside
+ * the card the text goes to the host, which has no origin to be blocked by. */
export function downloadText(text: string, name: string, type = "text/plain") {
- const scope = downloadScope();
- const url = scope.window.URL.createObjectURL(new scope.window.Blob([text], { type }));
- clickAnchor(scope.window, url, name);
- setTimeout(() => scope.window.URL.revokeObjectURL(url), 10_000);
- scope.done();
-}
-
-/**
- * Framed inside the ChatGPT card, the page sits under a sandbox without
- * `allow-downloads`, which blocks every download a frame starts, nested
- * frames included. A popup escapes that sandbox and, opened blank, inherits
- * this origin, so the anchor is clicked in there and the popup closes once
- * the download has started.
- */
-function downloadScope(): { window: Window & typeof globalThis; done: () => void } {
- const popup = window.self !== window.top ? window.open("", "_blank") : null;
- if (!popup) return { window, done: () => {} };
- return { window: popup as Window & typeof globalThis, done: () => setTimeout(() => popup.close(), 1000) };
+ if (saveThroughHost(text, name, type)) return;
+ const url = window.URL.createObjectURL(new Blob([text], { type }));
+ clickAnchor(window, url, name);
+ setTimeout(() => window.URL.revokeObjectURL(url), 10_000);
}
function clickAnchor(win: Window, href: string, name: string) {
diff --git a/site/src/cut/lib/hostBridge.ts b/site/src/cut/lib/hostBridge.ts
index 28e0d597..6e990338 100644
--- a/site/src/cut/lib/hostBridge.ts
+++ b/site/src/cut/lib/hostBridge.ts
@@ -10,6 +10,11 @@ import { editorHost } from "@/cut/lib/environment";
* last rows above that band through `--host-inset-bottom`, and asks the card
* for fullscreen from its own toolbar. Outside the card nothing is sent and
* the variable stays unset.
+ *
+ * The frame also asks the card for the two things a sandboxed frame cannot do
+ * itself: open a page in a tab of its own, and save a file. ChatGPT sandboxes
+ * the card without `allow-downloads`, and a popup opened from inside inherits
+ * that sandbox, so the host has to carry both.
*/
export type HostDisplayMode = "inline" | "fullscreen";
@@ -28,6 +33,40 @@ export function requestHostFullscreen() {
window.parent.postMessage({ type: "donkeycut:fullscreen" }, "*");
}
+/**
+ * Open a page in a tab the host owns. Answers whether the host took it, so a
+ * caller on the web falls through to its own navigation.
+ */
+export function openThroughHost(url: string): boolean {
+ if (editorHost() !== "chatgpt") return false;
+ window.parent.postMessage(
+ { type: "donkeycut:open", url: new URL(url, window.location.href).toString() },
+ "*"
+ );
+ return true;
+}
+
+/** Open a page in a new tab: through the host inside the card, where the
+ * sandbox blocks a popup, and straight from the page everywhere else. */
+export function openExternal(url: string) {
+ if (openThroughHost(url)) return;
+ window.open(url, "_blank", "noopener,noreferrer");
+}
+
+/** Save text made in the frame — a captions file — through the host. */
+export function saveThroughHost(text: string, name: string, mimeType: string): boolean {
+ if (editorHost() !== "chatgpt") return false;
+ window.parent.postMessage({ type: "donkeycut:save", text, name, mimeType }, "*");
+ return true;
+}
+
+/** The project on donkeycut.com: this page without the frame's embed flag. */
+export function projectPageUrl(): string {
+ const url = new URL(window.location.href);
+ url.searchParams.delete("embed");
+ return url.toString();
+}
+
export function useHostBridge() {
useEffect(() => {
if (editorHost() !== "chatgpt") return;