Skip to content
Snippets Groups Projects
Select Git revision
  • 16f6203f909ad5da52664b9f0fcba03c77ec69c2
  • master default protected
2 results

clipboard.js

Blame
  • user avatar
    Alexander Danilov authored and GitHub committed
    c0245386
    History
    clipboard.js 590 B
    (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) {
          if (window.getSelection().toString()) {
            return;
          }
          select(code.parentElement);
    
          if (navigator.clipboard) {
            navigator.clipboard.writeText(code.parentElement.textContent);
          }
        });
      });
    })();