| 123456789101112131415161718192021222324252627282930 |
- export default function (content){
- return new Promise((resolve,reject)=>{
- // 判断是否支持 clipboard api 复制
- try {
- if (navigator.clipboard) {
- navigator.clipboard.writeText(content).then(resolve).catch(reject);
- } else {
- let textarea = document.createElement('textarea');
- // 隐藏此输入框
- textarea.style.position = 'fixed';
- textarea.style.opacity = '0';
- textarea.style.top = '20px';
- document.body.appendChild(textarea);
- // 赋值
- textarea.value = content;
- // 选中
- textarea.select();
- // 复制
- document.execCommand('copy', true);
- // 移除输入框
- document.body.removeChild(textarea);
- }
- resolve('ok');
- } catch (error) {
- reject(error);
- }
- })
- }
|