Created
July 16, 2015 14:20
-
-
Save jkeks/b9988831fd582cce1ed1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// при копировании добавляем URL | |
function nsaddlink() { | |
if (typeof window.getSelection == "undefined") return; //только для тех кто умеет копировать | |
var selection = window.getSelection(), | |
newdiv = document.createElement('div'), | |
newa = document.createElement('a'); | |
newa.style.position=newdiv.style.position = 'absolute'; //скрываем служебный блок и ссылку в скопированном тексте | |
newdiv.style.left=newa.style.left='-9999px'; | |
newa.href=document.location.href; | |
newa.innerHTML=" Источник: "+document.location.href; //ссылка на текущую страницу и анкор (можно и без анкора, но если вставляется как текст то ссылка как бы сохраняется) | |
newdiv.appendChild(selection.getRangeAt(0).cloneContents()); | |
newdiv.appendChild(newa); | |
document.body.appendChild(newdiv); | |
selection.selectAllChildren(newdiv); //вставляем скопированный текст и ссылку в служебный блок и делаем новое выделение, которое пользователь и копируем в буфер | |
window.setTimeout(function(){document.body.removeChild(newdiv);}, 200); //удляем служебный блок | |
} | |
document.addEventListener('copy', nsaddlink); //синхронно (перед закрывающимся body) вешаем наш скрипт на событие копирования |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment