Clipboard Browser API

Note: The API is still experimental and does not work along all browsers!
Tested in current version of Google Chrome Desktop Browser.

Write text to clipboard

javascript

function copyToClipboard(text) {
    navigator.clipboard.writeText(text);
}

Get text from clipboard

javascript

function getFromClipboard() {
    navigator.clipboard
        .readText()
        .then(text => {
            return text;
        })
        .catch(err => {
            console.error('Failed to read clipboard contents: ', err);
            return null;
        });
}

Leave a Reply

Your email address will not be published. Required fields are marked *