【发布时间】:2019-06-19 04:58:44
【问题描述】:
如何使用 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" }
所以问题可能可以重新表述——如何在copy 事件处理函数中将文件附加到clipboardData?
document.addEventListener('copy', (event) => {
// event.clipboardData.?
// maybe event.clipboardData.setData(?)
})
【问题讨论】:
-
当您在右键单击图像的上下文菜单中单击
copy image时,它已经在剪贴板中。 -
@koushik-chatterjee 是的。我想以编程方式模拟这种行为。
-
好的,让我为工作演示添加一个答案
-
@epascarello 感谢您的链接
标签: javascript html clipboarddata