Skip to content

Commit d4f45ca

Browse files
committed
Enhance clipboard functionality with fallback for unsupported environments
1 parent fbe10f3 commit d4f45ca

1 file changed

Lines changed: 39 additions & 5 deletions

File tree

src/server/public/js/main.js

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,46 @@ function showInfoToast(message, title = 'Info', duration = null) {
212212
}
213213

214214
function copyFileName(name) {
215-
navigator.clipboard.writeText(name).then(() => {
216-
showSuccessToast(name + " copied to clipboard!", "Copied!");
217-
}).catch(err => {
218-
console.error('Failed to copy text: ', err);
215+
// Try modern clipboard API first
216+
if (navigator.clipboard && window.isSecureContext) {
217+
navigator.clipboard.writeText(name).then(() => {
218+
showSuccessToast(name + " copied to clipboard!", "Copied!");
219+
}).catch(err => {
220+
console.error('Failed to copy text: ', err);
221+
fallbackCopyTextToClipboard(name);
222+
});
223+
} else {
224+
fallbackCopyTextToClipboard(name);
225+
}
226+
}
227+
228+
function fallbackCopyTextToClipboard(text) {
229+
const textArea = document.createElement("textarea");
230+
textArea.value = text;
231+
232+
// Avoid scrolling to bottom
233+
textArea.style.top = "0";
234+
textArea.style.left = "0";
235+
textArea.style.position = "fixed";
236+
textArea.style.opacity = "0";
237+
238+
document.body.appendChild(textArea);
239+
textArea.focus();
240+
textArea.select();
241+
242+
try {
243+
const successful = document.execCommand('copy');
244+
if (successful) {
245+
showSuccessToast(text + " copied to clipboard!", "Copied!");
246+
} else {
247+
showErrorToast("Failed to copy to clipboard", "Copy Error");
248+
}
249+
} catch (err) {
250+
console.error('Fallback: Could not copy text: ', err);
219251
showErrorToast("Failed to copy to clipboard", "Copy Error");
220-
});
252+
}
253+
254+
document.body.removeChild(textArea);
221255
}
222256

223257

0 commit comments

Comments
 (0)