Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

298
Visualizações
Issue when using event.clipboardData.setData for text/html data type

I added a custom copy listener to change the copied text on clipboard to something else. Now when I copy it as text/html , it doesn't work on following cases:

Copy - paste on word (works correctly) Copy - paste on notepad(Nothing gets pasted at all) Copy - paste on url bar (Nothing gets pasted at all)

document.getElementById(config.editorEl).addEventListener('copy', function(event) {
    const range = window.getSelection().getRangeAt(0);
    const content = range.cloneContents();
    const div = document.createElement('div');
    div.appendChild(content);
    $(div)
    .find('.remove_this_class')
    .contents()
    .unwrap();
    event.clipboardData.setData('text/html', div.innerHTML);
    event.preventDefault();
    });

However, when i replace text/html with text/paste, it works correctly by the html tags are not rendered.

Can anyone help me out to fix this for text/html too ?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I can't justify your question, but see this code, it helped you. Because by copying I can paste the notepad and URL bar from it.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

</head>
<body>
  <div id="test" style="width:500px;height: 200px; border: 1px solid sandybrown;">
      Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
  </div>
  <script>
    document.getElementById("test").addEventListener('copy', function(event) {
    const range   = window.getSelection(); 
    const selRange = range.getRangeAt(0);
    const content = selRange.toString();
    alert(content);
    const div = document.createElement('div');
    div.style.color = "yellow";
    div.appendChild(content);
    $(div).find('.remove_this_class').contents().unwrap();
    event.clipboardData.setData('text/html', div.innerHTML);
    event.preventDefault();
    });
  </script>
</body>
</html>
about 4 years ago · Juan Pablo Isaza Relatório

0

Finally, I was able to guess the solution myself. While we paste we use getData("DataType"). So if I add dataType to "text/html", then paste in url or the text area which allows only "text/plain", the getData("text/plain") will return nothing. Thus we need to add both:

event.clipboardData.setData('text/html', div.innerHTML); event.clipboardData.setData('text/plain', div.innerHTML.toString());

What this does is that it will allow paste to get the data according to the datatype and hence will work in the url as well.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda