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() .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/clipboardapiweb