2024-02-12 23:07:56 +08:00
|
|
|
window.codeElements.forEach(code => {
|
2023-05-09 12:54:34 +08:00
|
|
|
// copy btn
|
2024-02-12 23:07:56 +08:00
|
|
|
const codeCopyBtn = document.createElement('div');
|
|
|
|
codeCopyBtn.classList.add('copy-btn');
|
|
|
|
codeCopyBtn.innerHTML = ctx.copycode.default_text;
|
|
|
|
code.appendChild(codeCopyBtn);
|
2023-05-09 12:54:34 +08:00
|
|
|
codeCopyBtn.addEventListener('click', async () => {
|
2024-02-12 23:07:56 +08:00
|
|
|
const currentCodeElement = code.children[0]?.innerText;
|
|
|
|
await copyCode(currentCodeElement);
|
|
|
|
codeCopyBtn.innerHTML = ctx.copycode.success_text;
|
|
|
|
codeCopyBtn.classList.add('success');
|
|
|
|
hud.toast(ctx.copycode.toast, 2500);
|
2023-05-09 12:54:34 +08:00
|
|
|
setTimeout(() => {
|
2024-02-12 23:07:56 +08:00
|
|
|
codeCopyBtn.innerHTML = ctx.copycode.default_text;
|
|
|
|
codeCopyBtn.classList.remove('success');
|
|
|
|
},3000);
|
2023-05-09 12:54:34 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
async function copyCode(currentCode) {
|
|
|
|
if (navigator.clipboard) {
|
|
|
|
try {
|
2024-02-12 23:07:56 +08:00
|
|
|
await navigator.clipboard.writeText(currentCode);
|
2023-12-06 13:22:07 +08:00
|
|
|
} catch (error) {
|
2023-05-09 12:54:34 +08:00
|
|
|
// 未获得用户许可
|
2024-02-12 23:07:56 +08:00
|
|
|
codeCopyBtn.innerText = '未获得用户许可';
|
|
|
|
codeCopyBtn.classList.add('warning');
|
2023-05-09 12:54:34 +08:00
|
|
|
setTimeout(() => {
|
2024-02-12 23:07:56 +08:00
|
|
|
codeCopyBtn.innerText = ctx.copycode.default_text;
|
|
|
|
codeCopyBtn.classList.remove('warning');
|
|
|
|
},3000);
|
2023-05-09 12:54:34 +08:00
|
|
|
}
|
|
|
|
} else {
|
2024-02-12 23:07:56 +08:00
|
|
|
codeCopyBtn.innerText = '当前浏览器不支持此api';
|
|
|
|
codeCopyBtn.classList.add('warning');
|
2023-05-09 12:54:34 +08:00
|
|
|
setTimeout(() => {
|
2024-02-12 23:07:56 +08:00
|
|
|
codeCopyBtn.innerText = ctx.copycode.default_text;
|
|
|
|
codeCopyBtn.classList.remove('warning');
|
|
|
|
},3000);
|
2023-05-09 12:54:34 +08:00
|
|
|
}
|
|
|
|
}
|