From 6720168dd507dfaf58629718ef6bc7b7073699b2 Mon Sep 17 00:00:00 2001 From: Alex Shpak Date: Mon, 3 May 2021 23:52:00 +0200 Subject: #281, Copy code block content on click --- assets/clipboard.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 assets/clipboard.js (limited to 'assets/clipboard.js') 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); + } + }); + }); +})(); -- cgit v1.2.3