diff options
author | Alex Shpak <alex-shpak@users.noreply.github.com> | 2021-05-03 23:52:00 +0200 |
---|---|---|
committer | Alex Shpak <alex-shpak@users.noreply.github.com> | 2021-05-03 23:52:00 +0200 |
commit | 6720168dd507dfaf58629718ef6bc7b7073699b2 (patch) | |
tree | 63a1ecc38140c0d5ce02cb1f507d8da9fffc66c1 /assets/clipboard.js | |
parent | 6a1932611cb183f913c4b827cffa2b0b11dbdc4f (diff) |
#281, Copy code block content on click
Diffstat (limited to 'assets/clipboard.js')
-rw-r--r-- | assets/clipboard.js | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/assets/clipboard.js b/assets/clipboard.js new file mode 100644 index 0000000..fdf84cf --- /dev/null +++ b/assets/clipboard.js @@ -0,0 +1,21 @@ +(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) { + select(code.parentElement); + + if (navigator.clipboard) { + navigator.clipboard.writeText(code.parentElement.textContent); + } + }); + }); +})(); |