Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. // copy to clipboard
  2. const d = document;
  3. const tempCopyInput = d.createElement('input');
  4. d.body.appendChild(tempCopyInput);
  5. tempCopyInput.style.position = 'fixed';
  6. tempCopyInput.style.top = '-1000px';
  7. // copy to clipboard function
  8. const copyToCB = () => {
  9. tempCopyInput.select();
  10. d.execCommand('copy');
  11. };
  12.  
  13. // Hugo outputs code `div` with `highlight` class name.
  14. // copy from highlight container
  15. let highlight = d.querySelectorAll('.highlight'),// or any div containing code stuff
  16. copyIcon = d.createElement('button');
  17. copyIcon.innerHTML = 'copy to clipboard';
  18.  
  19. highlight.forEach((hl) => {
  20. hl.appendChild(copyIcon.cloneNode([true]));
  21. hl.children[1].addEventListener('click',() => {
  22. let codeText = hl.childNodes[0].textContent;
  23. tempCopyInput.setAttribute('value', codeText);
  24. copyToCB();
  25. })
  26.  
  27. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement