This repository has no description
1{{ define "repo/fragments/copyFileScript" }}
2<script>
3async function copyRawFile(btn, url) {
4 const label = btn.querySelector('.copy-label');
5 const spinner = btn.querySelector('.copy-spinner');
6 label.textContent = 'copying';
7 spinner.classList.remove('hidden');
8 btn.disabled = true;
9 try {
10 const res = await fetch(url);
11 if (!res.ok) throw new Error();
12 const contentType = res.headers.get('Content-Type') || '';
13 if (contentType.startsWith('image/')) {
14 const blob = await res.blob();
15 await navigator.clipboard.write([
16 new ClipboardItem({ [contentType]: blob })
17 ]);
18 } else {
19 const text = await res.text();
20 await navigator.clipboard.writeText(text);
21 }
22 spinner.classList.add('hidden');
23 label.textContent = 'copied';
24 btn.querySelector('.copy-check').classList.remove('hidden');
25 btn.disabled = false;
26 setTimeout(() => {
27 label.textContent = 'copy to clipboard';
28 btn.querySelector('.copy-check').classList.add('hidden');
29 }, 2500);
30 } catch {
31 spinner.classList.add('hidden');
32 label.textContent = 'error';
33 btn.disabled = false;
34 setTimeout(() => { label.textContent = 'copy to clipboard'; }, 2500);
35 }
36}
37</script>
38{{ end }}