0

I want to add a read more text to coppied text of my website. So if someone selects and copy a text and place it to a clipboard/other source it will add automatically read more: url of the source.

The problem is that I'm using a code but when someone selects and copy a text the layout of the text disappears. For example white lines and paragraphs disappear and all the text will be together without interruptions with white lines or paragraphs.

I've tried several solutions in the past but most are as simple as this code.

<script>
function addLink() {
 //Get the selected text and append the extra info
 var selection = window.getSelection(),
 pagelink = '<br /><br /> Lees meer op: ' + document.location.href + ' voor meer informatie', // Change this text
 copytext = selection + pagelink,
 newdiv = document.createElement('div');

 //hide the newly created container
 newdiv.style.position = 'absolute';
 newdiv.style.left = '-99999px';

 //insert the container, fill it with the extended text, and define the new selection
 document.body.appendChild(newdiv);
 newdiv.innerHTML = copytext;
 selection.selectAllChildren(newdiv);

 window.setTimeout(function () {
 document.body.removeChild(newdiv);
 }, 100);
 }

 document.addEventListener('copy', addLink);
</script>

I want to avoid that the layout of the selected and copied text disappaers. How can I solve this? Thanks in advance.

Runfun026
  • 1
  • 1

1 Answers1

0

The strange thing is I finally found a code at stackoverflow that works with the phpbb part of my website but not with the WordPress part of my website. Does anyone know how I can make the code mentioned here How to automatically append text to text copied with JavaScript work for the WordPress part of my website?

Runfun026
  • 1
  • 1