Skip to content

修复兼容性问题 #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 26, 2023
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,5 @@ fabric.properties
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser


.idea
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,32 @@ consoleLogFontSize
# 九、其它

本项目拆分自:

[https://github.com/CC11001100/crawler-js-hook-framework-public/tree/master/001-cookie-hook#%E7%9B%91%E6%8E%A7%E5%AE%9A%E4%BD%8Djavascript%E6%93%8D%E4%BD%9Ccookie](https://github.com/CC11001100/crawler-js-hook-framework-public/tree/master/001-cookie-hook#%E7%9B%91%E6%8E%A7%E5%AE%9A%E4%BD%8Djavascript%E6%93%8D%E4%BD%9Ccookie)

更改了namespace,可能安装量要清零了,截图纪念下,截止到目前(2022-7-29 21:40:01)安装量破三百了,感觉对于这么窄领域的一个小工具来说很不容易了...

![image-20220729214105718](README.assets/image-20220729214105718.png)

# 十、感谢支持
感谢热心网友反馈问题,谢谢支持。

<div>
<ul style="list-style: none; outside: none; margin:0px; padding: 0px;">
<li style="float: left; margin: 0 10px;">
<a href="https://github.com/CC11001100">
<img src="https://avatars.githubusercontent.com/u/12819457?v=4" style="width: 100px; border-radius: 100px;"/><br/>CC11001100
</a>
</li>
<li style="float: left; margin: 0 10px;">
<a href="https://github.com/chenstrace">
<img src="https://avatars.githubusercontent.com/u/16367987?v=4" style="width: 100px; border-radius: 100px;"/><br/>chenstrace
</a>
</li>
<li style="float: left; margin: 0 10px;">
<a href="https://github.com/llfer2006">
<img src="https://avatars.githubusercontent.com/u/1904451?v=4" style="width: 100px; border-radius: 100px;"/><br/>llfer2006
</a>
</li>
</ul>
</div>

15 changes: 15 additions & 0 deletions dev-header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// ==UserScript==
// @name JS Cookie Monitor/Debugger Hook
// @namespace https://github.com/CC11001100/js-cookie-monitor-debugger-hook
// @version 0.10
// @description 用于监控js对cookie的修改,或者在cookie符合给定条件时进入断点
// @document https://github.com/CC11001100/js-cookie-monitor-debugger-hook
// @author CC11001100
// @match *://*/*
// @run-at document-start
// @grant none
// @require file://D:/workspace/js-cookie-monitor-debugger-hook/js-cookie-monitor-debugger-hook.js
// ==/UserScript==
(() => {

})()
220 changes: 138 additions & 82 deletions js-cookie-monitor-debugger-hook.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ==UserScript==
// @name JS Cookie Monitor/Debugger Hook
// @namespace https://github.com/CC11001100/js-cookie-monitor-debugger-hook
// @version 0.9
// @version 0.10
// @description 用于监控js对cookie的修改,或者在cookie符合给定条件时进入断点
// @document https://github.com/CC11001100/js-cookie-monitor-debugger-hook
// @author CC11001100
Expand All @@ -21,10 +21,7 @@

// 设置事件断点是否开启,一般保持默认即可
const enableEventDebugger = {
"add": true,
"update": true,
"delete": true,
"read": true,
"add": true, "update": true, "delete": true, "read": true,
}

// 在控制台打印日志时字体大小,根据自己喜好调整
Expand All @@ -34,23 +31,119 @@
// 使用document.cookie更新cookie,但是cookie新的值和原来的值一样,此时要不要忽略这个事件
const ignoreUpdateButNotChanged = false;

// 网站的开发者也可能会使用到Object.,这会与工具内置的冲突,使用这个变量持有者目标网站开发者自己设置的
// 然后在执行的时候使其真正的生效,这样不影响原有的逻辑
let realDocumentCookieProperty = null;

// 用于区分是本插件自己调用的definePropertyIsMe还是外部调用的
const definePropertyIsMe = "CC11001100-js-cookie-monitor-debugger-hook";

// 页面内部的Object.defineProperty需要能够劫持一下
(function () {

// 把Object.defineProperty给拦截了
Object.defineProperty = new Proxy(Object.defineProperty, {
apply: function (target, thisArg, argArray) {

// 检查是否是自己调用的
const isMe = argArray && argArray.length >= 3 && argArray[2] && definePropertyIsMe in argArray[2];

// 检查是否是定义的document.cookie
const isDocumentCookie = argArray && argArray.length >= 2 && argArray[0] === document && "cookie" === argArray[1];

if (!isMe && isDocumentCookie) {
// 检查要定义访问符的是否是document.cookie这个方法的话就包装一下,保证同时多个都能被调用到
if (argArray && argArray.length >= 3) {
// 更新一下real property就不管了,
realDocumentCookieProperty = argArray[2];
return;
}
}
return target.apply(thisArg, argArray);
}
});

Object.defineProperty.toString = function () {
return "function defineProperty() { [native code] }";
}

// 把Object.defineProperties也给拦截了
Object.defineProperties = new Proxy(Object.defineProperties, {
apply: function (target, thisArg, argArray) {
// 可能会通过如下代码来调用:
// Object.defineProperties(document, {"cookie": {...})
const isDocumentCookie = argArray && argArray.length >= 2 && document === argArray[0] && "cookie" in argArray[1];
if (isDocumentCookie) {
// 把要设置的property描述符持有者
realDocumentCookieProperty = argArray[1]["cookie"];
// 任务这个cookie的define已经执行完了,将其删除掉
delete argArray[1]["cookie"];
// 如果只有一个cookie的话,删除完没有其它的属性了,则没必要继续往下了
// 如果有剩余的属性的话,则需要原样继续执行
if (!Object.keys(argArray[1]).length) {
return;
}
}
return target.apply(thisArg, argArray);
}
});

Object.defineProperties.toString = function () {
return "function defineProperties() { [native code] }";
}

})();

// 此处实现的反复hook,保证页面流程能够继续往下走下去
(function addCookieHook() {
Object.defineProperty(document, "cookie", {
const handler = {
get: () => {

// 先恢复原状
delete document.cookie;
const currentDocumentCookie = document.cookie;
addCookieHook();
return currentDocumentCookie;
},
set: newValue => {

try {
// 如果网站开发者有设置自己的属性访问符的话,则以他设置的为准,把它的返回值作为此函数最终的返回值,保持其原有逻辑
if (realDocumentCookieProperty && "get" in realDocumentCookieProperty) {
// 在网站执行者自己定义的cookie的property执行期间,我们的工具添加的hook是被下掉的,所以是没有影响的
return realDocumentCookieProperty["get"].apply(this, arguments);
} else {
// 如果网站开发者没有设置自己的property的话,则获取到真正的cookie值返回
return document.cookie;
}
} finally {
// 然后这么获取完之后,还是要把hook加上
addCookieHook();
}

}, set: newValue => {

// 先触发相关的事件
cc11001100_onSetCookie(newValue);

// 然后恢复原状,把我们设置的hook啥的下掉
delete document.cookie;
document.cookie = newValue;
addCookieHook();
},
configurable: true
});

try {
// 如果网站开发者有设置自己的属性访问符的话,则以他设置的为准
if (realDocumentCookieProperty && "set" in realDocumentCookieProperty) {
// 在网站执行者自己定义的cookie的property执行期间,我们的工具添加的hook是被下掉的,所以是没有影响的
// 不过这同时带来一个新的问题,就是如果它在这个property中进行cookie的操作我们无法感知到,那能怎么办呢?有得必有失
// TODO 2023-7-26 22:02:11 那,有没有比较简单的“我全都要”的方案呢?
realDocumentCookieProperty["set"].apply(this, [newValue]);
} else {
// 如果网站开发者没有设置property或者没有设置set的话,则还是走默认的赋值逻辑
document.cookie = newValue;
}
} finally {
// 然后再把hook设置上,加在finally里保证就算出错了也能恢复hook
addCookieHook();
}

}, configurable: true, enumerable: false,
};
handler[definePropertyIsMe] = true;
Object.defineProperty(document, "cookie", handler);
})();

