From aadb80e149d8e42b77de2464c1795ee4e99f4905 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 27 Feb 2026 17:05:00 +0000 Subject: [PATCH 1/2] Initial plan From c11e6cc1c60d364246f84e306f72456f00299f94 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 27 Feb 2026 17:07:24 +0000 Subject: [PATCH 2/2] Add CORS response headers to random API endpoint Co-authored-by: MarSeventh <108160987+MarSeventh@users.noreply.github.com> --- functions/random/index.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/functions/random/index.js b/functions/random/index.js index b5c65444..6a57b1b0 100644 --- a/functions/random/index.js +++ b/functions/random/index.js @@ -2,6 +2,14 @@ import { fetchOthersConfig } from "../utils/sysConfig"; import { readIndex } from "../utils/indexManager"; import { detectDevice, resolveOrientation, addClientHintsHeaders } from "./adaptive.js"; +// CORS 跨域响应头 +const corsHeaders = { + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'GET, OPTIONS', + 'Access-Control-Allow-Headers': 'Content-Type', + 'Access-Control-Max-Age': '86400', +}; + let othersConfig = {}; let allowRandom = false; @@ -17,6 +25,11 @@ export async function onRequest(context) { } = context; const requestUrl = new URL(request.url); + // 处理 OPTIONS 预检请求 + if (request.method === 'OPTIONS') { + return new Response(null, { headers: corsHeaders }); + } + // 读取其他设置 othersConfig = await fetchOthersConfig(env); allowRandom = othersConfig.randomImageAPI.enabled; @@ -24,7 +37,7 @@ export async function onRequest(context) { // 检查是否启用了随机图功能 if (allowRandom != true) { - return new Response(JSON.stringify({ error: "Random is disabled" }), { status: 403 }); + return new Response(JSON.stringify({ error: "Random is disabled" }), { status: 403, headers: corsHeaders }); } // 处理允许的目录,每个目录调整为标准格式,去掉首尾空格,去掉开头的/,替换多个连续的/为单个/,去掉末尾的/ @@ -73,7 +86,7 @@ export async function onRequest(context) { } } if (!dirAllowed) { - return new Response(JSON.stringify({ error: "Directory not allowed" }), { status: 403 }); + return new Response(JSON.stringify({ error: "Directory not allowed" }), { status: 403, headers: corsHeaders }); } // 调用randomFileList接口,读取KV数据库中的所有记录 @@ -111,8 +124,8 @@ export async function onRequest(context) { allRecords = allRecordsBeforeOrientationFilter; } - // 构建响应头:自适应模式下添加 Client Hints 协商头 - const responseHeaders = new Headers(); + // 构建响应头:添加 CORS 跨域响应头,自适应模式下添加 Client Hints 协商头 + const responseHeaders = new Headers(corsHeaders); if (isAutoMode) { addClientHintsHeaders(responseHeaders); }