You can capture an image.我试图找出如何捕获文本.我猜是因为安全原因没有,但我想确定一下.
这个东西还有参考吗? window.Clipboard对象不是v8引擎的一部分,它是chrome浏览器的一部分,我无法找到它的官方文档.
解决方法:
在您链接的代码中,有一个pasteHandler函数,其中包含以下内容:
// 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开发者框架告诉我items [i]是DataTransferItem (reference)
在参考页面上,我看到了一个kind属性和一个getAsString()方法.后者似乎需要一个回调函数来接收文本作为参数.因此,要使用上述脚本处理文本值,您可以修改我链接的部分,如下所示:
// 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);
});
}
}
}