-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
27 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,32 @@ | ||
window.codeElements.forEach(code => { | ||
// copy btn | ||
window.codeElements.forEach((codeElement) => { | ||
// 创建复制按钮 | ||
const codeCopyBtn = document.createElement('div'); | ||
codeCopyBtn.classList.add('copy-btn'); | ||
codeCopyBtn.innerHTML = ctx.copycode.default_text; | ||
code.appendChild(codeCopyBtn); | ||
codeCopyBtn.addEventListener('click', async () => { | ||
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); | ||
setTimeout(() => { | ||
codeCopyBtn.innerHTML = ctx.copycode.default_text; | ||
codeCopyBtn.classList.remove('success'); | ||
},3000); | ||
}) | ||
}) | ||
codeCopyBtn.className = 'copy-btn'; | ||
codeCopyBtn.textContent = ctx.copycode.default_text; | ||
codeElement.appendChild(codeCopyBtn); | ||
|
||
async function copyCode(currentCode) { | ||
if (navigator.clipboard) { | ||
try { | ||
await navigator.clipboard.writeText(currentCode); | ||
} catch (error) { | ||
// 未获得用户许可 | ||
codeCopyBtn.innerText = '未获得用户许可'; | ||
// 添加点击事件监听 | ||
codeCopyBtn.addEventListener('click', async () => { | ||
const codeToCopy = codeElement.querySelector('code')?.textContent || ''; | ||
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'; | ||
codeCopyBtn.classList.add('warning'); | ||
setTimeout(() => { | ||
codeCopyBtn.innerText = ctx.copycode.default_text; | ||
codeCopyBtn.classList.remove('warning'); | ||
},3000); | ||
} | ||
} else { | ||
codeCopyBtn.innerText = '当前浏览器不支持此api'; | ||
codeCopyBtn.classList.add('warning'); | ||
|
||
// 3秒后恢复默认文本 | ||
setTimeout(() => { | ||
codeCopyBtn.innerText = ctx.copycode.default_text; | ||
codeCopyBtn.classList.remove('warning'); | ||
},3000); | ||
} | ||
} | ||
codeCopyBtn.textContent = ctx.copycode.default_text; | ||
codeCopyBtn.classList.remove('success', 'warning'); | ||
}, 3000); | ||
}); | ||
}); |