CORS的修改

This commit is contained in:
✨小透明・宸✨
2021-05-29 00:04:35 +08:00
parent 8d032667d3
commit 06c61abb12
2 changed files with 17 additions and 7 deletions

View File

@@ -22,7 +22,7 @@
>
> 以下网站二次部署时抹去了原作者相关信息,原作者对此表示非常遗憾,在此列出来批判一番:
>
> * h**ps://www.41661.com/ 甚至还替换了赞赏的收款码
> * h**ps://www(dot)41661(dot)com/ 甚至还替换了赞赏的收款码
---

View File

@@ -1,5 +1,18 @@
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
let response = handleRequest(event.request);
const origin = event.request.headers.get('Origin');
if ([
'http://localhost',
'https://akarin.dev',
'https://transparentlc.github.io',
// Add other mirrors
].includes(origin)) {
response = response.then(e => {
e.headers.set('Access-Control-Allow-Origin', origin);
return e;
});
}
event.respondWith(response);
});
/**
@@ -15,7 +28,6 @@ async function handleRequest(request) {
const responseConfig = {
status: 200,
headers: {
'Access-Control-Allow-Origin': 'https://akarin.dev',
'Content-Type': 'application/json',
},
}
@@ -24,9 +36,7 @@ async function handleRequest(request) {
try {
if (!articleURL || !articleURL.startsWith('https://mp.weixin.qq.com')) throw new Error('Invalid URL');
const articleContent = await (
await fetch(articleURL)
).text();
const articleContent = await fetch(articleURL).then(res => res.text());
const match = articleContent.match(
/var msg_title = \'(?<title>[\S\s]*?)\'.html\(false\);[\S\s]*?var msg_cdn_url = "(?<cover>[\S\s]*?)";/
@@ -58,6 +68,6 @@ async function handleRequest(request) {
} catch (error) {
console.log(error);
}
return new Response(JSON.stringify(result), responseConfig);
}