From e57e434ffa0490d3b4f96f1244916c427c98938b Mon Sep 17 00:00:00 2001
From: Elegy233 <82034747+wan0ge@users.noreply.github.com>
Date: Sun, 28 Sep 2025 22:38:05 +0800
Subject: [PATCH] =?UTF-8?q?=E6=99=BA=E8=83=BD=E5=A4=84=E7=90=86API?=
=?UTF-8?q?=E8=B7=AF=E5=BE=84=EF=BC=8C=E5=BC=BA=E5=85=BC=E5=B0=8F=E5=B9=BB?=
=?UTF-8?q?=EF=BC=88=E5=88=92=E6=8E=89=EF=BC=89=20(#31)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
* 增加API路径智能检查补全,强兼小幻
* 更新小幻使用提示
* 根目录访问日志访问不打印处理日志
* 修复中文逗号
* 怎么还有句号,好神经的网页编辑
* 漏了一个日志命名
---
README.md | 6 +++++-
danmu_api/worker.js | 36 +++++++++++++++++++++++++++++++++++-
2 files changed, 40 insertions(+), 2 deletions(-)
diff --git a/README.md b/README.md
index bb76c59..98063f0 100644
--- a/README.md
+++ b/README.md
@@ -187,7 +187,11 @@ Settings > Functions > Advanced Setting > Function Region 切换为 Hong Kong,
-> 注意:小幻在填写API的时候需要在API后面加上`/api/v2`,如`http://192.168.1.7:9321/87654321/api/v2`
+> 注意:
+>
+> ~~小幻在填写API的时候需要在API后面加上`/api/v2`,如`http://192.168.1.7:9321/87654321/api/v2`~~
+>
+> (已对小幻做兼容,`/api/v2`可加可不加都可以正确处理)
>
> 有很多人问FW能不能用,FW推荐直接使用插件,如果非要使用,则可以配合 `https://raw.githubusercontent.com/huangxd-/ForwardWidgets/refs/heads/main/widgets.fwd` 里的`danmu_api`插件使用
diff --git a/danmu_api/worker.js b/danmu_api/worker.js
index b26536c..e55c4d2 100644
--- a/danmu_api/worker.js
+++ b/danmu_api/worker.js
@@ -3400,6 +3400,40 @@ async function handleRequest(req, env) {
log("log", path);
+ // 智能处理API路径前缀,确保最终有一个正确的 /api/v2
+ if (path !== "/" && path !== "/api/logs") {
+ log('log', `[Path Check] Starting path normalization for: "${path}"`);
+ const pathBeforeCleanup = path; // 保存清理前的路径检查是否修改
+
+ // 1. 清理:应对“用户填写/api/v2”+“客户端添加/api/v2”导致的重复前缀
+ while (path.startsWith('/api/v2/api/v2/')) {
+ log('log', `[Path Check] Found redundant /api/v2 prefix. Cleaning...`);
+ // 从第二个 /api/v2 的位置开始截取,相当于移除第一个
+ path = path.substring('/api/v2'.length);
+ }
+
+ // 打印日志:只有在发生清理时才显示清理后的路径,否则显示“无需清理”
+ if (path !== pathBeforeCleanup) {
+ log('log', `[Path Check] Path after cleanup: "${path}"`);
+ } else {
+ log('log', `[Path Check] Path after cleanup: No cleanup needed.`);
+ }
+
+ // 2. 补全:如果路径缺少前缀(例如请求原始路径为 /search/anime),则补全
+ const pathBeforePrefixCheck = path;
+ if (!path.startsWith('/api/v2') && path !== '/' && !path.startsWith('/api/logs')) {
+ log('log', `[Path Check] Path is missing /api/v2 prefix. Adding...`);
+ path = '/api/v2' + path;
+ }
+
+ // 打印日志:只有在发生添加前缀时才显示添加后的路径,否则显示“无需补全”
+ if (path === pathBeforePrefixCheck) {
+ log('log', `[Path Check] Prefix Check: No prefix addition needed.`);
+ }
+
+ log('log', `[Path Check] Final normalized path: "${path}"`);
+ }
+
// GET /
if (path === "/" && method === "GET") {
return handleHomepage();
@@ -3474,4 +3508,4 @@ export async function vercelHandler(req, res) {
// 为了测试导出 handleRequest
export { handleRequest, searchAnime, searchEpisodes, matchAnime, getBangumi, getComment, fetchTencentVideo, fetchIqiyi,
- fetchMangoTV, fetchBilibili, fetchYouku, fetchOtherServer, httpGet, httpPost };
\ No newline at end of file
+ fetchMangoTV, fetchBilibili, fetchYouku, fetchOtherServer, httpGet, httpPost };