Created
December 23, 2018 12:54
-
-
Save Firsh/e0fa22586c9fdd8a396460aa3b963398 to your computer and use it in GitHub Desktop.
Copyright Copy Paste
This file contains hidden or 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
function addLink() { | |
var content_element = document.getElementById('wtr-content'); | |
var body_element = document.getElementsByTagName("body")[0] | |
var selection, newdiv; | |
if (window.getSelection) { | |
selection = window.getSelection(); | |
}else{ | |
var ypos = jQuery(document).scrollTop() | |
selection = readSelected(content_element) | |
jQuery(document).scrollTop(ypos) | |
} | |
newdiv = document.createElement('div'); | |
newdiv.style.position = "absolute"; | |
newdiv.style.top=ypos | |
newdiv.style.left=-9999 | |
body_element.appendChild(newdiv); | |
var copytext = selection + "<br /><br /> Read more at: <a href='"+document.location.href+"'>"+document.location.href+"</a>"; | |
newdiv.innerHTML = copytext; | |
if (window.getSelection) { | |
selection.selectAllChildren(newdiv); | |
} else { | |
var range = document.body.createTextRange(); | |
range.moveToElementText(newdiv); | |
range.select (); | |
} | |
window.setTimeout(function() { | |
body_element.removeChild(newdiv); | |
},0); | |
} | |
document.getElementById('wtr-content').oncopy = addLink; | |
function readSelected(id) { | |
var sel = "", el = id; | |
if (typeof el.selectionStart == "number" && typeof el.selectionEnd == "number") { | |
sel = el.value.slice(el.selectionStart, el.selectionEnd); | |
} else if (document.selection && document.selection.createRange) { | |
el.focus(); | |
sel = document.selection.createRange().text; | |
} | |
return sel; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment