gpt4 book ai didi

javascript - 类型错误 : Failed to construct 'ClipboardItem' : Failed to convert value to 'Blob'

转载 作者:行者123 更新时间:2023-12-01 16:09:44 27 4
gpt4 key购买 nike

我想将文本和 html 写入用户剪贴板。我正在使用来自 MDN 的代码片段:https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/write

navigator.permissions.query({ name: 'clipboard-write' }).then(result => {
if (result.state === 'granted') {
let data = [new ClipboardItem({ "text/plain": message })];
navigator.clipboard.write(data).then(function() {
$.growl.notice({ message: ResourceService.getKey("CopyToClipboardSuccess"), location: "tc", title: "" });
}, function() {
$.growl.error({ message: ResourceService.getKey("CopyToClipboardError"), location: "tc", title: "" });
});
}
});
我得到的只是这个错误:
Uncaught (in promise) TypeError: Failed to construct 'ClipboardItem': Failed to convert value to 'Blob'.
是否有另一种方法可以将文本和 HTML 复制到剪贴板。我想念什么?

最佳答案

假设您的消息是字符串类型,here is a demo code
你的代码是

navigator.permissions.query({ name: 'clipboard-write' }).then(result => {
if (result.state === 'granted') {
const type = 'text/plain';
const blob = new Blob([message], { type });
let data = [new ClipboardItem({ [type]: blob })];
navigator.clipboard.write(data).then(function() {
$.growl.notice({ message: ResourceService.getKey("CopyToClipboardSuccess"), location: "tc", title: "" });
}, function() {
$.growl.error({ message: ResourceService.getKey("CopyToClipboardError"), location: "tc", title: "" });
});
}
});
但剪贴板 API 和事件仍在草稿中,我建议使用 clipboard.js 之类的替代方法

关于javascript - 类型错误 : Failed to construct 'ClipboardItem' : Failed to convert value to 'Blob' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63561767/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com