diff --git a/docs/old-code-backup/js-cookie-monitor-debugger-hook.js b/docs/old-code-backup/js-cookie-monitor-debugger-hook.js index e61e83a..fb5c0cf 100644 --- a/docs/old-code-backup/js-cookie-monitor-debugger-hook.js +++ b/docs/old-code-backup/js-cookie-monitor-debugger-hook.js @@ -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 @@ -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 { diff --git a/src/utils/format.ts b/src/utils/format.ts index 8dc0568..3e95207 100644 --- a/src/utils/format.ts +++ b/src/utils/format.ts @@ -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 包含键值对的字符串 @@ -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 {