Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to detect the drop event in chrome extension when dropped a file

I am building a chrome extension which will let the user to drag and drop files and save the same to the server. From the extension I have injected a div element in the page when I drop an image file the browser is displaying the image on the entire page. The drop event is not getting detected in the extension, but if I have a input element with the type file and if I drop the file on that element then the change event is getting detected.

Not sure how to detect the drop event from the extension. Any help is appreciated.

contentScript.js file

//building the Dropzone Div
var dropdiv = $("<div>", {
  id    :"sforce-dz-dropZone",
  class : "sforce-dz-dropZonebg"
}).text('Add you\'re files here');

//injecting the drop div in the page
$("input[name=attachFile]").after(dropdiv);

//adding 'drop' event listener to the div.
//This is not getting logged at all.
$("#sforce-dz-dropZone").on('drop', function(e){
  e.preventDefault();
  e.stopPropagation();

  var files = e.target.files || e.dataTransfer.files;
  // process all File objects
  for (var i = 0, f; f = files[i]; i++) {
    console.log('the file name is '+ f.name);
  }

});

//Adding another event. click, just to see if the events are getting triggered.
//When clicked on the div the console is logging the below string.
$("#sforce-dz-dropZone").on('click',function(){
  console.log('clicked');
});

Manifest File

{
  "name": "name",
  "version": "0.0.1",
  "manifest_version": 2,
  "description": "text",
  "author": "someone",
  "content_scripts": [
    {
      "matches" : ["https://*.mysite.com/*"],
       "js"     : ["jquery.js","contentScript.js"],
       "css"    : ["sforce-dz.css"]
    }
  ],
  "permissions": [
     "cookies",
     "unlimitedStorage"
  ]
}
like image 252
Sam Avatar asked Dec 08 '14 16:12

Sam


People also ask

Why is drop event not firing?

In order to have the drop event occur on a div element, you must cancel the ondragenter and ondragover events. Using jquery and your code provided... Show activity on this post.

Why can t I drag and drop files in chrome?

Running Chrome as an administrator is causing this problem. Just go to chrome.exe settings and disable it. Restart the browser and drag and drop is back.

How do I drag and drop in Chrome?

To drag and drop using the Chromebook touchpad: Click the item you want to move with one finger. With a second finger, move the item. Release both fingers to drop the item at its new location.


1 Answers

This is a really confusing part of handling drag and drop. You'd expect to just listen for the drop event as you do with click submit mouseOver etc. But it won't work unless it also has a dragOver event.

"without the dragOver event handler, the default event handler from dragOver event is used, thus, no drop event is triggered." Answer Here

Simplest working example

/* events fired on the drop targets */
document.addEventListener("dragover", function( event ) {
  // prevent default to allow drop
  event.preventDefault();
});

document.addEventListener("drop", function( event ) {
  // prevent default action (open as link for some elements)
  event.preventDefault();
  console.log('dropped');
});

Working example from HTML5Rocks

function handleFileSelect(evt) {
  evt.stopPropagation();
  evt.preventDefault();

  var files = evt.dataTransfer.files; // FileList object.

  // files is a FileList of File objects. List some properties.
  var output = [];
  for (var i = 0, f; f = files[i]; i++) {
    output.push('<li><strong>', escape(f.name), '</strong> (', f.type || 'n/a', ') - ',
                f.size, ' bytes, last modified: ',
                f.lastModifiedDate ? f.lastModifiedDate.toLocaleDateString() : 'n/a',
                '</li>');
  }
  document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>';
}

function handleDragOver(evt) {
  evt.stopPropagation();
  evt.preventDefault();
  evt.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
}

// Setup the dnd listeners.
var dropZone = document.getElementById('drop_zone');
dropZone.addEventListener('dragover', handleDragOver, false);
dropZone.addEventListener('drop', handleFileSelect, false);
like image 127
Lex Avatar answered Oct 11 '22 12:10

Lex