Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
62 lines (55 loc) · 1.62 KB
/
Copy pathscript.js
File metadata and controls
62 lines (55 loc) · 1.62 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
59
60
61
62
const copyButton = document.getElementById("copy-bibtex");
const bibtex = document.getElementById("bibtex");
const copyStatus = document.getElementById("copy-status");
function setCopyFeedback(buttonText, statusText) {
if (copyButton) {
copyButton.textContent = buttonText;
}
if (copyStatus) {
copyStatus.textContent = statusText;
}
}
function legacyCopy(text) {
const textarea = document.createElement("textarea");
textarea.value = text;
textarea.setAttribute("readonly", "");
textarea.style.position = "absolute";
textarea.style.left = "-9999px";
document.body.appendChild(textarea);
textarea.select();
textarea.setSelectionRange(0, textarea.value.length);
let copied = false;
try {
copied = document.execCommand("copy");
} catch (error) {
copied = false;
}
document.body.removeChild(textarea);
return copied;
}
if (copyButton && bibtex) {
copyButton.addEventListener("click", async () => {
const original = copyButton.textContent;
const text = bibtex.innerText.trim();
let copied = false;
if (navigator.clipboard && navigator.clipboard.writeText) {
try {
await navigator.clipboard.writeText(text);
copied = true;
} catch (error) {
copied = false;
}
}
if (!copied) {
copied = legacyCopy(text);
}
if (copied) {
setCopyFeedback("Copied", "BibTeX copied to your clipboard.");
} else {
setCopyFeedback("Use Download BibTeX", "Clipboard access is unavailable here. Use the Download BibTeX button instead.");
}
window.setTimeout(() => {
setCopyFeedback(original, "");
}, 1800);
});
}