Carlos Aguni

Highly motivated self-taught IT analyst. Always learning and ready to explore new skills. An eternal apprentice.


Javascript copyToClipboard

10 Dec 2022 »
const copyToClipboard = (str, b64=false) => {
  if (b64) str = atob(str)
  const el = document.createElement('textarea');
  el.value = str;
  document.body.appendChild(el);
  el.select();
  document.execCommand('copy');
  document.body.removeChild(el);
};

const copyToClipboardRich = (str, b64=false) => {
  if(b64) str = atob(str)
  function listener(e){
    e.clipboardData.setData("text/html ", str)
    e.clipboardData.setData("text/plain ", str)
    e.preventDefault()
  }
  document.addEventListener('copy', listener)
  document.execCommand('copy')
  document.removeEventListener('copy', listener)
};