From 758ec29efaa98f9472fea1ccf99bcefea2126663 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: Tue, 26 May 2020 13:25:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8Images.weserv.nl=E7=9A=84?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E4=B8=AD=E8=BD=AC=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +++- cfworker_proxy.js | 57 ++++++++++++++++++++++++++++++++++++----------- index.html | 1 + 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 4ea594e..2638341 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,9 @@ 由于自己的服务器用了 Cloudflare 的免费 CDN,然而最近分配到的 IPv4 地址被墙了,所以后端(在墙内)不能用了…… -于是试着用 Cloudflare Worker 写了个简单的代理(参见文件 `cfworker_proxy.js`)解决之(〃′▽`) +于是试着用 Cloudflare Worker 写了个~~简单的代理~~(参见文件 `cfworker_proxy.js`)解决之(〃′▽`)~~ + +直接使用 [Images.weserv.nl](https://images.weserv.nl/) 的服务中转图片,就不需要部署后端了! #### 2019.12.5 diff --git a/cfworker_proxy.js b/cfworker_proxy.js index e8d49b0..90adcd5 100644 --- a/cfworker_proxy.js +++ b/cfworker_proxy.js @@ -7,26 +7,57 @@ addEventListener('fetch', event => { * @param {Request} request */ async function handleRequest(request) { - let responseConfig = { + const result = { + success: false, + title: '', + cover: '', + }; + const responseConfig = { status: 200, headers: { 'Access-Control-Allow-Origin': 'https://akarin.dev', 'Content-Type': 'application/json', }, } - let articleURL = new URL(request.url).searchParams.get('url'); + const articleURL = new URL(request.url).searchParams.get('url'); - if (!articleURL || !articleURL.startsWith('https://mp.weixin.qq.com')) { - return new Response('{"success":false}', responseConfig); - } + try { + if (!articleURL || !articleURL.startsWith('https://mp.weixin.qq.com')) throw new Error('Invalid URL'); + + const articleContent = await ( + await fetch(articleURL) + ).text(); + + const match = articleContent.match( + /var msg_title = \'(?[\S\s]*?)\'.html\(false\);[\S\s]*?var msg_cdn_url = "(?<cover>[\S\s]*?)";/ + ); + if (!match) throw new Error('Unable to match content'); - let requestURL = new URL('https://i.akarin.dev/misc/get_article_info.php'); - requestURL.searchParams.set('url', articleURL); + result.title = match.groups.title; + for (const [k, v] of Object.entries({ + '`': '`', + ''': '\'', + '"': '"', + ' ': ' ', + '>': '>', + '<': '<', + '¥': '¥', + })) result.title = result.title.replace(new RegExp(k, 'g'), v); - return new Response( - await ( - await fetch(requestURL.toString()) - ).text(), - responseConfig - ); + result.cover = new URL('https://images.weserv.nl'); + for (const [k, v] of Object.entries({ + url: match.groups.cover, + il: '', + we: '', + h: 360, + q: 70, + })) result.cover.searchParams.set(k, v); + result.cover = result.cover.toString(); + + result.success = true; + } catch (error) { + console.log(error); + } + + return new Response(JSON.stringify(result), responseConfig); } \ No newline at end of file diff --git a/index.html b/index.html index 71dbf18..7d15aff 100644 --- a/index.html +++ b/index.html @@ -780,6 +780,7 @@ <h4>开源就是好</h4> { modal: true, closeOnEsc: false, + history: false, } );