This repository has been archived on 2024-05-11. You can view files and clone it, but cannot push or open issues or pull requests.
wiki/themes/hugo-book/assets/clipboard.js

22 lines
520 B
JavaScript
Raw Normal View History

2022-03-31 16:29:00 +00:00
(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);
}
});
});
})();