Skip to content

Commit e92947e

Browse files
committed
fix: split limit error: #1
1 parent 52e4193 commit e92947e

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

js-cookie-monitor-debugger-hook.js

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ==UserScript==
22
// @name JS Cookie Monitor/Debugger Hook
33
// @namespace https://github.com/CC11001100/crawler-js-hook-framework-public
4-
// @version 0.8
4+
// @version 0.9
55
// @description 用于监控js对cookie的修改,或者在cookie符合给定条件时进入断点
66
// @document https://github.com/CC11001100/crawler-js-hook-framework-public/tree/master/001-cookie-hook
77
// @author CC11001100
@@ -252,19 +252,38 @@
252252
function parseSetCookie(cookieString) {
253253
// uuid_tt_dd=10_37476713480-1609821005397-659114; Expires=Thu, 01 Jan 1025 00:00:00 GMT; Path=/; Domain=.csdn.net;
254254
const cookieStringSplit = cookieString.split(";");
255-
const cookieNameValueArray = cookieStringSplit[0].split("=", 2);
256-
const cookieName = decodeURIComponent(cookieNameValueArray[0].trim());
257-
const cookieValue = cookieNameValueArray.length > 1 ? decodeURIComponent(cookieNameValueArray[1].trim()) : "";
255+
const {key, value} = splitKeyValue(cookieStringSplit.length && cookieStringSplit[0])
258256
const map = new Map();
259257
for (let i = 1; i < cookieStringSplit.length; i++) {
260-
const ss = cookieStringSplit[i].split("=", 2);
261-
const key = ss[0].trim().toLowerCase();
262-
const value = ss.length > 1 ? ss[1].trim() : "";
263-
map.set(key, value);
258+
let {key, value} = splitKeyValue(cookieStringSplit[i]);
259+
map.set(key.toLowerCase(), value);
264260
}
265261
// 当不设置expires的时候关闭浏览器就过期
266262
const expires = map.get("expires");
267-
return new CookiePair(cookieName, cookieValue, expires ? new Date(expires).getTime() : null)
263+
return new CookiePair(key, value, expires ? new Date(expires).getTime() : null)
264+
}
265+
266+
/**
267+
* 把按照等号=拼接的key、value字符串切分开
268+
* @param s
269+
* @returns {{value: string, key: string}}
270+
*/
271+
function splitKeyValue(s) {
272+
let key = "", value = "";
273+
const keyValueArray = (s || "").split("=");
274+
275+
if (keyValueArray.length) {
276+
key = decodeURIComponent(keyValueArray[0].trim());
277+
}
278+
279+
if (keyValueArray.length > 1) {
280+
value = decodeURIComponent(keyValueArray.slice(1).join("=").trim());
281+
}
282+
283+
return {
284+
key,
285+
value
286+
}
268287
}
269288

270289
/**
@@ -278,9 +297,7 @@
278297
return cookieMap;
279298
}
280299
document.cookie.split(";").forEach(x => {
281-
const ss = x.split("=", 2);
282-
const key = decodeURIComponent(ss[0].trim());
283-
const value = ss.length > 1 ? decodeURIComponent(ss[1].trim()) : "";
300+
const {key, value} = splitKeyValue(x);
284301
cookieMap.set(key, new CookiePair(key, value));
285302
});
286303
return cookieMap;

0 commit comments

Comments
 (0)