File tree 1 file changed +11
-27
lines changed
1 file changed +11
-27
lines changed Original file line number Diff line number Diff line change 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 ( ) ;
4
5
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 ( ) !== '' ) ;
8
8
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 ) ] ;
12
11
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
+ } ) ;
31
15
}
You can’t perform that action at this time.
0 commit comments