Forked from bes-internal/gist:63e82299a6f609ff4577
Last active
August 29, 2015 14:25
-
-
Save jkeks/4a3cd69381fe9b81d6a9 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
<script> | |
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) вешаем наш скрипт на событие копирования | |
<\/script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment