export const copyContent = (content) => {
let copyResult = true;
const text = content || '让我们一起快乐的敲代码吧~';
if (window.navigator.clipboard) {
window.navigator.clipboard
.writeText(text)
.then((res) => {
console.log('复制成功');
return copyResult;
})
.catch((err) => {
console.log('复制失败--采取第二种复制方案', err);
copyContent2(text);
});
} else {
copyContent2(text);
}
};
export function copyContent2(text) {
let copyResult = true;
let inputDom = document.createElement('textarea');
inputDom.setAttribute('readonly', 'readonly');
inputDom.value = text;
document.body.appendChild(inputDom);
inputDom.select();
const result = document.execCommand('copy');
if (result) {
console.log('复制成功');
} else {
console.log('复制失败');
copyResult = false;
}
document.body.removeChild(inputDom);
return copyResult;
}