/** * Copies text to the clipboard using the textarea. * @param {string} text - Text to copy. * @returns {Promise} Promise. */ function textAreaCopy(text) { const textArea = document.createElement('textarea'); textArea.value = text; document.body.appendChild(textArea); textArea.focus(); textArea.select(); let ret; try { if (document.execCommand('copy')) { ret = Promise.resolve(); } else { ret = Promise.reject(); } } catch (_) { ret = Promise.reject(); } document.body.removeChild(textArea); return ret; } /** * Copies text to the clipboard. * @param {string} text - Text to copy. * @returns {Promise} Promise. */ export function copy(text) { /* eslint-disable-next-line compat/compat */ if (navigator.clipboard === undefined) { return textAreaCopy(text); } else { /* eslint-disable-next-line compat/compat */ return navigator.clipboard.writeText(text).catch(() => { return textAreaCopy(text); }); } }