如何使用 Javascript(在copy
事件处理程序中)将图像放入剪贴板,就像通过右键单击浏览器中的图像并从上下文菜单中选择“复制图像”来完成一样。
此脚本显示剪贴板内容的详细信息。对于复制的图像,它会打印:
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(?)
})