/**
Expand Down Expand Up @@ -88,34 +181,24 @@

const message = [

normalStyle,
now(),
normalStyle, now(),

normalStyle,
"JS Cookie Monitor: ",
normalStyle, "JS Cookie Monitor: ",

normalStyle,
"delete cookie, cookieName = ",
normalStyle, "delete cookie, cookieName = ",

valueStyle,
`${cookieName}`,
valueStyle, `${cookieName}`,

...(() => {
if (!cookieValue) {
return [];
}
return [
normalStyle,
", value = ",
return [normalStyle, ", value = ",

valueStyle,
`${cookieValue}`,
];
valueStyle, `${cookieValue}`,];
})(),

normalStyle,
`, code location = ${getCodeLocation()}`
];
normalStyle, `, code location = ${getCodeLocation()}`];
console.log(genFormatArray(message), ...message);

testDebuggerRules(cookieOriginalValue, "delete", cookieName, cookieValue);
Expand All @@ -134,53 +217,35 @@

const message = [

normalStyle,
now(),
normalStyle, now(),

normalStyle,
"JS Cookie Monitor: ",
normalStyle, "JS Cookie Monitor: ",

normalStyle,
"update cookie, cookieName = ",
normalStyle, "update cookie, cookieName = ",

valueStyle,
`${cookieName}`,
valueStyle, `${cookieName}`,

...(() => {
if (cookieValueChanged) {
return [
normalStyle,
`, oldValue = `,
return [normalStyle, `, oldValue = `,

valueStyle,
`${oldCookieValue}`,
valueStyle, `${oldCookieValue}`,

normalStyle,
`, newValue = `,
normalStyle, `, newValue = `,

valueStyle,
`${newCookieValue}`
]
valueStyle, `${newCookieValue}`]
} else {
return [
normalStyle,
`, value = `,
return [normalStyle, `, value = `,

valueStyle,
`${newCookieValue}`,
];
valueStyle, `${newCookieValue}`,];
}
})(),

normalStyle,
`, valueChanged = `,
normalStyle, `, valueChanged = `,

valueStyle,
`${cookieValueChanged}`,
valueStyle, `${cookieValueChanged}`,

normalStyle,
`, code location = ${getCodeLocation()}`
];
normalStyle, `, code location = ${getCodeLocation()}`];
console.log(genFormatArray(message), ...message);

testDebuggerRules(cookieOriginalValue, "update", cookieName, newCookieValue, cookieValueChanged);
Expand All @@ -192,27 +257,19 @@

const message = [

normalStyle,
now(),
normalStyle, now(),

normalStyle,
"JS Cookie Monitor: ",
normalStyle, "JS Cookie Monitor: ",

normalStyle,
"add cookie, cookieName = ",
normalStyle, "add cookie, cookieName = ",

valueStyle,
`${cookieName}`,
valueStyle, `${cookieName}`,

normalStyle,
", cookieValue = ",
normalStyle, ", cookieValue = ",

valueStyle,
`${cookieValue}`,
valueStyle, `${cookieValue}`,

normalStyle,
`, code location = ${getCodeLocation()}`
];
normalStyle, `, code location = ${getCodeLocation()}`];
console.log(genFormatArray(message), ...message);

testDebuggerRules(cookieOriginalValue, "add", cookieName, cookieValue);
Expand Down Expand Up @@ -281,8 +338,7 @@
}

return {
key,
value
key, value
}
}

Expand Down Expand Up @@ -469,4 +525,4 @@

}

)();
)();
Loading