Skip to content

Instantly share code, notes, and snippets.

@SergSlon
Created November 9, 2015 10:52
Show Gist options
  • Save SergSlon/c38bf4e987b24c005fe4 to your computer and use it in GitHub Desktop.
Save SergSlon/c38bf4e987b24c005fe4 to your computer and use it in GitHub Desktop.

Revisions

  1. SergSlon renamed this gist Nov 9, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. SergSlon created this gist Nov 9, 2015.
    30 changes: 30 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    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);
    });

    });