{{ define "repo/fragments/copyFileScript" }}
<script>
async function copyRawFile(btn, url) {
    const icon = btn.querySelector('.copy-icon');
    const spinner = btn.querySelector('.copy-spinner');
    const check = btn.querySelector('.copy-check');

    icon.classList.add('hidden');
    spinner.classList.remove('hidden');
    btn.disabled = true;

    try {
        const res = await fetch(url);
        if (!res.ok) throw new Error();
        const contentType = res.headers.get('Content-Type') || '';
        if (contentType.startsWith('image/')) {
            const blob = await res.blob();
            await navigator.clipboard.write([
                new ClipboardItem({ [contentType]: blob })
            ]);
        } else {
            const text = await res.text();
            await navigator.clipboard.writeText(text);
        }
        spinner.classList.add('hidden');
        check.classList.remove('hidden');
        btn.disabled = false;
        setTimeout(() => {
            check.classList.add('hidden');
            icon.classList.remove('hidden');
        }, 2000);
    } catch {
        spinner.classList.add('hidden');
        icon.classList.remove('hidden');
        btn.disabled = false;
    }
}
</script>
{{ end }}
