Skip to content

Instantly share code, notes, and snippets.

@dkrnl
Last active August 29, 2015 14:20
Show Gist options
  • Save dkrnl/e1a24b7072ca60987477 to your computer and use it in GitHub Desktop.
Save dkrnl/e1a24b7072ca60987477 to your computer and use it in GitHub Desktop.
Jquery Clipboard Copyright
jQuery(function($){
$(document).on("copy", function () {
var selection, html = "";
if (window.getSelection || document.getSelection) {
selection = window.getSelection ? window.getSelection() : document.getSelection();
if (selection.rangeCount) {
html = document.createElement("div");
for (var i = 0, n = selection.rangeCount; i < n; ++i) {
html.appendChild(selection.getRangeAt(i).cloneContents());
}
html = html.innerHTML;
}
} else if (document.selection && document.selection.type == "Text") {
selection = document.selection.createRange();
html = selection.htmlText;
}
if (!html) {
return;
}
html += "<br>\n<br>\n" + document.location.href; // magic here
var container = $("<div style=\"position:absolute;left:-99999px\"></div>").appendTo("body");
container.html(html);
selection.selectAllChildren(container.get(0));
setTimeout(function() {
container.remove();
}, 0);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment