From a7c77da8750694c1bcda0a0f061567dc1b7e617d Mon Sep 17 00:00:00 2001 From: Thomas Dehaeze Date: Thu, 26 Nov 2020 19:57:07 +0100 Subject: [PATCH] Correct copy code block text --- js/script.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/js/script.js b/js/script.js index f98f671..827ec8a 100644 --- a/js/script.js +++ b/js/script.js @@ -34,15 +34,24 @@ docReady(function() { boxCopy.textContent = "Copy" boxCopy.onclick = function() { - // Get text inside (remove 4 first char corresponding to "Copy") - let srcText = this.parentNode.textContent.slice(4) + // Create a clone the node to not affect the original one + let nodeClone = this.parentNode.cloneNode(true) + + // Remove Line Numbers and Copy Button + let linenumNodes = nodeClone.querySelectorAll('.linenr,.src-copy') + for (var i = linenumNodes.length - 1 ; i >= 0 ; --i) { + nodeClone.removeChild(linenumNodes[i]) + } + + // Copy the filetered content to the clipboard + let srcText = nodeClone.textContent navigator.clipboard.writeText(srcText).then(function() { boxCopy.textContent = "Copied" boxCopy.classList.add("src-copied") setTimeout(function() { boxCopy.textContent = "Copy" boxCopy.classList.remove("src-copied") - },1000); + }, 2000); }); }