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