JSFiddle - React, Tailwind, and code Playground
HTML
Lorem ipsum dolor sit amet, Собака consectetur adipisicing elit. Debitis Собака culpa dignissimos corporis mollitia repudiandae, dolore maxime similique cumque quisquam pariatur, voluptatum sed, quis exercitationem eaque, nisi fugiat. Quia, sapiente, veritatis Собака!
JavaScript
function addLinkOriginal() {
var body_element = document.getElementsByTagName('body')[0];
var selection = window.getSelection();
var pagelink = " <a href='"+document.location.href+"'>источник</a> © ";
var copytext = selection + pagelink;
var newdiv = document.createElement('div');
newdiv.style.position = 'absolute';
newdiv.style.left = '-99999px';
body_element.appendChild(newdiv);
newdiv.innerHTML = copytext;
selection.selectAllChildren(newdiv);
window.setTimeout( function() {
body_element.removeChild(newdiv);
}, 0);
}
function addLink() {
var body_element = document.getElementsByTagName('body')[0];
var selection;
selection = window.getSelection();
var pos = (''+selection).lastIndexOf("Собака");
var pagelink = "<a href='"+document.location.href+"'>Собака</a>";
String.prototype.replaceAt=function(index, character) {
return this.substr(0, index) + character + this.substr(index+6);
}
var copytext = (''+selection).replaceAt(pos, pagelink);
var newdiv = document.createElement('div');
newdiv.style.position='absolute';
newdiv.style.left='-99999px';
body_element.appendChild(newdiv);
newdiv.innerHTML = copytext;
selection.selectAllChildren(newdiv);
window.setTimeout(function() {
body_element.removeChild(newdiv);
},0);
}
document.oncopy = addLink;