【问题标题】:Paste an Image from Clipboard in Chrome在 Chrome 中粘贴剪贴板中的图像
【发布时间】:2012-11-20 16:41:24
【问题描述】:

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

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

【问题讨论】:

    标签: google-chrome google-chrome-extension clipboard google-drive-api google-chrome-app


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      归功于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);
      }
      

      【讨论】:

      • 后续问题:我懂一点JS,但我想更多的是C#。我需要做什么才能将粘贴的文件保存到磁盘?我的猜测是这样的吗? reader.Save(路径)?
      【解决方案3】:

      目前还没有来自 chrome 的 API

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

      【讨论】:

      • 您是否检查过drive.google.com 并尝试使用编辑器中的“粘贴”图标编辑文档并粘贴一些内容。如果您通过 chrome webapp 获得许可,有可能但不确定如何?
      猜你喜欢
      • 2013-07-08
      • 1970-01-01
      • 2013-12-06
      • 1970-01-01
      • 1970-01-01
      • 2017-06-06
      • 1970-01-01
      • 1970-01-01
      • 2013-02-21
      相关资源
      最近更新 更多