Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain data from clipboard in Firefox

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 Chrome:

$('#textarea')[0].onpaste = function(event)
{
    var items = event.clipboardData.items;

    if (items.length)
    {
        var blob = items[0].getAsFile();
        var fr = new FileReader();

        fr.onload = function(e)
        {
            alert('got it!');
        }

        fr.readAsDataURL(blob);
    }
}

Does not work on Firefox: event.clipboardData.items does not exists. Do you have any idea how to retrive onpaste event in element?

like image 349
Bald Avatar asked Feb 10 '12 21:02

Bald


People also ask

How do I access the clipboard in Firefox?

In order to access stored clipboard items, please open toolbar popup or right-click on an editable area and then choose clipboard manager in the right-click. Then, click on a desired clipboard item; and it will be inserted to the editable filed.

How can I retrieve data from clipboard?

To retrieve data from the Clipboard in a single, common format. Use the GetAudioStream, GetFileDropList, GetImage, or GetText method. Optionally, use the corresponding Contains Format methods first to determine whether data is available in a particular format. These methods are available only in .

How do I enable clipboard sharing in Firefox?

Chosen solution Firefox currently doesn't allow web pages to access the clipboard via JavaScript, so your only option would be to use the keyboard. Does Shift+Insert work in case the website is intercepting the Ctrl key?

Can a website read your clipboard?

This is because websites scripts can erase and replace what you currently have in your clipboard (data loss issue) and they can read whatever you have in your clipboard (security and privacy issue); as such, you should grant access with caution.


1 Answers

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.

like image 81
Mahendra Avatar answered Oct 05 '22 03:10

Mahendra