Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions docs/old-code-backup/js-cookie-monitor-debugger-hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,20 @@
return new CookiePair(key, value, expires ? new Date(expires).getTime() : null)
}

/**
* 安全地解码 URI 组件
* @param str
* @returns {string}
*/
function safeDecode(str) {
try {
return decodeURIComponent(str);
} catch (e) {
// 出错就原样返回,避免 URI malformed
return str;
}
}

/**
* 把按照等号=拼接的key、value字符串切分开
* @param s
Expand All @@ -332,11 +346,11 @@
const keyValueArray = (s || "").split("=");

if (keyValueArray.length) {
key = decodeURIComponent(keyValueArray[0].trim());
key = safeDecode(keyValueArray[0].trim());
}

if (keyValueArray.length > 1) {
value = decodeURIComponent(keyValueArray.slice(1).join("=").trim());
value = safeDecode(keyValueArray.slice(1).join("=").trim());
}

return {
Expand Down
18 changes: 16 additions & 2 deletions src/utils/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ export function genFormatArray(messageAndStyleArray: string[]): string {
return formatArray.join("");
}

/**
* 安全地解码 URI 组件
* @param str
* @returns 解码后的字符串
*/
function safeDecode(str: string) {
try {
return decodeURIComponent(str);
} catch (e) {
// 出错就原样返回,避免 URI malformed
return str;
}
}

/**
* 把按照等号=拼接的key、value字符串切分开
* @param s 包含键值对的字符串
Expand All @@ -23,11 +37,11 @@ export function splitKeyValue(s: string): KeyValuePair {
const keyValueArray = (s || "").split("=");

if (keyValueArray.length) {
key = decodeURIComponent(keyValueArray[0].trim());
key = safeDecode(keyValueArray[0].trim());
}

if (keyValueArray.length > 1) {
value = decodeURIComponent(keyValueArray.slice(1).join("=").trim());
value = safeDecode(keyValueArray.slice(1).join("=").trim());
}

return {
Expand Down