如何使用Javascript(在copy
事件的处理程序中)将图像放入剪贴板,就好像它会通过右键单击浏览器中的图像并从上下文菜单中选择“复制图像”来完成。如何将图像放入剪贴板中?
此script显示剪贴板内容的详细信息。对于复制的图像其打印:
DataTransfer { dropEffect: "none", effectAllowed: "uninitialized", items: DataTransferItemList[2], types: Array[2], files: FileList[1], mozItemCount: 1, mozCursor: "auto", mozUserCancelled: false, mozSourceNode: null }
DataTransferItem { kind: "string", type: "text/html" }
DataTransferItem { kind: "file", type: "image/png" }
Array [ "text/html", "Files" ]
File { name: "image.png", lastModified: 1504122845696, lastModifiedDate: Date 2017-08-30T19:54:05.696Z, webkitRelativePath: "", size: 385273, type: "image/png" }
所以,问题可以很可能改写 - 如何将文件附加到clipboardData
在copy
事件处理函数?
document.addEventListener('copy', (event) => {
// event.clipboardData.?
// maybe event.clipboardData.setData(?)
})
你搜索? https://stackoverflow.com/questions/33175909/copy-image-to-clipboard – epascarello
当你点击'复制图像'在右键单击图像上下文菜单中,它已经在剪贴板中。 –
@ koushik-chatterjee是的。我想以编程方式模拟这种行为。 – czerny