-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcf.js
More file actions
38 lines (32 loc) · 1.21 KB
/
Copy pathcf.js
File metadata and controls
38 lines (32 loc) · 1.21 KB
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
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
const url = 'https://raw.githubusercontent.com/EhTagTranslation/DatabaseReleases/refs/heads/master/db.text.json';
const kvKey = 'tag'; // KV 存储的键
// 尝试从 KV 中获取数据
let processedContent = await tag.get(kvKey);
// 如果 KV 中没有数据,则从 URL 获取并处理数据
if (!processedContent) {
const response = await fetch(url);
const content = await response.text();
// 进行文本处理
processedContent = content
.replace(/^.+(.{85}"parody")/, '$1')
.replace(/\{"namespace":"([^"]+).*?"data"../g, '"$1,')
.replace(/,"intro":"[^"]*","links":"[^"]*"/g, '')
.replace(/..\{"name":"([^"]+)"\}/g, '│$1')
.replace(/,"/g, '\n')
.replace(/..\{"name":"(.+?).\n.+\n.+/g, '│$1')
.replace(/\\/g, '')
.replace(/(\}|])\}/g, '');
// 将处理后的数据存储到 KV 中,设置过期时间为一周
await tag.put(kvKey, processedContent, { expirationTtl: 604800 });
}
// 返回处理后的内容
return new Response(processedContent, {
headers: {
'Content-Type': 'text/plain'
}
});
}