Última actividad 1726714294

利用cf worker实现简单截图api

LiuShen's Avatar LiuShen revisó este gist 1726714294. Ir a la revisión

1 file changed, 94 insertions

liushen-api.js(archivo creado)

@@ -0,0 +1,94 @@
1 + import HOME_HTML from './home.html';
2 +
3 + import SCREENSHOT_HTML from './screenshot.html';
4 +
5 + export default {
6 + async fetch(request, env, ctx) {
7 + // 获取请求的 URL 和路径
8 + const url = new URL(request.url);
9 + const path = url.pathname;
10 +
11 + // 路由不同的 API 接口
12 + if (path.startsWith('/api/screenshot')) {
13 + return handleScreenshotRequest(url);
14 + }
15 +
16 + // 处理主页的请求
17 + if (path === '/') {
18 + return new Response(HOME_HTML, {
19 + status: 200,
20 + headers: {
21 + "content-type": "text/html"
22 + }
23 + });
24 + }
25 +
26 + if (path === '/screenshot') {
27 + return new Response(SCREENSHOT_HTML, {
28 + status: 200,
29 + headers: {
30 + "content-type": "text/html"
31 + }
32 + });
33 + }
34 +
35 + return new Response('Invalid API endpoint', { status: 404 });
36 + }
37 + }
38 +
39 + // 处理 /api/screenshot 请求
40 + async function handleScreenshotRequest(url) {
41 + const targetUrl = url.searchParams.get('url') || 'https://www.liushen.fun/';
42 + const width = url.searchParams.get('width') || 400;
43 + const height = url.searchParams.get('height') || 250;
44 + const method = url.searchParams.get('method') || '1'; // 默认为 mshot 方法
45 +
46 + console.log(targetUrl, width, height, method);
47 +
48 + // 构造第一个截图 API 请求地址 (mshots)
49 + const screenshotUrl1 = `https://s0.wp.com/mshots/v1/${encodeURIComponent(targetUrl)}?w=${width}&h=${height}`;
50 + // 构造第二个截图 API 请求地址 (urlscan)
51 + const screenshotUrl2 = `https://urlscan.io/liveshot/?width=${width}&height=${height}&url=${encodeURIComponent(targetUrl)}`;
52 +
53 + try {
54 + let response;
55 +
56 + // 根据 method 参数选择首选的截图方式
57 + if (method === '1') {
58 + // 请求 mshot
59 + response = await fetchWithUA(screenshotUrl1);
60 + if (!response.ok) {
61 + console.log("mshots failed, trying urlscan");
62 + response = await fetchWithUA(screenshotUrl2); // 如果 mshots 失败,尝试 urlscan
63 + }
64 + } else {
65 + // 请求 urlscan
66 + response = await fetchWithUA(screenshotUrl2);
67 + if (!response.ok) {
68 + console.log("urlscan failed, trying mshots");
69 + response = await fetchWithUA(screenshotUrl1); // 如果 urlscan 失败,尝试 mshots
70 + }
71 + }
72 +
73 + // 如果两个 API 都失败,返回 403 错误
74 + if (!response.ok) {
75 + console.log("Both APIs failed, returning 403");
76 + return new Response("Both screenshot APIs failed.", { status: 403 });
77 + }
78 +
79 + return new Response(response.body, response);
80 + } catch (error) {
81 + // 捕获异常并返回 403 错误
82 + console.log("Error occurred:", error);
83 + return new Response("Error occurred while processing the request.", { status: 403 });
84 + }
85 + }
86 +
87 + // 添加带有 User-Agent 的 fetch 方法
88 + async function fetchWithUA(url) {
89 + return await fetch(url, {
90 + headers: {
91 + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36'
92 + }
93 + });
94 + }
Siguiente Anterior