智能处理API路径,强兼小幻(划掉) (#31)

* 增加API路径智能检查补全,强兼小幻

* 更新小幻使用提示

* 根目录访问日志访问不打印处理日志

* 修复中文逗号

* 怎么还有句号,好神经的网页编辑

* 漏了一个日志命名
This commit is contained in:
Elegy233 authored and GitHub committed 2025-09-28 22:38:05 +08:00
1 parent 85b1bfbcb8
commit e57e434ffa
2 files changed
+40 -2

No files matched your search

+5 -1
View File
@@ -187,7 +187,11 @@ Settings > Functions > Advanced Setting > Function Region 切换为 Hong Kong,
<img src="https://i.mji.rip/2025/09/14/9fdf945fb247994518042691f60d7849.jpeg" style="width:400px" />
<img src="https://i.mji.rip/2025/09/14/dbacc0cf9c8a839f16b8960de1f38f11.jpeg" style="width:400px" />
> 注意:小幻在填写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`插件使用
+35 -1
View File
@@ -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 };
fetchMangoTV, fetchBilibili, fetchYouku, fetchOtherServer, httpGet, httpPost };