-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
58 lines (51 loc) · 1.86 KB
/
Copy pathscript.js
File metadata and controls
58 lines (51 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* Copy-to-clipboard for code blocks.
Progressive enhancement: the button is in the markup but does nothing
without this file, and the block remains selectable either way. */
(() => {
"use strict";
const RESET_MS = 2000;
function textOf(pre) {
// innerText collapses the <span> wrappers and preserves line breaks
return (pre.innerText || pre.textContent || "").replace(/\s+$/, "");
}
async function writeClipboard(text) {
if (navigator.clipboard && window.isSecureContext) {
await navigator.clipboard.writeText(text);
return;
}
// fallback for non-secure contexts (e.g. plain-http previews)
const ta = document.createElement("textarea");
ta.value = text;
ta.setAttribute("readonly", "");
ta.style.cssText = "position:absolute;left:-9999px;top:0";
document.body.appendChild(ta);
ta.select();
const ok = document.execCommand("copy");
document.body.removeChild(ta);
if (!ok) throw new Error("execCommand copy failed");
}
document.querySelectorAll("[data-copy]").forEach((btn) => {
const target = document.querySelector(btn.getAttribute("data-copy"));
if (!target) return;
const status = btn.closest("section")?.querySelector(".code__status");
let timer;
btn.addEventListener("click", async () => {
try {
await writeClipboard(textOf(target));
btn.dataset.state = "copied";
btn.title = "Copied";
if (status) status.textContent = "Copied to clipboard.";
} catch {
btn.title = "Press Ctrl/Cmd+C to copy";
if (status) status.textContent = "Copy failed — select the text and press Ctrl/Cmd+C.";
}
clearTimeout(timer);
timer = setTimeout(() => {
delete btn.dataset.state;
btn.title = "Copy";
if (status) status.textContent = "";
}, RESET_MS);
});
btn.title = "Copy";
});
})();