Skip to content

Commit

Permalink
fix: 解决内网访问点击复制按钮失效 (GaiZhenbiao#827)
Browse files Browse the repository at this point in the history
fix GaiZhenbiao#826 .
Co-authored-by: w_xiaolizu <w_xiaolizu@kingsoft.com>
  • Loading branch information
Kilig947 authored Jul 2, 2023
1 parent 9b74535 commit 31c04be
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions assets/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,19 +373,38 @@ function addChuanhuButton(botElement) {
copyButton.classList.add('copy-bot-btn');
copyButton.setAttribute('aria-label', 'Copy');
copyButton.innerHTML = copyIcon;
copyButton.addEventListener('click', () => {
copyButton.addEventListener('click', async () => {
const textToCopy = rawMessage.innerText;
navigator.clipboard
.writeText(textToCopy)
.then(() => {

try {
if ("clipboard" in navigator) {
await navigator.clipboard.writeText(textToCopy);
copyButton.innerHTML = copiedIcon;
setTimeout(() => {
copyButton.innerHTML = copyIcon;
}, 1500);
})
.catch(() => {
console.error("copy failed");
});
} else {
const textArea = document.createElement("textarea");
textArea.value = textToCopy;

document.body.appendChild(textArea);
textArea.select();

try {
document.execCommand('copy');
copyButton.innerHTML = copiedIcon;
setTimeout(() => {
copyButton.innerHTML = copyIcon;
}, 1500);
} catch (error) {
console.error("Copy failed: ", error);
}

document.body.removeChild(textArea);
}
} catch (error) {
console.error("Copy failed: ", error);
}
});
botElement.appendChild(copyButton);

Expand Down

0 comments on commit 31c04be

Please sign in to comment.