|
async function setupButtons() { |
|
const buttons = document.querySelector(".raw-markdown-buttons") |
|
if (!buttons) return |
|
|
|
const rawUrl = buttons.getAttribute("data-raw-url") |
|
const filename = buttons.getAttribute("data-filename") |
|
if (!rawUrl || !filename) return |
|
|
|
async function getContent() { |
|
const response = await fetch(rawUrl as string) |
|
if (!response.ok) { |
|
console.error("Failed to fetch:", rawUrl) |
|
return null |
|
} |
|
return await response.text() |
|
} |
|
|
|
const copyBtn = buttons.querySelector(".raw-btn-copy") |
|
if (copyBtn) { |
|
copyBtn.addEventListener("click", async () => { |
|
const content = await getContent() |
|
if (!content) return |
|
|
|
await navigator.clipboard.writeText(content) |
|
const originalIcon = copyBtn.innerHTML |
|
copyBtn.innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6L9 17l-5-5"/></svg>` |
|
setTimeout(() => (copyBtn.innerHTML = originalIcon), 1000) |
|
}) |
|
} |
|
|
|
const downloadBtn = buttons.querySelector(".raw-btn-download") |
|
if (downloadBtn) { |
|
downloadBtn.addEventListener("click", async () => { |
|
const content = await getContent() |
|
if (!content) return |
|
|
|
const a = document.createElement("a") |
|
const blob = new Blob([content], { type: "text/markdown" }) |
|
a.href = URL.createObjectURL(blob) |
|
a.download = filename |
|
a.click() |
|
URL.revokeObjectURL(a.href) |
|
}) |
|
} |
|
|
|
const viewBtn = buttons.querySelector(".raw-btn-view") |
|
if (viewBtn) { |
|
viewBtn.addEventListener("click", async () => { |
|
const content = await getContent() |
|
if (!content) return |
|
|
|
const modal = document.createElement("div") |
|
modal.className = "raw-modal" |
|
modal.innerHTML = ` |
|
<div class="raw-modal-content"> |
|
<div class="raw-modal-header"> |
|
<span>${filename}</span> |
|
<button class="raw-modal-close">×</button> |
|
</div> |
|
<pre class="raw-modal-pre">${content.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">")}</pre> |
|
</div> |
|
` |
|
document.body.appendChild(modal) |
|
|
|
const closeModal = () => document.body.removeChild(modal) |
|
modal.querySelector(".raw-modal-close")?.addEventListener("click", closeModal) |
|
modal.addEventListener("click", (e) => { |
|
if (e.target === modal) closeModal() |
|
}) |
|
}) |
|
} |
|
} |
|
|
|
document.addEventListener("nav", setupButtons) |