Skip to content

Instantly share code, notes, and snippets.

@daler445
Created May 17, 2018 14:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daler445/d1be93b00f5a46e7944f0d61cafdc90b to your computer and use it in GitHub Desktop.
Save daler445/d1be93b00f5a46e7944f0d61cafdc90b to your computer and use it in GitHub Desktop.
add text to copied text with styles
<!doctype html>
<html>
<head>
<title>Playground</title>
<script type='text/javascript'>
function getSelectionHtml() {
var html = "";
if (typeof window.getSelection != "undefined") {
var sel = window.getSelection();
if (sel.rangeCount) {
var container = document.createElement("div");
for (var i = 0, len = sel.rangeCount; i < len; ++i) {
container.appendChild(sel.getRangeAt(i).cloneContents());
}
html = container.innerHTML;
}
} else if (typeof document.selection != "undefined") {
if (document.selection.type == "Text") {
html = document.selection.createRange().htmlText;
}
}
return html;
}
document.addEventListener('copy', function(){
var selectedHTML = getSelectionHtml();
var selection = window.getSelection(),
pagelink = 'my text inject', // <-------------- change text here
copytext = getSelectionHtml() + pagelink,
newdiv = document.createElement('div');
newdiv.style.position = 'absolute';
newdiv.style.left = '-99999px';
document.body.appendChild(newdiv);
newdiv.innerHTML = copytext;
selection.selectAllChildren(newdiv);
window.setTimeout(function() {document.body.removeChild(newdiv)}, 100);
});
</script>
</head>
<body>
<div>
<p>Nemegtia Eotriceratops Euhelopus Bienosaurus Siamotyrannus Nothronychus Jeholosaurus Sinosauropteryx Cristatusaurus Shuvuuia Campylodon Ekrixinatosaurus Proceratosaurus Cryptovolans Danubiosaurus.</p><p>Hallopus Amargasaurus Elopteryx Puertasaurus Guaibasaurus Lycorhinus Gasosaurus <b>Dryptosaurus Siamosaurus Proa Pyroraptor Bruhathkayosaurus Dubreuillosaurus</b> Dyoplosaurus Sauroniops.</p><p>Rapator Dongyangosaurus <i>Ferganocephale</i> Cedrorestes Amtosaurus <a href="https://google.com">Gobipteryx</a> Microvenator Neimongosaurus Dysganus Pneumatoarthrus Torilion Tylocephale Dystylosaurus Gwyneddosaurus Psittacosaurus.</p><p>Nigersaurus Janenschia Cylindricodon Manidens Eolosaurus Erlicosaurus Saltopus Clepsysaurus Macrogryphosaurus Ornithosuchus Rahiolisaurus Eoabelisaurus <u>Diceratus <i>Eustreptospondylus</i></u> Gasosaurus.</p><p>Haplocanthosaurus Iliosuchus Goyocephale Eobrontosaurus Jainosaurus Troodon Albinykus Sphenosaurus Gigantosaurus Abrictosaurus Linhevenator Unenlagia Austrosaurus Daanosaurus Ajkaceratops.</p><p>Edmontosaurus Jubbulpuria Niobrarasaurus <b>Eucentrosaurus Vitakrisaurus Othnielia Piatnitzkysaurus Tehuelchesaurus Tsaagan Hypsibema</b> Blasisaurus Lanasaurus <span style='color:red;'>Neuquenraptor</span> Sinusonasus Riojasuchus.</p><p>Nodocephalosaurus Erectopus Alectrosaurus Loncosaurus Ganzhousaurus Guaibasaurus Argyrosaurus Ahshislepelta Dakosaurus Leptorhynchos Hesperonychus Kakuru Saltasaurus Quilmesaurus Sinovenator.</p>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment