1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { diffHtml } from "https://esm.town/v/chet/diffHtml";
import { blob } from "https://esm.town/v/std/blob?v=11";
import { email } from "https://esm.town/v/std/email?v=11";
import { fetch } from "https://esm.town/v/std/fetch";
export async function watchWebsite(url: string) {
const newHtml = await fetch(url).then(r => r.text());
const key = "watch:" + url;
let oldHtml = "";
try {
oldHtml = await blob.get(key).then(r => r.text());
} catch (error) {}
await blob.set(key, newHtml);
if (!oldHtml) return console.log("NO OLD", { oldHtml, newHtml });
const diff = diffHtml(oldHtml, newHtml);
if (!diff) return console.log("NO DIFF", { oldHtml, newHtml });
console.log("DIFF", diff);
email({ subject: `Diff for ${url}`, text: diff });
}
๐Ÿ‘† This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data โ€” all from the browser.