From 96356946d0253f57ce268b3a14a66c81d279fdcf Mon Sep 17 00:00:00 2001 From: fruworg Date: Tue, 1 Aug 2023 22:23:53 +0600 Subject: [PATCH] copy --- static/js/copy.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/static/js/copy.js b/static/js/copy.js index e69de29..673ac7a 100644 --- a/static/js/copy.js +++ b/static/js/copy.js @@ -0,0 +1,22 @@ +document.addEventListener('DOMContentLoaded', function() { + const codeBlocks = document.querySelectorAll('.highlight pre code[class*="language-"]::before'); + + codeBlocks.forEach(codeBlock => { + codeBlock.addEventListener('click', function() { + const textToCopy = this.nextElementSibling.innerText; + copyTextToClipboard(textToCopy); + alert('Текст скопирован в буфер обмена: ' + textToCopy); + }); + }); + + function copyTextToClipboard(text) { + const tempInput = document.createElement('textarea'); + tempInput.style.position = 'absolute'; + tempInput.style.left = '-9999px'; + tempInput.value = text; + document.body.appendChild(tempInput); + tempInput.select(); + document.execCommand('copy'); + document.body.removeChild(tempInput); + } +}); \ No newline at end of file