From 06c61abb121912c40a6d774ac41b907be86b106e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=9C=A8=E5=B0=8F=E9=80=8F=E6=98=8E=E3=83=BB=E5=AE=B8?= =?UTF-8?q?=E2=9C=A8?= <1272742391@qq.com> Date: Sat, 29 May 2021 00:04:35 +0800 Subject: [PATCH] =?UTF-8?q?CORS=E7=9A=84=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- cfworker_proxy.js | 22 ++++++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b15aa7e..9ccd792 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ > > 以下网站二次部署时抹去了原作者相关信息,原作者对此表示非常遗憾,在此列出来批判一番: > -> * h**ps://www.41661.com/ 甚至还替换了赞赏的收款码 +> * h**ps://www(dot)41661(dot)com/ 甚至还替换了赞赏的收款码 --- diff --git a/cfworker_proxy.js b/cfworker_proxy.js index 90adcd5..96dfa79 100644 --- a/cfworker_proxy.js +++ b/cfworker_proxy.js @@ -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 = \'(?