Skip to content

Commit b323d3c

Browse files
authored
Improve clipboard javascript (#512)
* improve clipboard javascript * improve code of copyclipboard
1 parent 9e172d6 commit b323d3c

1 file changed

Lines changed: 28 additions & 4 deletions

File tree

src/_includes/bio.njk

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,41 @@ layout: layouts/base.njk
104104

105105
<script>
106106
function copyToClipboard(text, el) {
107-
navigator.clipboard.writeText(text);
108107
const span = el.querySelector('span:last-child');
109-
span.innerText = 'Copied!';
110-
setTimeout(() => span.innerText = 'Copy', 2000);
108+
const showSuccess = () => {
109+
if (span) {
110+
span.innerText = 'Copied!';
111+
clearTimeout(el.copyTimeout);
112+
el.copyTimeout = setTimeout(() => { span.innerText = 'Copy'; }, 2000);
113+
}
114+
};
115+
116+
if (!navigator.clipboard) {
117+
const ta = document.createElement('textarea');
118+
ta.value = text;
119+
ta.style.position = 'fixed';
120+
ta.style.opacity = '0';
121+
document.body.appendChild(ta);
122+
ta.select();
123+
try {
124+
document.execCommand('copy');
125+
showSuccess();
126+
} catch (err) {
127+
console.error('Fallback copy failed:', err);
128+
}
129+
document.body.removeChild(ta);
130+
return;
131+
}
132+
133+
navigator.clipboard.writeText(text)
134+
.then(showSuccess)
135+
.catch(err => console.error('Failed to copy:', err));
111136
}
112137
113138
document.querySelector('.copy-email-btn')?.addEventListener('click', function() {
114139
copyToClipboard(this.getAttribute('data-email'), this);
115140
});
116141
117-
// Pass profile data to game modules
118142
const profileData = document.getElementById('profile-data');
119143
window.PROFILE_NAME = profileData ? profileData.dataset.name : "";
120144
window.PROFILE_SKILLS = profileData && profileData.dataset.skills ? profileData.dataset.skills.trim().split(/\s+/).filter(Boolean) : [];

0 commit comments

Comments
 (0)