Skip to content

Instantly share code, notes, and snippets.

@chukon
Forked from dusanmarsa/clipboard-paste-image.js
Created March 5, 2020 05:09
Show Gist options
  • Save chukon/a863e66b7af859f3a7bca4dca57a8c84 to your computer and use it in GitHub Desktop.
Save chukon/a863e66b7af859f3a7bca4dca57a8c84 to your computer and use it in GitHub Desktop.

Revisions

  1. @dusanmarsa dusanmarsa revised this gist Oct 29, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion clipboard-paste-image.js
    Original file line number Diff line number Diff line change
    @@ -23,5 +23,5 @@ document.onpaste = function(e){
    }
    }

    // Normal paste handling here
    e.PreventDefault()
    }
  2. @dusanmarsa dusanmarsa created this gist Oct 29, 2017.
    27 changes: 27 additions & 0 deletions clipboard-paste-image.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    var IMAGE_MIME_REGEX = /^image\/(p?jpeg|gif|png)$/i;

    var loadImage = function (file) {
    var reader = new FileReader();
    reader.onload = function(e){
    var img = document.createElement('img');
    img.src = e.target.result;

    var range = window.getSelection().getRangeAt(0);
    range.deleteContents();
    range.insertNode(img);
    };
    reader.readAsDataURL(file);
    };

    document.onpaste = function(e){
    var items = e.clipboardData.items;

    for (var i = 0; i < items.length; i++) {
    if (IMAGE_MIME_REGEX.test(items[i].type)) {
    loadImage(items[i].getAsFile());
    return;
    }
    }

    // Normal paste handling here
    }