admin管理员组文章数量:1355031
I want to write text and html to the user clipboard. I am using the code snippet from MDN:
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: "" });
});
}
});
All I get is this error:
Uncaught (in promise) TypeError: Failed to construct 'ClipboardItem': Failed to convert value to 'Blob'.
Is there another way to copy text and HTML to the clipboard. What do I miss?
I want to write text and html to the user clipboard. I am using the code snippet from MDN: https://developer.mozilla/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: "" });
});
}
});
All I get is this error:
Uncaught (in promise) TypeError: Failed to construct 'ClipboardItem': Failed to convert value to 'Blob'.
Is there another way to copy text and HTML to the clipboard. What do I miss?
Share Improve this question asked Aug 24, 2020 at 13:12 kaljakkaljak 1,2731 gold badge17 silver badges34 bronze badges 3-
Can you give the type and content of the
message
variable? It seems that it can't be converted to a blob in the third line. – Yann Commented Aug 27, 2020 at 6:23 -
Also, what browser are you using? The Clipboard API is only a draft at the time of writing and only Chrome seems to support it fully (with
ClipboardItem
). developer.mozilla/en-US/docs/Web/API/… – Yann Commented Aug 27, 2020 at 6:27 - I am using the latest Chrome and message is a string – kaljak Commented Aug 27, 2020 at 9:14
1 Answer
Reset to default 9 +50assuming your message is string type, here is a demo code
your code would be
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: "" });
});
}
});
but Clipboard API and events are still working draft, I remend using an alternatives like clipboard.js
本文标签:
版权声明:本文标题:javascript - TypeError: Failed to construct 'ClipboardItem': Failed to convert value to 'Blob' - 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744006372a2574776.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论