1

我正在编辑器上工作,我想通过单击提供一个按钮来粘贴已复制到剪贴板的图像。我不能这样做,因为出于安全原因,浏览器不允许访问剪贴板。

我查看了 Google Drive 以了解 Google 是如何完成的。在 chrome 中,他们要求从 Chrome webstore 安装 Google Drive webapp,该应用程序请求 clipboardRead 和 clipboardWrite权限,并且一旦安装了应用程序。一切都像 Google Drive 中的魅力一样。文档说使用 document.execCommand('paste')。但是我找不到任何实现这个的示例,也无法在我的应用程序中实现它。有人可以在这里为我提供一个示例,说明当图像在剪贴板中时如何进行这项工作。

4

3 回答 3

1

我们需要在应用程序的清单文件中添加权限(clipboardRead 和 clipboardWrite),然后如果要从剪贴板粘贴文本/图像,我们需要将焦点放在 html 中的虚拟文本框/图像元素上,然后运行 ​​document. execCommand('paste') 现在这个元素可以访问剪贴板复制的项目并获取文本或图像以使用它。

于 2013-01-21T18:19:09.970 回答
1

归功于https://stackoverflow.com/a/6338207/85597

// window.addEventListener('paste', ... or
document.onpaste = function(event){
  var items = event.clipboardData.items;
  console.log(JSON.stringify(items)); // will give you the mime types
  var blob = items[0].getAsFile();
  var reader = new FileReader();
  reader.onload = function(event){
    console.log(event.target.result)}; // data url!
  reader.readAsDataURL(blob);
}
于 2013-07-02T17:24:30.197 回答
0

到目前为止,没有来自 chrome 的 API

https://groups.google.com/forum/?fromgroups=#!topic/chromium-extensions/BJAn5_OwT2g,因为粘贴被认为存在安全风险

于 2012-11-21T06:35:56.637 回答