forked from TransparentLC/WechatMomentScreenshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfworker_proxy.js
80 lines (70 loc) · 2.72 KB
/
cfworker_proxy.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
addEventListener('fetch', event => event.respondWith(handleRequest(event.request)));
/**
* Respond to the request
* @param {Request} request
*/
async function handleRequest(request) {
const result = {
success: false,
title: '',
cover: '',
author: '',
};
const origin = request.headers.get('origin') || '';
const responseConfig = {
status: 200,
headers: {
'Content-Type': 'application/json',
},
}
if (origin && (origin.startsWith('https://') || origin.startsWith('http://'))) {
responseConfig.headers['Access-Control-Allow-Origin'] = origin;
}
const articleURL = new URL(request.url).searchParams.get('url');
try {
if (!articleURL || !articleURL.startsWith('https://mp.weixin.qq.com')) throw new Error('Invalid URL');
const articleContent = await fetch(articleURL).then(r => r.text());
let match = articleContent.match(
/var nickname = htmlDecode\("(?<author>[\S\s]*?)"\);[\S\s]*?var msg_title = \'(?<title>[\S\s]*?)\'.html\(false\);[\S\s]*?var msg_cdn_url = "(?<cover>[\S\s]*?)";/
);
if (!match) {
// Test case:
// https://mp.weixin.qq.com/s/_F1sw9yrzMfpARzvHaG_MA
// https://mp.weixin.qq.com/s/HpfdeLrEkXwlEzSP2yzN1A
match = articleContent.match(
/d\.title = xml \? getXmlValue\('title\.DATA'\) : '(?<title>.*?)';[\S\s]*?d\.nick_name = xml \? getXmlValue\('nick_name\.DATA'\) : '(?<author>.*?)';[\S\s]*?d\.cdn_1_1_img = xml \? getXmlValue\('cdn_url_1_1\.DATA'\) : '(?<cover>.*?)';/
);
}
if (!match) throw new Error('Unable to match content');
result.title = match.groups.title;
result.author = match.groups.author;
for (const [k, v] of Object.entries({
'`': '`',
''': '\'',
'"': '"',
' ': ' ',
'>': '>',
'<': '<',
'¥': '¥',
'&': '&',
})) {
result.title = result.title.replace(new RegExp(k, 'g'), v);
result.author = result.author.replace(new RegExp(k, 'g'), v);
}
const u = new URL('https://images.weserv.nl/');
for (const [k, v] of Object.entries({
url: match.groups.cover,
il: '',
we: '',
h: 360,
q: 70,
encoding: 'base64',
})) u.searchParams.set(k, v.toString());
result.cover = await fetch(u.toString()).then(r => r.text());
result.success = true;
console.log(articleURL);
} catch (error) {
console.log(error);
}
return new Response(JSON.stringify(result), responseConfig);
}