1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/scripts/clipboard.js

46 lines
1.1 KiB
JavaScript
Raw Normal View History

/**
* Copies text to the clipboard using the textarea.
* @param {string} text - Text to copy.
* @returns {Promise<void>} 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<void>} 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);
});
}
}