diff --git a/docs/bundle.js b/docs/bundle.js index c44bc871f..009dec509 100644 --- a/docs/bundle.js +++ b/docs/bundle.js @@ -1,11 +1,20 @@ -var s = document.querySelectorAll("pre"); -s.forEach(element => { - var button = document.createElement("button"); +var pres = document.querySelectorAll("pre"); +pres.forEach(element => { + const button = document.createElement("button"); button.innerHTML = "Copy to clipboard"; element.parentElement.appendChild(button); button.addEventListener ("click", function() { - var text = element.textContent.replace(/\/\*(.|[\r\n])*?\*\//g, '').replace(/\/\/.*/gm, ''); - console.log(text); + //The following regex removes all the comments from the snippet + const text = element.textContent.replace(/\/\*(.|[\r\n])*?\*\//g, '').replace(/\/\/.*/gm, ''); + // Apparently you can copy variable to clipboard so you need to create text input element, + // give it a value, copy it and then remove it + const textArea = document.createElement("textarea"); + textArea.value = text; + document.body.appendChild(textArea); + textArea.select(); + document.execCommand("Copy"); + document.body.removeChild(textArea); + console.log(`copied: ${text}`); }); }); \ No newline at end of file diff --git a/static-parts/index-start.html b/static-parts/index-start.html index d1e09b45c..b81e02d7a 100644 --- a/static-parts/index-start.html +++ b/static-parts/index-start.html @@ -22,6 +22,26 @@ var remove = false, childs = Array.from(node.parentElement.parentElement.children), toRemove = childs[0]; Array.from(node.parentElement.parentElement.children).forEach(x => x.tagName == 'H3' ? (toRemove.style.display = (remove ? 'none' : ''), toRemove = x, remove = true) : (x.style.display == '' ? remove = false : remove=remove)); } + + const pres = document.querySelectorAll("pre"); + pres.forEach(element => { + const button = document.createElement("button"); + button.innerHTML = "Copy to clipboard"; + element.parentElement.appendChild(button); + + button.addEventListener ("click", function() { + //The following regex removes all the comments from the snippet + const text = element.textContent.replace(/\/\*(.|[\r\n])*?\*\//g, '').replace(/\/\/.*/gm, ''); + // Apparently you can copy variable to clipboard so you need to create text input element, + // give it a value, copy it and then remove it + const textArea = document.createElement("textarea"); + textArea.value = text; + document.body.appendChild(textArea); + textArea.select(); + document.execCommand("Copy"); + document.body.removeChild(textArea); + }); +});