forked from stackingsaunter/codex-ppq
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
52 lines (44 loc) · 1.58 KB
/
script.js
File metadata and controls
52 lines (44 loc) · 1.58 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
const copyButtons = document.querySelectorAll("[data-copy-target]");
copyButtons.forEach((button) => {
const originalLabel = button.textContent;
button.addEventListener("click", async () => {
const target = document.getElementById(button.dataset.copyTarget);
const text = target?.textContent.trim();
if (!text) return;
try {
if (!navigator.clipboard?.writeText) {
throw new Error("Clipboard API unavailable");
}
await navigator.clipboard.writeText(text);
button.textContent = "Copied";
window.setTimeout(() => {
button.textContent = originalLabel;
}, 1800);
} catch {
const textArea = document.createElement("textarea");
textArea.value = text;
textArea.setAttribute("readonly", "");
textArea.style.position = "fixed";
textArea.style.inset = "0 auto auto 0";
textArea.style.width = "1px";
textArea.style.height = "1px";
textArea.style.opacity = "0";
document.body.appendChild(textArea);
textArea.select();
textArea.setSelectionRange(0, textArea.value.length);
const copied = document.execCommand("copy");
document.body.removeChild(textArea);
if (!copied) {
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(target);
selection.removeAllRanges();
selection.addRange(range);
}
button.textContent = copied ? "Copied" : "Prompt selected";
window.setTimeout(() => {
button.textContent = originalLabel;
}, 1800);
}
});
});