Skip to content

Commit 40ea071

Browse files
committed
Fix copy button on Safari
1 parent 7c90c6e commit 40ea071

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/components/PageHeading.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,20 @@ function CopyAsMarkdownButton() {
4141
return () => clearTimeout(timer);
4242
}, [copied]);
4343

44-
async function handleCopy() {
44+
async function fetchPageBlob() {
4545
const cleanPath = asPath.split(/[?#]/)[0];
46+
const res = await fetch(cleanPath + '.md');
47+
if (!res.ok) throw new Error('Failed to fetch');
48+
const text = await res.text();
49+
return new Blob([text], {type: 'text/plain'});
50+
}
51+
52+
async function handleCopy() {
4653
try {
47-
const res = await fetch(cleanPath + '.md');
48-
if (!res.ok) return;
49-
const text = await res.text();
50-
await navigator.clipboard.writeText(text);
54+
await navigator.clipboard.write([
55+
// Don't wait for the blob, or Safari will refuse clipboard access
56+
new ClipboardItem({'text/plain': fetchPageBlob()}),
57+
]);
5158
setCopied(true);
5259
} catch {
5360
// Silently fail

0 commit comments

Comments
 (0)