javascript 从剪贴板 Firefox 获取粘贴的图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15253468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Pasted Image from Clipboard Firefox
提问by Bobby Tables
I'm trying to allow the user to paste an image into a div
. The problem is that I need it work in Firefox.
我正在尝试允许用户将图像粘贴到div
. 问题是我需要它在 Firefox 中工作。
From what I've read, Firefox since version 13 (I think) doesn't allow JavaScript access to the clipboard, and event.clipboard
doesn't exist in it. I know it can be done because Gmail and Yahoo alow it even in Firefox.
根据我的阅读,Firefox 自 13 版(我认为)不允许 JavaScript 访问剪贴板,并且event.clipboard
不存在于剪贴板中。我知道它可以完成,因为 Gmail 和 Yahoo 允许它甚至在 Firefox 中。
I just want it to work in anyway posible, be with jQuery, JavaScript, HTML5, it doesn't matter as long as it works in the latest Firefox. (No Flash though).
我只是想让它以任何可能的方式工作,使用 jQuery、JavaScript、HTML5,只要它在最新的 Firefox 中工作都没有关系。(虽然没有闪光灯)。
采纳答案by Dave Lasley
I used the code from this questionfor my cross-browser paste implementation.. it works in all browsers I have tested (scroll down for the actual solution/code). It should be noted that event.clipboardData
expires immediately after the paste event has completed execution.
我将这个问题中的代码用于我的跨浏览器粘贴实现。它适用于我测试过的所有浏览器(向下滚动以获得实际的解决方案/代码)。需要注意的是event.clipboardData
,在 paste 事件执行完成后立即过期。
I went ahead and quadruple checked that this does work in Firefox version 19 (I don't have 13 available, but it sounds like this question was about degradation of a feature in newer versions).
我继续进行了四重检查,这在 Firefox 19 版中确实有效(我没有 13 版,但听起来这个问题是关于新版本中某个功能的降级)。
Below is the answer, quoted from Nico Burns
:
以下是答案,引自Nico Burns
:
Solution
解决方案
Tested in IE6+, FF 3.5+, recent-ish versions of Opera, Chrome, Safari.
在 IE6+、FF 3.5+、Opera、Chrome、Safari 的最新版本中测试。
function handlepaste (elem, e) {
var savedcontent = elem.innerHTML;
if (e && e.clipboardData && e.clipboardData.getData) {// Webkit - get data from clipboard, put into editdiv, cleanup, then cancel event
if (/text\/html/.test(e.clipboardData.types)) {
elem.innerHTML = e.clipboardData.getData('text/html');
}
else if (/text\/plain/.test(e.clipboardData.types)) {
elem.innerHTML = e.clipboardData.getData('text/plain');
}
else {
elem.innerHTML = "";
}
waitforpastedata(elem, savedcontent);
if (e.preventDefault) {
e.stopPropagation();
e.preventDefault();
}
return false;
}
else {// Everything else - empty editdiv and allow browser to paste content into it, then cleanup
elem.innerHTML = "";
waitforpastedata(elem, savedcontent);
return true;
}
}
function waitforpastedata (elem, savedcontent) {
if (elem.childNodes && elem.childNodes.length > 0) {
processpaste(elem, savedcontent);
}
else {
that = {
e: elem,
s: savedcontent
}
that.callself = function () {
waitforpastedata(that.e, that.s)
}
setTimeout(that.callself,20);
}
}
function processpaste (elem, savedcontent) {
pasteddata = elem.innerHTML;
//^^Alternatively loop through dom (elem.childNodes or elem.getElementsByTagName) here
elem.innerHTML = savedcontent;
// Do whatever with gathered data;
alert(pasteddata);
}
<div id='div' contenteditable='true' onpaste='handlepaste(this, event)'>Paste</div>
Explanation
解释
The onpaste
event has the handlepaste
function attached to it, and passed two arguments: this
(i.e. a reference to the element that the event is attached to) and event
which is the event object.
该onpaste
事件具有handlepaste
附加到它的函数,并传递了两个参数:(this
即对事件附加到的元素的引用)和event
哪个是事件对象。
The handlepaste
function:
该handlepaste
函数:
The first line simply saves the content of the editable div to a variable so it can be restored again at the end.
第一行只是将可编辑 div 的内容保存到一个变量中,以便在最后可以再次恢复。
The if
checks whether the browser is an webkit browser (chrome or safari), and if it is it sets contents of the editable div to the data being pasted. It then cancels the event to prevent webkit pasting anything twice. This is because webkit is awkward, and won't paste anything if you simply clear the div.
的if
检查浏览器是否是一个webkit浏览(Chrome或Safari),并且如果它是它设置被粘贴可编辑div来的数据的内容。然后它取消该事件以防止 webkit 两次粘贴任何内容。这是因为 webkit 很笨拙,如果您只是清除 div,则不会粘贴任何内容。
If it is not a webkit browser then it simply clears the editable div.
如果它不是 webkit 浏览器,那么它只是清除可编辑的 div。
It then calls the waitforpastedata
function
然后调用waitforpastedata
函数
The waitforpastedata
function:
该waitforpastedata
函数:
This is necessary because the pasted data doesn't appear straight away, so if you just called processpaste
straight away then it wouldn't have any data to process.
这是必要的,因为粘贴的数据不会立即出现,所以如果您直接调用processpaste
,那么它不会有任何数据要处理。
What it does is check if the editable div has any content, if it does then calls processpaste
, otherwise it sets a timer to call itself and check again in 20 milliseconds.
它所做的是检查可编辑的 div 是否有任何内容,如果有则调用processpaste
,否则它会设置一个计时器来调用自身并在 20 毫秒内再次检查。
The processpaste
function:
该processpaste
函数:
This function saved the innerHTML of the editable div (which is now the pasted data) to a variable, restores the innerHTML of the editable div back to its original value, and the alert the pasted data. Obviously in a real usage scenario you would probably want to something other than just alert data, you can do whatever you like with it from here.
该函数将可编辑div的innerHTML(即现在粘贴的数据)保存到一个变量中,将可编辑div的innerHTML恢复到原来的值,并警告粘贴的数据。显然,在实际使用场景中,您可能想要的不仅仅是警报数据,您可以从这里做任何您喜欢的事情。
You will probably also want to run the pasted data through some kind of data sanitising process. This can be done either while it is still in the editable div, or on the extracted string.
您可能还希望通过某种数据清理过程来运行粘贴的数据。这可以在它仍然在可编辑的 div 中时完成,也可以在提取的字符串上完成。
In a real sitution you would probably want to save the selection before, and restore it afterwards (Set cursor position on contentEditable <div>). You could then insert the pasted data at the position the cursor was in when the user initiated the paste action.
在实际情况中,您可能希望之前保存选择,然后恢复它(在 contentEditable <div> 上设置光标位置)。然后,您可以在用户启动粘贴操作时光标所在的位置插入粘贴的数据。
P.S. The combination of this code, IE <= 8 and jsfiddledoesn't seem to work, but it does work in ie <= 8 in a non-jsfiddle environment.
PS 这段代码的组合,IE <= 8 和jsfiddle似乎不起作用,但它确实在非 jsfiddle 环境中的 ie <= 8 中起作用。