diff options
Diffstat (limited to '')
| -rw-r--r-- | themes/hugo-book/assets/clipboard.js | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/themes/hugo-book/assets/clipboard.js b/themes/hugo-book/assets/clipboard.js new file mode 100644 index 0000000..2799f2f --- /dev/null +++ b/themes/hugo-book/assets/clipboard.js @@ -0,0 +1,24 @@ +(function () { + function select(element) { + const selection = window.getSelection(); + + const range = document.createRange(); + range.selectNodeContents(element); + + selection.removeAllRanges(); + selection.addRange(range); + } + + document.querySelectorAll("pre code").forEach(code => { + code.addEventListener("click", function (event) { + if (window.getSelection().toString()) { + return; + } + select(code.parentElement); + + if (navigator.clipboard) { + navigator.clipboard.writeText(code.parentElement.textContent); + } + }); + }); +})(); |
