Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

fix(open-knowledge): share-link copy fallback for blocked clipboard (#1845)

miles-kt-inkeep95761beee063
diff --git a/.changeset/share-url-copy-fallback.md b/.changeset/share-url-copy-fallback.md
new file mode 100644
index 000000000..42e642b60
--- /dev/null
+++ b/.changeset/share-url-copy-fallback.md
@@ -0,0 +1,11 @@
+---
+"@inkeep/open-knowledge": patch
+---
+
+Make share-link copying work in embedded previews.
+
+Clicking "Copy share link" inside an embedding host that blocks the modern
+clipboard API (for example the Claude preview pane) now falls back to the
+browser's legacy copy path instead of showing "couldn't copy" — the link lands
+on your clipboard on the first click. When the embedding host blocks both
+paths, the failure message now correctly explains the preview limitation.
diff --git a/packages/app/src/components/PublishToGitHubDialog.tsx b/packages/app/src/components/PublishToGitHubDialog.tsx
index afdf83730..400615419 100644
--- a/packages/app/src/components/PublishToGitHubDialog.tsx
+++ b/packages/app/src/components/PublishToGitHubDialog.tsx
@@ -587,6 +587,7 @@ function PublishSuccessView({
value={shareUrl}
onFocus={(e) => e.currentTarget.select()}
onClick={(e) => e.currentTarget.select()}
+ onContextMenu={(e) => e.currentTarget.select()}
className="font-mono text-xs"
/>
) : (
diff --git a/packages/app/src/components/ShareButton.tsx b/packages/app/src/components/ShareButton.tsx
index 5e29af846..8e4a31643 100644
--- a/packages/app/src/components/ShareButton.tsx
+++ b/packages/app/src/components/ShareButton.tsx
@@ -102,6 +102,7 @@ export function ShareButton({ input, onClickWhenNoRemote }: ShareButtonProps) {
value={clipboardFailedUrl ?? ''}
onFocus={(e) => e.currentTarget.select()}
onClick={(e) => e.currentTarget.select()}
+ onContextMenu={(e) => e.currentTarget.select()}
className="font-mono text-xs"
data-testid="share-button-fallback-url"
aria-label={t`Share URL`}
diff --git a/packages/app/src/lib/share/clipboard-adapter.test.ts b/packages/app/src/lib/share/clipboard-adapter.test.ts
index ffbdb91bc..327865a9c 100644
--- a/packages/app/src/lib/share/clipboard-adapter.test.ts
+++ b/packages/app/src/lib/share/clipboard-adapter.test.ts
@@ -174,3 +174,180 @@ describe('scheduleClipboardWrite — no clipboard API available', () => {
);
});
});
+
+describe('isPermissionsPolicyRefusal — Chromium permissions-policy wording', () => {
+ test('matches "blocked because of a permissions policy" (current Chromium iframe block)', () => {
+ const error = new Error(
+ "Failed to execute 'writeText' on 'Clipboard': The Clipboard API has been blocked because of a permissions policy applied to the current document.",
+ );
+ error.name = 'NotAllowedError';
+ expect(isPermissionsPolicyRefusal(error)).toBe(true);
+ });
+});
+
+describe('scheduleClipboardWrite — document.execCommand("copy") fallback', () => {
+ interface FakeTextArea {
+ value: string;
+ style: Record<string, string>;
+ attributes: Record<string, string>;
+ focused: boolean;
+ selected: boolean;
+ removed: boolean;
+ setAttribute(name: string, value: string): void;
+ focus(): void;
+ select(): void;
+ remove(): void;
+ }
+
+ function makeFakeDocument(opts: { execResult?: boolean; execThrows?: boolean } = {}) {
+ const created: FakeTextArea[] = [];
+ const appended: FakeTextArea[] = [];
+ const execCalls: string[] = [];
+ const doc = {
+ activeElement: null,
+ body: {
+ appendChild(el: FakeTextArea) {
+ appended.push(el);
+ },
+ },
+ createElement(_tag: string): FakeTextArea {
+ const ta: FakeTextArea = {
+ value: '',
+ style: {},
+ attributes: {},
+ focused: false,
+ selected: false,
+ removed: false,
+ setAttribute(name, value) {
+ ta.attributes[name] = value;
+ },
+ focus() {
+ ta.focused = true;
+ },
+ select() {
+ ta.selected = true;
+ },
+ remove() {
+ ta.removed = true;
+ },
+ };
+ created.push(ta);
+ return ta;
+ },
+ execCommand(command: string): boolean {
+ execCalls.push(command);
+ if (opts.execThrows) throw new Error('execCommand exploded');
+ return opts.execResult ?? true;
+ },
+ };
+ return { doc, created, appended, execCalls };
+ }
+
+ function policyRefusal(): Error {
+ const error = new Error(
+ "Failed to execute 'writeText' on 'Clipboard': The Clipboard API has been blocked because of a permissions policy applied to the current document.",
+ );
+ error.name = 'NotAllowedError';
+ return error;
+ }
+
+ let savedDocument: unknown;
+ const docHolder = globalThis as { document?: unknown };
+
+ beforeEach(() => {
+ savedDocument = docHolder.document;
+ });
+
+ afterEach(() => {
+ if (savedDocument === undefined) delete docHolder.document;
+ else docHolder.document = savedDocument;
+ });
+
+ test('writeText rejection falls back to execCommand and resolves', async () => {
+ const { doc, created, execCalls } = makeFakeDocument({ execResult: true });
+ docHolder.document = doc;
+ setGlobals({
+ windowOkDesktop: undefined,
+ navigatorClipboardWriteText: async () => {
+ throw policyRefusal();
+ },
+ });
+
+ await scheduleClipboardWrite('https://openknowledge.ai/d/CCC');
+
+ expect(execCalls).toEqual(['copy']);
+ expect(created).toHaveLength(1);
+ expect(created[0]?.value).toBe('https://openknowledge.ai/d/CCC');
+ expect(created[0]?.selected).toBe(true);
+ expect(created[0]?.removed).toBe(true);
+ });
+
+ test('missing navigator.clipboard falls back to execCommand and resolves', async () => {
+ const { doc, execCalls } = makeFakeDocument({ execResult: true });
+ docHolder.document = doc;
+ setGlobals({
+ windowOkDesktop: undefined,
+ navigatorClipboardWriteText: undefined,
+ });
+
+ await scheduleClipboardWrite('https://openknowledge.ai/d/DDD');
+ expect(execCalls).toEqual(['copy']);
+ });
+
+ test('execCommand returning false rejects with the ORIGINAL writeText error', async () => {
+ const { doc } = makeFakeDocument({ execResult: false });
+ docHolder.document = doc;
+ setGlobals({
+ windowOkDesktop: undefined,
+ navigatorClipboardWriteText: async () => {
+ throw policyRefusal();
+ },
+ });
+
+ await expect(scheduleClipboardWrite('https://openknowledge.ai/d/EEE')).rejects.toThrow(
+ /permissions policy/,
+ );
+ });
+
+ test('execCommand throwing rejects with the original writeText error', async () => {
+ const { doc, created } = makeFakeDocument({ execThrows: true });
+ docHolder.document = doc;
+ setGlobals({
+ windowOkDesktop: undefined,
+ navigatorClipboardWriteText: async () => {
+ throw policyRefusal();
+ },
+ });
+
+ await expect(scheduleClipboardWrite('https://openknowledge.ai/d/FFF')).rejects.toThrow(
+ /permissions policy/,
+ );
+ expect(created[0]?.removed).toBe(true);
+ });
+
+ test('no document available + writeText rejects → original error propagates', async () => {
+ delete docHolder.document;
+ setGlobals({
+ windowOkDesktop: undefined,
+ navigatorClipboardWriteText: async () => {
+ throw policyRefusal();
+ },
+ });
+
+ await expect(scheduleClipboardWrite('https://openknowledge.ai/d/GGG')).rejects.toThrow(
+ /permissions policy/,
+ );
+ });
+
+ test('okDesktop bridge path never touches execCommand', async () => {
+ const { doc, execCalls } = makeFakeDocument({ execResult: true });
+ docHolder.document = doc;
+ setGlobals({
+ windowOkDesktop: { clipboard: { writeText: async () => undefined } },
+ navigatorClipboardWriteText: undefined,
+ });
+
+ await scheduleClipboardWrite('https://openknowledge.ai/d/HHH');
+ expect(execCalls).toEqual([]);
+ });
+});
diff --git a/packages/app/src/lib/share/clipboard-adapter.ts b/packages/app/src/lib/share/clipboard-adapter.ts
index d94310de2..54e00bc98 100644
--- a/packages/app/src/lib/share/clipboard-adapter.ts
+++ b/packages/app/src/lib/share/clipboard-adapter.ts
@@ -13,7 +13,51 @@ interface NavClipboardHost {
export function isPermissionsPolicyRefusal(error: unknown): boolean {
if (!(error instanceof Error)) return false;
if (error.name !== 'NotAllowedError') return false;
- return /permission denied/i.test(error.message);
+ return /permission denied|permissions policy/i.test(error.message);
+}
+
+interface ScratchTextArea {
+ value: string;
+ style: { position: string; opacity: string; pointerEvents: string };
+ setAttribute(name: string, value: string): void;
+ focus(): void;
+ select(): void;
+ remove(): void;
+}
+
+interface DocumentHost {
+ document?: {
+ body?: { appendChild(el: ScratchTextArea): void };
+ createElement(tag: 'textarea'): ScratchTextArea;
+ execCommand(command: 'copy'): boolean;
+ };
+}
+
+function tryExecCommandCopy(text: string): boolean {
+ const doc = (globalThis as DocumentHost).document;
+ if (
+ !doc?.body ||
+ typeof doc.createElement !== 'function' ||
+ typeof doc.execCommand !== 'function'
+ ) {
+ return false;
+ }
+ const scratch = doc.createElement('textarea');
+ scratch.value = text;
+ scratch.style.position = 'fixed';
+ scratch.style.opacity = '0';
+ scratch.style.pointerEvents = 'none';
+ scratch.setAttribute('readonly', '');
+ doc.body.appendChild(scratch);
+ try {
+ scratch.focus();
+ scratch.select();
+ return doc.execCommand('copy');
+ } catch {
+ return false;
+ } finally {
+ scratch.remove();
+ }
}
export async function scheduleClipboardWrite(text: string): Promise<void> {
@@ -25,9 +69,15 @@ export async function scheduleClipboardWrite(text: string): Promise<void> {
const navClipboard = (globalThis as NavClipboardHost).navigator?.clipboard;
if (navClipboard && typeof navClipboard.writeText === 'function') {
- await navClipboard.writeText(text);
- return;
+ try {
+ await navClipboard.writeText(text);
+ return;
+ } catch (error) {
+ if (tryExecCommandCopy(text)) return;
+ throw error;
+ }
}
+ if (tryExecCommandCopy(text)) return;
throw new Error('clipboard API unavailable');
}