|
1 | 1 | // ==UserScript== |
2 | 2 | // @name JS Cookie Monitor/Debugger Hook |
3 | 3 | // @namespace https://github.com/CC11001100/crawler-js-hook-framework-public |
4 | | -// @version 0.8 |
| 4 | +// @version 0.9 |
5 | 5 | // @description 用于监控js对cookie的修改,或者在cookie符合给定条件时进入断点 |
6 | 6 | // @document https://github.com/CC11001100/crawler-js-hook-framework-public/tree/master/001-cookie-hook |
7 | 7 | // @author CC11001100 |
|
252 | 252 | function parseSetCookie(cookieString) { |
253 | 253 | // uuid_tt_dd=10_37476713480-1609821005397-659114; Expires=Thu, 01 Jan 1025 00:00:00 GMT; Path=/; Domain=.csdn.net; |
254 | 254 | 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]) |
258 | 256 | const map = new Map(); |
259 | 257 | 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); |
264 | 260 | } |
265 | 261 | // 当不设置expires的时候关闭浏览器就过期 |
266 | 262 | 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 | + } |
268 | 287 | } |
269 | 288 |
|
270 | 289 | /** |
|
278 | 297 | return cookieMap; |
279 | 298 | } |
280 | 299 | 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); |
284 | 301 | cookieMap.set(key, new CookiePair(key, value)); |
285 | 302 | }); |
286 | 303 | return cookieMap; |
|
0 commit comments