Skip to content

Commit 64e09d0

Browse files
Fix Copy button fails in Safari (#181)
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
1 parent 4a99ac1 commit 64e09d0

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

plugin/processor.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -436,21 +436,22 @@ def process_html(
436436
let rawUrl = editBtn.href.replace('github.com', 'raw.githubusercontent.com');
437437
rawUrl = rawUrl.replace('/blob/', '/').replace('/tree/', '/');
438438
439-
try {{
439+
async function getContent() {{
440440
const response = await fetch(rawUrl);
441441
let markdown = await response.text();
442-
443442
if (markdown.startsWith('---')) {{
444443
const frontMatterEnd = markdown.indexOf('\\n---\\n', 3);
445-
if (frontMatterEnd !== -1) {{
446-
markdown = markdown.substring(frontMatterEnd + 5).trim();
447-
}}
444+
if (frontMatterEnd !== -1) markdown = markdown.substring(frontMatterEnd + 5).trim();
448445
}}
449-
450446
const title = document.querySelector('h1')?.textContent || document.title;
451-
const content = `# ${{title}}\\n\\nSource: ${{window.location.href}}\\n\\n---\\n\\n${{markdown}}`;
447+
return `# ${{title}}\\n\\nSource: ${{window.location.href}}\\n\\n---\\n\\n${{markdown}}`;
448+
}}
452449
453-
await navigator.clipboard.writeText(content);
450+
try {{
451+
const clipboardItem = new ClipboardItem({{
452+
'text/plain': getContent().then(text => new Blob([text], {{ type: 'text/plain' }}))
453+
}});
454+
await navigator.clipboard.write([clipboardItem]);
454455
button.innerHTML = checkIcon + ' Copied!';
455456
setTimeout(() => {{ button.innerHTML = originalHTML; }}, 2000);
456457
}} catch (err) {{

0 commit comments

Comments
 (0)