Update cfworker_proxy.js
This commit is contained in:
@@ -1,19 +1,4 @@
|
||||
addEventListener('fetch', event => {
|
||||
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);
|
||||
});
|
||||
addEventListener('fetch', event => event.respondWith(handleRequest(event.request)));
|
||||
|
||||
/**
|
||||
* Respond to the request
|
||||
@@ -24,26 +9,42 @@ async function handleRequest(request) {
|
||||
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(res => res.text());
|
||||
const articleContent = await fetch(articleURL).then(r => r.text());
|
||||
|
||||
const match = articleContent.match(
|
||||
/var msg_title = \'(?<title>[\S\s]*?)\'.html\(false\);[\S\s]*?var msg_cdn_url = "(?<cover>[\S\s]*?)";/
|
||||
let match = articleContent.match(
|
||||
/var nickname = "(?<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({
|
||||
'`': '`',
|
||||
''': '\'',
|
||||
@@ -53,19 +54,24 @@ async function handleRequest(request) {
|
||||
'<': '<',
|
||||
'¥': '¥',
|
||||
'&': '&',
|
||||
})) result.title = result.title.replace(new RegExp(k, 'g'), v);
|
||||
})) {
|
||||
result.title = result.title.replace(new RegExp(k, 'g'), v);
|
||||
result.author = result.author.replace(new RegExp(k, 'g'), v);
|
||||
}
|
||||
|
||||
result.cover = new URL('https://images.weserv.nl');
|
||||
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,
|
||||
})) result.cover.searchParams.set(k, v);
|
||||
result.cover = result.cover.toString();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user