-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
51 lines (50 loc) · 1.74 KB
/
index.js
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
44
45
46
47
48
49
50
51
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
const url = new URL(request.url);
const isRedirect = url.searchParams.has("redirect");
if (url.searchParams.get("token") !== UNI_TOKEN) {
return new Response("Bad Token", {
status: 404
});
}
// Post
if (request.method === "POST") {
const getBody = async () => {
const contentType = request.headers.get("content-type") || "";
if (contentType.includes("application/json")) {
const json = await request.json();
return json.value ?? json;
}
return request.text();
};
const body = await getBody();
await UNI_CLIPBOARD.put("clipboard", body, { expirationTtl: 60 });
return new Response("Post Clipboard", {
headers: { "content-type": "text/plain; charset=utf-8" }
});
} else if (request.method === "GET") {
// Get
const clipboard = await UNI_CLIPBOARD.get("clipboard");
const isHTTP = clipboard?.startsWith("http");
if (clipboard) {
if (isRedirect && isHTTP) {
return new Response(null, {
status: 302,
headers: {
Location: `${clipboard}`
}
});
}
return new Response(clipboard, {
headers: { "content-type": "text/plain; charset=utf-8" }
});
} else {
return new Response("Not Found", {
headers: { "content-type": "text/plain; charset=utf-8" },
status: 404
});
}
}
}