我刚刚更新到Chrome 76稳定版,并尝试使用新的剪贴板API将一些文本(以及后来的图像)复制到剪贴板。但是,navigator.clipboard.write(data)
似乎不起作用。这是我的代码:
setInterval(function() {
console.log("Wriing to clipbard");
navigator.permissions.query({ name: 'clipboard-write' }).then(result => {
if (result.state === 'granted') {
var data = new Blob(["Text data"], {type : "text/plain"});
navigator.clipboard.write(data).then(function() {
console.log("Copied to clipboard successfully!");
}, function(error) {
console.error("unable to write to clipboard. Error:");
console.log(error);
});
} else {
console.log("clipboard-permissoin not granted: " + result);
}
});
}, 3000);
我正在chrome扩展内容脚本中运行此脚本,并设置了剪贴板权限,因此确实可以授予这些权限。引发的错误是:
unable to write to clipboard. Error:
TypeError: Failed to execute 'write' on 'Clipboard': Iterator getter is not callable.
足够奇怪的是,当我使用navigator.clipboard.write text(text)
而不是navigator.clipboard.write(data)
时,一切运行正常。问题是,我想使用write(data)
,因为稍后我也想将图像写入剪贴板。任何想法为什么它不起作用?谢谢。
编辑:我从规格表https://w3c.github.io/clipboard-apis/#dom-clipboard-write中获取了代码
更新:好的,我可以使用ClipboardItem进行文本复制(请参阅下面的答案),但是如果我对dataURL编码的图像进行同样的操作,则整个网页都会崩溃,并显示“ Aw snap”消息。所以不确定那里发生了什么。这是将使站点崩溃并强制重新加载的代码:
setInterval(function() {
console.log("Wriing to clipbard");
navigator.permissions.query({ name: 'clipboard-write' }).then(result => {
if (result.state === 'granted') {
//var blob = new Blob(['hello'], {type: 'text/plain'});
var data = new Blob(["iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+P+/HgAFhAJ/wlseKgAAAABJRU5ErkJggg=="], {type : "image/png"});
var item = new ClipboardItem({'image/png': data});
navigator.clipboard.write([item]).then(function() {
console.log("Copied to clipboard successfully!");
}, function(error) {
console.error("unable to write to clipboard. Error:");
console.log(error);
});
} else {
console.log("clipboard-permissoin not granted: " + result);
}
});
}, 3000);
答案 0 :(得分:1)
好的,这是对我有用的解决方案。您必须在调用write()之前将blob包装在ClipboardItem对象中。我通过阅读https://bugs.chromium.org/p/chromium/issues/detail?id=150835,然后在网上搜索“ ClipboardItem”,然后找到此代码https://github.com/web-platform-tests/wpt/blob/master/clipboard-apis/async-navigator-clipboard-basics.https.html,偶然发现了该解决方案。似乎还没有真正的文档,但是,它可以工作!
setInterval(function() {
console.log("Wriing to clipbard");
navigator.permissions.query({ name: 'clipboard-write' }).then(result => {
if (result.state === 'granted') {
var blob = new Blob(['hello'], {type: 'text/plain'});
var item = new ClipboardItem({'text/plain': blob});
navigator.clipboard.write([item]).then(function() {
console.log("Copied to clipboard successfully!");
}, function(error) {
console.error("unable to write to clipboard. Error:");
console.log(error);
});
} else {
console.log("clipboard-permissoin not granted: " + result);
}
});
}, 3000);
更新:好的,我还可以复制图像(查看同一来源)。 url是图片的网址(duh)
async function copyImage(url) {
console.log("Wriing to clipbard");
const response = await fetch(url);
const blob = await response.blob();
const item = new ClipboardItem({'image/png': blob});
await navigator.clipboard.write([item]).then(function() {
console.log("Copied to clipboard successfully!");
}, function(error) {
console.error("unable to write to clipboard. Error:");
console.log(error);
});
}