Skip to content

Commit 46d34ec

Browse files
committed
api
1 parent dfb68d7 commit 46d34ec

File tree

1 file changed

+11
-27
lines changed

1 file changed

+11
-27
lines changed

functions/api.js

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,15 @@
1-
addEventListener('fetch', event => {
2-
event.respondWith(handleRequest(event.request))
3-
})
1+
export async function onRequest(context) {
2+
const url = 'https://unpkg.com/genshin-good-words/Good-words.txt';
3+
const response = await fetch(url);
4+
const text = await response.text();
45

5-
async function handleRequest(request) {
6-
// 定义从 URL 获取内容的地址
7-
const url = 'https://genshin-good-words.pages.dev/Good-words.txt'
6+
// Split the text into lines
7+
const lines = text.split('\n').filter(line => line.trim() !== '');
88

9-
// 发起请求获取内容
10-
const response = await fetch(url)
11-
const text = await response.text()
9+
// Get a random line
10+
const randomLine = lines[Math.floor(Math.random() * lines.length)];
1211

13-
// 将内容拆分为行
14-
const lines = text.split('\n').filter(line => line.trim() !== '')
15-
16-
// 随机选择一行
17-
const randomIndex = Math.floor(Math.random() * lines.length)
18-
const randomLine = lines[randomIndex]
19-
20-
// 构建 JSON 响应
21-
const jsonResponse = {
22-
goodWord: randomLine
23-
}
24-
25-
// 返回 JSON 响应
26-
return new Response(JSON.stringify(jsonResponse), {
27-
headers: {
28-
'Content-Type': 'application/json'
29-
}
30-
})
12+
return new Response(randomLine, {
13+
headers: { 'content-type': 'text/plain' },
14+
});
3115
}

0 commit comments

Comments
 (0)