import { api } from "https://esm.town/v/pomdtr/api";
import { extractValInfo } from "https://esm.town/v/pomdtr/extractValInfo";
import { fetchText } from "https://esm.town/v/stevekrouse/fetchText?v=6";
import { Hono } from "npm:hono";
import { jsxRenderer } from "npm:hono/jsx-renderer";
import { minify } from "npm:terser";
const app = new Hono();
async function bookmarkletUrl(author: string, name: string) {
const resp = await fetch(`https://esm.town/v/${author}/${name}`);
const { code } = await minify(await resp.text());
return `javascript:(() => {${encodeURIComponent(code)}})()`;
}
app.get(
"/*",
jsxRenderer(({ children }) => {
return (
<html>
<head>
<title>Bookmarklets</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@1/css/pico.min.css" />
<link rel="icon" href="https://fav.farm/🔖" />
</head>
<body>
<main class="container">
{children}
</main>
</body>
</html>
);
}),
);
app.get("/", async c => {
const { data: vals } = await api(`/v1/search/vals?query=${encodeURIComponent("#bookmarklet")}`);
const bookmarklets = await Promise.all(
vals.filter(
val => val.name != "bookmarklets",
).map(async val => {
try {
const url = await bookmarkletUrl(val.author.username, val.name);
return { val, url };
} catch (error) {
return { val, error };
}
}),
);
return c.render(
<>
<h1>Bookmarklets</h1>
<table>
<thead>
<th scope="col">Bookmarklet</th>
<th scope="col">Val</th>
</thead>
<tbody>
{bookmarklets.map(({ val, url: bookmarkletUrl }) => {
const bookmarkletVal = {
label: `@${val.author.username}/${val.name}`,
url: new URL(`https://val.town/v/${val.author.username}/${val.name}`).href,
};
return (
<tr>
<td>
<a href={bookmarkletUrl}>{bookmarkletVal.label}</a>
</td>
<td>
<a href={bookmarkletVal.url}>View Val</a>
</td>
</tr>
);
})}
</tbody>
</table>
<a href="https://www.val.town/v/pomdtr/bookmarklets">Add your bookmarklet to this list</a>
</>,
);
});
app.get("/v/:author/:name", async c => {
const { author, name } = c.req.param();
const { privacy } = await api(`/v1/alias/${author}/${name}`);
if (privacy === "private") {
return c.notFound();
}
const label = `@${author}/${name}`;
const url = await bookmarkletUrl(author, name);
return c.render(
<a href={url}>{label}</a>,
);
});
export default app.fetch;