浏览器原生剪贴板 navigator.clipboard
写入 navigator.clipboard.writeText
navigator.clipboard.writeText('Linr Text to be copied')
.then(() => {
console.log('Text copied to clipboard');
})
.catch(err => {
// This can happen if the user denies clipboard permissions:
console.error('Could not copy text: ', err);
});
读取 navigator.clipboard.readText
navigator.clipboard.readText()
.then(text => {
console.log('Pasted content: ', text);
})
.catch(err => {
console.error('Failed to read clipboard contents: ', err);
});
document.addEventListener('paste', event => {
event.preventDefault();
navigator.clipboard.getText().then(text => {
console.log('Pasted text: ', text);
});
});
原文:https://developers.google.com/web/updates/2018/03/clipboardapi