-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode-copy.js
61 lines (61 loc) · 2.3 KB
/
code-copy.js
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
(() => {
// <stdin>
(function() {
const copyIcon = document.getElementById("code-copy").innerHTML;
const copyIconDone = document.getElementById("code-copy-done").innerHTML;
function createCopyButton(highlightDiv) {
const button = document.createElement("button");
button.className = "right-1 top-1 absolute";
button.type = "button";
button.innerHTML = copyIcon;
button.addEventListener("click", () => copyCodeToClipboard(button, highlightDiv));
highlightDiv.insertBefore(button, highlightDiv.firstChild);
}
document.querySelectorAll(".highlight").forEach((highlightDiv) => createCopyButton(highlightDiv));
async function copyCodeToClipboard(button, highlightDiv) {
let codeToCopy = highlightDiv.querySelector("code").textContent;
if (highlightDiv.querySelector("table")) {
codeToCopy = highlightDiv.querySelector("td:last-child code").textContent;
}
let permissionGranted = true;
try {
var result = await navigator.permissions.query({ name: "clipboard-write" });
if (result.state != "granted" && result.state != "prompt") {
permissionGranted = false;
}
} catch (_) {
}
try {
if (permissionGranted) {
await navigator.clipboard.writeText(codeToCopy);
} else {
copyCodeBlockExecCommand(codeToCopy, highlightDiv);
}
} catch (_) {
copyCodeBlockExecCommand(codeToCopy, highlightDiv);
} finally {
button.blur();
button.innerHTML = copyIconDone;
setTimeout(function() {
button.innerHTML = copyIcon;
}, 2e3);
}
}
function copyCodeBlockExecCommand(codeToCopy, highlightDiv) {
const textArea = document.createElement("textArea");
textArea.contentEditable = "true";
textArea.readOnly = "false";
textArea.className = "copyable-text-area";
textArea.value = codeToCopy;
highlightDiv.insertBefore(textArea, highlightDiv.firstChild);
const range = document.createRange();
range.selectNodeContents(textArea);
const sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
textArea.setSelectionRange(0, 999999);
document.execCommand("copy");
highlightDiv.removeChild(textArea);
}
})();
})();