К примеру: "пользователь скопировал этот текст", а в буфере обмена появиться: "пользователь скопировал этот текст www.example.com"
2 ответа
вроде так
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.innerText = 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.innerText = copytext;
selection.selectAllChildren(newdiv);
window.setTimeout(function() {
body_element.removeChild(newdiv);
}, 0);
}
document.oncopy = addLink;
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 !
-
<a href='stacksnippets.net/js'>Собака</a>ue, nisi fugiat. Quia, sapiente, veritatis ! это результат – user33274 8 апр '18 в 15:38
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.innerText = copytext;
selection.selectAllChildren(newdiv);
window.setTimeout(function() {
body_element.removeChild(newdiv);
}, 0);
}
document.oncopy = addLinkOriginal;
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 !