在chrome中,使用window.Clipboard对象,是否有一种方法来捕获粘贴的文本? [英] In chrome, using the window.Clipboard object, is there a way to capture pasted text?
问题描述
您可以拍摄图片。我试图找出如何捕获文本。我猜,没有,出于安全原因,但我想确保。
You can capture an image. I am trying to figure out how to capture text. I'm guessing there isn't, for security reasons, but I wanted to make sure.
还有这个东西的参考? window.Clipboard
对象不是v8引擎的一部分,它是chrome浏览器的一部分,我找不到它的官方文档。
Also is there a reference for this stuff? window.Clipboard
object isn't part of the v8 engine, it's a part of the chrome browser and I can't find official documentation for it.
推荐答案
在您链接的代码中有一个 pasteHandler
函数:
In the code you linked there is a pasteHandler
function with the following:
// Get the items from the clipboard
var items = e.clipboardData.items;
if (items) {
// Loop through all items, looking for any kind of image
for (var i = 0; i < items.length; i++) {
if (items[i].type.indexOf("image") !== -1) {
// We need to represent the image as a file,
var blob = items[i].getAsFile();
// and use a URL or webkitURL (whichever is available to the browser)
// to create a temporary URL to the object
var URLObj = window.URL || window.webkitURL;
var source = URLObj.createObjectURL(blob);
// The URL can then be used as the source of an image
createImage(source);
}
}
}
Chrome开发人员框架告诉我项目[i]是 DataTransferItem
(参考)
Chrome developer frame is telling me that items[i] is a DataTransferItem
(reference)
在参考页面上,我看到一个 kind
属性和一个 getAsString ()
方法。后者似乎需要一个接收文本作为参数的回调函数。因此,要使用上述脚本处理文本值,您可以修改我链接的部分如下:
On the reference page I see a kind
property and a getAsString()
method. The latter seems to require a callback function that receives the text as a parameter. So to handle text values using the above script you might modify the section I linked as follows:
// Get the items from the clipboard
var items = e.clipboardData.items;
if (items) {
// Loop through all items, looking for any kind of image
for (var i = 0; i < items.length; i++) {
if (items[i].type.indexOf("image") !== -1) {
// We need to represent the image as a file,
var blob = items[i].getAsFile();
// and use a URL or webkitURL (whichever is available to the browser)
// to create a temporary URL to the object
var URLObj = window.URL || window.webkitURL;
var source = URLObj.createObjectURL(blob);
// The URL can then be used as the source of an image
createImage(source);
}
if (items[i].kind === "string"){
items[i].getAsString(function(s) {
alert(s);
});
}
}
}
这篇关于在chrome中,使用window.Clipboard对象,是否有一种方法来捕获粘贴的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!