Public
HTTP (deprecated)
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
42
43
import process from "node:process";
export async function zaobao() {
const res = await fetch(
`https://chrome.browserless.io/content?token=${process.env.browserlessKey}`,
{
method: "POST",
headers: {
"Cache-Control": "no-cache",
"Content-Type": "application/json",
},
body: JSON.stringify({
"url": "https://www.zaobao.com/realtime",
}),
},
);
const text = await res.text();
console.log(text.slice(0, 100));
const chn = text.match(/<h2>中国<\/h2>.+<h2>国际<\/h2>/)[0].match(
/realtime-time.+?<\/div>/g,
).map((m) => {
console.log(m);
const item = m.match(
/realtime-time"> (\d\d:\d\d).+">(.+)<\/a><\/span><\/div>/,
);
return { time: item[1], title: item[2] };
});
const wrd = text.match(/<h2>国际<\/h2>.+<h2>财经即时<\/h2>/)[0].match(
/realtime-time.+?<\/div>/g,
).map((m) => {
const item = m.match(
/realtime-time"> (\d\d:\d\d).+">(.+)<\/a><\/span><\/div>/,
);
return { time: item[1], title: item[2] };
});
// return { "中国": chn, "国际": wrd };
return new Response(
`<html><head><style>*{margin:0;padding:0;}body{padding:1em;}h3{padding:20px 0}p{border-bottom:1px solid #f0f0f0;padding: 20px 0;}span{color: #787878;}</style></head>
<body><h3>中国</h3>${chn.map((m) => `<p><span>${m.time}</span> ${m.title}</p>`).join("")}
<h3>国际</h3>${wrd.map((m) => `<p><span>${m.time}</span> ${m.title}</p>`).join("")}</body></html>`,
{ headers: { "Content-Type": "text/html" } },
);
}
baj-zaobao.web.val.run
November 9, 2023