【发布时间】:2015-02-06 08:10:25
【问题描述】:
我正在构建一个 chrome 扩展,它允许用户拖放文件并将其保存到服务器。从扩展中,我在页面中注入了div 元素,当我放置图像文件时,浏览器会在整个页面上显示图像。扩展中未检测到drop 事件,但如果我有一个类型为文件的输入元素,并且如果我将文件放在该元素上,则检测到change 事件。
不确定如何从扩展中检测drop 事件。任何帮助表示赞赏。
contentScript.js 文件
//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');
});
清单文件
{
"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"
]
}
【问题讨论】:
-
此代码在您的扩展程序中的什么位置,并向我们展示清单和/或您如何注入此代码。
-
我编辑了我的评论。 div 被注入就好了。我无法做的就是无法在 javascript contentScript 中检测到
drop事件
标签: javascript jquery google-chrome-extension drag-and-drop