Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/scripts/modules/clipboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// @ts-check

/**
* Copies text to the clipboard, reporting whether it worked.
*
* navigator.clipboard needs a secure context, so this falls back to the
* deprecated execCommand path for HTTP and older browsers.
*
* @param {string} text
* @returns {Promise<boolean>}
*/
async function writeToClipboard(text) {
if (
typeof navigator !== 'undefined' &&
navigator.clipboard &&
typeof navigator.clipboard.writeText === 'function' &&
(typeof window === 'undefined' || window.isSecureContext !== false)
) {
try {
await navigator.clipboard.writeText(text);
return true;
} catch (err) {
console.warn('[clipboard] navigator.clipboard failed, falling back', err);
}
}

return execCommandCopyFallback(text);
}

/**
* @param {string} text
* @returns {boolean}
*/
function execCommandCopyFallback(text) {
if (typeof document === 'undefined') return false;
const ta = document.createElement('textarea');
ta.value = text;
ta.setAttribute('readonly', '');
ta.style.position = 'fixed';
ta.style.top = '0';
ta.style.left = '0';
ta.style.opacity = '0';
ta.style.pointerEvents = 'none';
document.body.appendChild(ta);
ta.select();
let ok = false;
try {
ok = document.execCommand('copy');
} catch (err) {
console.warn('[clipboard] execCommand fallback threw', err);
ok = false;
}
document.body.removeChild(ta);
return ok;
}

export { writeToClipboard };
46 changes: 2 additions & 44 deletions src/scripts/modules/copy-markdown.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @ts-check
import { qs, qsa } from './query.js';
import { writeToClipboard } from './clipboard.js';

class CopyMarkdown {
constructor(menu) {
Expand Down Expand Up @@ -29,49 +30,6 @@ class CopyMarkdown {
}
}

// navigator.clipboard requires a secure context; falls back to
// execCommand for HTTP and older browsers.
async writeToClipboard(text) {
if (
typeof navigator !== 'undefined' &&
navigator.clipboard &&
typeof navigator.clipboard.writeText === 'function' &&
(typeof window === 'undefined' || window.isSecureContext !== false)
) {
try {
await navigator.clipboard.writeText(text);
return true;
} catch (err) {
console.warn('[copy-md] navigator.clipboard failed, falling back', err);
}
}

return this.execCommandCopyFallback(text);
}

execCommandCopyFallback(text) {
if (typeof document === 'undefined') return false;
const ta = document.createElement('textarea');
ta.value = text;
ta.setAttribute('readonly', '');
ta.style.position = 'fixed';
ta.style.top = '0';
ta.style.left = '0';
ta.style.opacity = '0';
ta.style.pointerEvents = 'none';
document.body.appendChild(ta);
ta.select();
let ok = false;
try {
ok = document.execCommand('copy');
} catch (err) {
console.warn('[copy-md] execCommand fallback threw', err);
ok = false;
}
document.body.removeChild(ta);
return ok;
}

async handleCopy(btn) {
const url = btn.dataset.copyMdUrl;
const success = btn.dataset.copyMdSuccess ?? '';
Expand All @@ -81,7 +39,7 @@ class CopyMarkdown {
const res = await fetch(url);
if (!res.ok) throw new Error('HTTP ' + res.status);
const text = await res.text();
const ok = await this.writeToClipboard(text);
const ok = await writeToClipboard(text);
if (!ok) throw new Error('clipboard-write-failed');
this.announce(btn, success);
} catch (err) {
Expand Down