2024-04-01 16:44:55 +08:00
|
|
|
window.codeElements.forEach((codeElement) => {
|
|
|
|
// 创建复制按钮
|
2024-02-12 23:07:56 +08:00
|
|
|
const codeCopyBtn = document.createElement('div');
|
2024-04-01 16:44:55 +08:00
|
|
|
codeCopyBtn.className = 'copy-btn';
|
|
|
|
codeCopyBtn.textContent = ctx.copycode.default_text;
|
|
|
|
codeElement.appendChild(codeCopyBtn);
|
2023-05-09 12:54:34 +08:00
|
|
|
|
2024-04-01 16:44:55 +08:00
|
|
|
// 添加点击事件监听
|
|
|
|
codeCopyBtn.addEventListener('click', async () => {
|
2024-04-26 01:41:53 +08:00
|
|
|
const codeToCopy = codeElement.querySelector('pre')?.innerText || '';
|
2024-04-01 16:44:55 +08:00
|
|
|
if (navigator.clipboard) {
|
|
|
|
try {
|
|
|
|
await navigator.clipboard.writeText(codeToCopy);
|
|
|
|
codeCopyBtn.textContent = ctx.copycode.success_text;
|
|
|
|
codeCopyBtn.classList.add('success');
|
|
|
|
hud.toast(ctx.copycode.toast, 2500);
|
|
|
|
} catch (error) {
|
|
|
|
codeCopyBtn.textContent = '未获得用户许可';
|
|
|
|
codeCopyBtn.classList.add('warning');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
codeCopyBtn.textContent = '浏览器不支持/非HTTPS';
|
2024-02-12 23:07:56 +08:00
|
|
|
codeCopyBtn.classList.add('warning');
|
2023-05-09 12:54:34 +08:00
|
|
|
}
|
2024-04-01 16:44:55 +08:00
|
|
|
|
|
|
|
// 3秒后恢复默认文本
|
2023-05-09 12:54:34 +08:00
|
|
|
setTimeout(() => {
|
2024-04-01 16:44:55 +08:00
|
|
|
codeCopyBtn.textContent = ctx.copycode.default_text;
|
|
|
|
codeCopyBtn.classList.remove('success', 'warning');
|
|
|
|
}, 3000);
|
|
|
|
});
|
|
|
|
});
|