js 获取复制的数据
在JavaScript中,可以使用Clipboard API来获取复制的数据。
- 首先,使用
navigator.clipboard.readText()方法来读取剪贴板中的文本数据。
navigator.clipboard.readText()
.then(text => {
console.log(text);
})
.catch(err => {
console.error('Failed to read clipboard contents: ', err);
});
- 如果剪贴板中的数据是HTML格式,可以使用
navigator.clipboard.read()方法来读取。
navigator.clipboard.read()
.then(data => {
for (const item of data) {
if (item.types.includes('text/html')) {
item.getType('text/html')
.then(html => {
console.log(html);
})
.catch(err => {
console.error('Failed to read HTML data from clipboard: ', err);
});
}
}
})
.catch(err => {
console.error('Failed to read clipboard contents: ', err);
});
注意:Clipboard API可能不支持所有的浏览器,因此在使用之前最好先检查浏览器的兼容性
原文地址: https://www.cveoy.top/t/topic/ieLF 著作权归作者所有。请勿转载和采集!