fix: use %20 instead of + for spaces in Bilibili WBI signed requests (#126)

URLSearchParams.toString() encodes spaces as +, but Bilibili's WBI
signature verification expects %20. This mismatch causes search
queries with spaces (e.g. "亚马逊 滞销产品") to fail with
TypeError: Failed to fetch due to CORS-blocked error responses.

Fixes #125

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Chencheng LiandClaude Opus 4.6 authored and GitHub committed 2026-03-20 16:07:46 +08:00
1 parent fafa990acd
commit 4cabca12df
1 file changed
+4 -2
+4 -2
View File
@@ -60,7 +60,9 @@ export async function wbiSign(
for (const key of Object.keys(allParams).sort()) {
sorted[key] = String(allParams[key]).replace(/[!'()*]/g, '');
}
const query = new URLSearchParams(sorted).toString();
// Bilibili WBI verification expects %20 for spaces, not + (URLSearchParams default).
// Using + causes signature mismatch → CORS-blocked error response → TypeError: Failed to fetch.
const query = new URLSearchParams(sorted).toString().replace(/\+/g, '%20');
const wRid = await md5(query + mixinKey);
sorted.w_rid = wRid;
return sorted;
@@ -78,7 +80,7 @@ export async function apiGet(
}
const qs = new URLSearchParams(
Object.fromEntries(Object.entries(params).map(([k, v]) => [k, String(v)])),
);
).toString().replace(/\+/g, '%20');
const url = `${baseUrl}${path}?${qs}`;
return fetchJson(page, url);
}