How to obtain data from clipboard in Firefox

后端 未结 3 1354
轻奢々
轻奢々 2021-02-06 08:02

I would like to trigger onpaste event on element to retrieve data in clipboard (I want to check if image exists in clipboard and upload it into the server). It works perfect on

相关标签:
3条回答
  • 2021-02-06 08:40

    You need to create one contenteditable div which will hold the image on paste

    var pasteCatcher = $('<div/>',{'id':'container'});
    $('body').append(pasteCatcher);
    var pasteCatcher = document.getElementById('container');
                   pasteCatcher.setAttribute("contenteditable", "");
    

    then you need to wait for paste event and process it

     window.addEventListener("paste",processEvent);
    function processEvent(e) {
    //some functionality
    }
    

    Also write the code to grab the image data from contenteditable div and send it to server.

    0 讨论(0)
  • 2021-02-06 08:41

    It seems not. Sorry.

    http://support.mozilla.org/en-US/kb/Granting%20JavaScript%20access%20to%20the%20clipboard

    JavaScript get clipboard data on paste event (Cross browser)

    0 讨论(0)
  • 2021-02-06 08:46

    Sure I can. In this example I retrieve image from clipboard after using Ctrl+V:

     <div id="foo" style="width: 200px; height: 200px" contenteditable="true">Paste here!</div>
     $('#foo')[0].onpaste = function(e)
    {                   
        setTimeout(function()
        {
            var blob = $('#foo img').attr('src');
    
    
            $.post('/upload/image', {'data': blob}, function(result)
            {
    
    
            }, 'json'); 
    
        }, 200);
    }
    

    It works with <div> element that has contenteditable attribute, but does not work with <textarea>

    P.S. Sorry for answering my own question but this piece of code might help someone.

    0 讨论(0)
提交回复
热议问题