clipboard.js
1 (function () { 2 function select(element) { 3 const selection = window.getSelection(); 4 5 const range = document.createRange(); 6 range.selectNodeContents(element); 7 8 selection.removeAllRanges(); 9 selection.addRange(range); 10 } 11 12 document.querySelectorAll("pre code").forEach(code => { 13 code.addEventListener("click", function (event) { 14 if (window.getSelection().toString()) { 15 return; 16 } 17 select(code.parentElement); 18 19 if (navigator.clipboard) { 20 navigator.clipboard.writeText(code.parentElement.textContent); 21 } 22 }); 23 }); 24 })();