复制到剪贴板
// 复制到剪贴板
export const copyText = (value) => {
return new Promise((resolve, reject) => {
try {
if (window.clipboardData) {
window.clipboardData.setData("Text", ${value || ''}
);
if (window.clipboardData.getData('text/plain') == ${value || ''}
|| window.clipboardData.getData('Text') == ${value || ''}
) {
resolve(true);
}else{
reject(false)
}
} else {
let copyInput = document.getElementsByClassName("copyInput")[0];
if (!copyInput) {
copyInput = document.createElement('textarea');
copyInput.value = ${value || ''}
;
document.body.appendChild(copyInput);
copyInput.select(); // 选择对象
document.execCommand("Copy"); // 执行浏览器复制命令
copyInput.className = 'copyInput';
copyInput.style.display = 'none';
} else {
copyInput.remove();
let newCopyInput = document.createElement('textarea');
newCopyInput.value = ${value || ''}
;
document.body.appendChild(newCopyInput);
newCopyInput.select(); // 选择对象
document.execCommand("Copy"); // 执行浏览器复制命令
newCopyInput.className = 'copyInput';
newCopyInput.style.display = 'none';
}
resolve(true);
}
} catch (err) {
reject(err)
}
})
}