Skip to content

Instantly share code, notes, and snippets.

@djekl
Forked from stevebauman/clipboard.js
Created October 12, 2021 11:02
Show Gist options
  • Save djekl/f163efa095cacf41c3dc7a46b2d6938f to your computer and use it in GitHub Desktop.
Save djekl/f163efa095cacf41c3dc7a46b2d6938f to your computer and use it in GitHub Desktop.

Revisions

  1. @stevebauman stevebauman revised this gist Aug 17, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion clipboard.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /**
    * Copy the rich text content to clipboard.
    * Copy rich text content to clipboard.
    *
    * Must be initiated by a user click event.
    *
  2. @stevebauman stevebauman revised this gist Aug 13, 2021. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions clipboard.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,10 @@
    /**
    * Copy the rich text content to clipboard.
    *
    * Must be initiated by a user click event.
    *
    * @param {string} content
    */
    export default function (content) {
    const selection = window.getSelection();

  3. @stevebauman stevebauman created this gist Aug 13, 2021.
    25 changes: 25 additions & 0 deletions clipboard.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    export default function (content) {
    const selection = window.getSelection();

    const range = document.createRange();

    range.selectNodeContents(document.body);

    selection.removeAllRanges();

    selection.addRange(range);

    const listener = (e) => {
    e.clipboardData.setData("text/html", content);
    e.clipboardData.setData("text/plain", content);
    e.preventDefault();
    }

    document.addEventListener("copy", listener);

    document.execCommand("copy");

    document.removeEventListener("copy", listener);

    selection.removeAllRanges();
    }