Public
Script
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { fetch } from "https://esm.town/v/std/fetch";
import { email as email2 } from "https://esm.town/v/std/email?v=9";
import { set } from "https://esm.town/v/std/set?v=11";
import { persistantValues } from "https://esm.town/v/kajgod/persistantValues";
export async function findTopNews(
url: string,
className: string,
offset: number,
email: (title: string) => string,
subject: (title: string) => string,
dryRun = false,
) {
let oldTitle = persistantValues[url] || "";
const v = await fetch(url);
const txt = await v.text();
const start = txt.indexOf(className);
const title = txt.substring(start + offset, start + offset + 50).replace(
/<\/?[^>]+(>|$)/g,
"",
);
if (dryRun) {
console.log(title, oldTitle, email(title), subject(title));
persistantValues[url] = title;
await set(
"persistantValues",
persistantValues,
);
return -1;
}
if (title !== oldTitle) {
await email2({ text: email(title), subject: subject(title) });
persistantValues[url] = title;
await set(
"persistantValues",
persistantValues,
);
return 1;
}
return 0;
}
October 23, 2023