Skip to content

Commit dc28d91

Browse files
committed
fix: 修复debugger bug
1 parent b2694de commit dc28d91

File tree

8 files changed

+147
-29
lines changed

8 files changed

+147
-29
lines changed

src/config/config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class Config {
6969
debuggerInformation.id = debuggerInformationObject.id;
7070
debuggerInformation.enable = debuggerInformationObject.enable;
7171
debuggerInformation.urlPattern = debuggerInformationObject.urlPattern;
72+
debuggerInformation.urlPatternType = debuggerInformationObject.urlPatternType;
7273
debuggerInformation.enableRequestDebugger = debuggerInformationObject.enableRequestDebugger;
7374
debuggerInformation.enableResponseDebugger = debuggerInformationObject.enableResponseDebugger;
7475
debuggerInformation.callbackFunctionParamName = debuggerInformationObject.callbackFunctionParamName;
@@ -90,7 +91,9 @@ class Config {
9091
*/
9192
testAll(scriptContext) {
9293
for (let jsonpDebugger of this.debuggers) {
93-
new DebuggerTester().test(this, jsonpDebugger, scriptContext);
94+
if (jsonpDebugger.enable) {
95+
new DebuggerTester().test(this, jsonpDebugger, scriptContext);
96+
}
9497
}
9598
}
9699

src/config/ui/component/configuration-component.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class ConfigurationComponent {
1313
<div id="cc11001100-js-script-hook-configuration-modal-window" style="display:none !important; position:fixed !important; left:0 !important; top:0 !important; width:100% !important; height:100% !important; background-color:rgba(0,0,0,0.85) !important; z-index:2147483646 !important; overflow-y:auto !important;">
1414
<div class="js-script-hook-scrollable-div" style="display: flex; width: 930px !important; text-align: center !important; padding: 30px !important; margin: 10px !important; position:absolute !important; left:50% !important; top:50% !important; transform:translate(-50%, -50%) !important; background:white !important; border-radius:5px !important; box-shadow: 0 4px 8px rgba(0,0,0,0.1) !important; max-width:80% !important; text-align:center !important; z-index:99999999999; !important">
1515
<button id="cc11001100-js-script-hook-configuration-close-btn" style="position:absolute; right:8px; top:8px; cursor:pointer; padding:3px 6px; border:none; background-color:#f44336; color:white; border-radius:50%; font-size:10px;">×</button>
16-
<div id="js-script-hook-configuration-content"></div>
16+
<div id="js-script-hook-configuration-content" style="color: black;"></div>
1717
</div>
1818
</div>
1919
`;

src/config/ui/component/debugger-component.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const {getGlobalConfig} = require("../../config");
2+
const {DebuggerTester} = require("../../../debugger/debugger-tester");
23

34
/**
45
* 用于表示一个断点配置
@@ -56,7 +57,7 @@ class DebuggerComponent {
5657
</div>
5758
<div class="js-script-hook-select-container" style="width: 400px !important; ">
5859
<select id="${debuggerConfig.id}-url-pattern">
59-
<option value="equals-string">${language.debugger_config.urlPatternType_EqualsThisString}</option>
60+
<option value="equals-string" >${language.debugger_config.urlPatternType_EqualsThisString}</option>
6061
<option value="contains-string">${language.debugger_config.urlPatternType_ContainsThisString}</option>
6162
<option value="match-regexp">${language.debugger_config.urlPatternType_MatchThisRegexp}</option>
6263
<option value="match-all">${language.debugger_config.urlPatternType_MatchALL}</option>
@@ -161,6 +162,11 @@ class DebuggerComponent {
161162
render(language, debuggerInformation) {
162163
const debuggerElt = $(this.template(language, debuggerInformation));
163164

165+
// 设置匹配类型
166+
if (debuggerInformation.urlPatternType) {
167+
debuggerElt.find(`#${debuggerInformation.id}-url-pattern`).val(debuggerInformation.urlPatternType);
168+
}
169+
164170
// 断点是否开启
165171
debuggerElt.find(`#${debuggerInformation.id}-enable-checkbox`).on('change', function () {
166172
const localDebuggerInformation = getGlobalConfig().findDebuggerById(debuggerInformation.id);
@@ -169,7 +175,8 @@ class DebuggerComponent {
169175
});
170176

171177
// URL匹配类型
172-
debuggerElt.find(`${debuggerInformation.id}-url-pattern`).change(function () {
178+
debuggerElt.find(`#${debuggerInformation.id}-url-pattern`).change(function () {
179+
debugger;
173180
const localDebuggerInformation = getGlobalConfig().findDebuggerById(debuggerInformation.id);
174181
localDebuggerInformation.urlPatternType = $(this).val();
175182
getGlobalConfig().persist();
@@ -184,9 +191,10 @@ class DebuggerComponent {
184191

185192
// URL匹配测试
186193
debuggerElt.find(`#${debuggerInformation.id}-url-pattern-test`).on('click', function () {
187-
// TODO 2025-01-05 16:10:01 测试
188-
let urlForTest = prompt("请输入要测试的URL", "");
189-
alert(urlForTest);
194+
let urlForTest = prompt(language.debugger_config.urlPatternTestPrompt, "");
195+
const debuggerConfig = getGlobalConfig().findDebuggerById(debuggerInformation.id);
196+
const result = new DebuggerTester().testUrlPattern(debuggerConfig.urlPatternType, debuggerConfig.urlPattern, urlForTest);
197+
alert(language.debugger_config.urlPatternTestResult + result);
190198
});
191199

192200
// enableRequestDebugger

src/config/ui/component/debugger-manager-component.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,18 @@ class DebuggerManagerComponent {
3434
// 增加断点配置
3535
debuggerManager.find("#js-script-hook-add-debugger-btn").click(() => {
3636
const debuggerComponent = new DebuggerComponent();
37-
const newDebuggerInformation = new Debugger();
38-
newDebuggerInformation.id = randomId();
39-
debuggerManager.find("#js-script-hook-debugger-list").append(debuggerComponent.render(language, newDebuggerInformation));
40-
41-
getGlobalConfig().addDebugger(newDebuggerInformation);
37+
const newDebuggerConfig = new Debugger();
38+
newDebuggerConfig.id = randomId();
39+
newDebuggerConfig.enable = true;
40+
newDebuggerConfig.urlPatternType = "match-all";
41+
newDebuggerConfig.urlPattern = "";
42+
newDebuggerConfig.enableRequestDebugger = true;
43+
newDebuggerConfig.enableResponseDebugger = true;
44+
newDebuggerConfig.callbackFunctionParamName = "";
45+
newDebuggerConfig.comment = "";
46+
debuggerManager.find("#js-script-hook-debugger-list").append(debuggerComponent.render(language, newDebuggerConfig));
47+
48+
getGlobalConfig().addDebugger(newDebuggerConfig);
4249
getGlobalConfig().persist();
4350
});
4451

src/config/ui/component/language.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ const chinese = {
4141

4242
urlPatternTest: "测试",
4343
urlPatternTestTips: "你可以输入一个script url测试此断点对其命中情况",
44+
urlPatternTestPrompt: "请输入要测试的URL:",
45+
urlPatternTestResult: "测试结果:",
4446

4547
enableRequestDebugger: "是否开启请求断点:",
4648
enableRequestDebuggerTips: "启动请求断点后,在script请求发出之前进入断点",

src/config/ui/component/style.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const css = `
4444
border-radius: 5px;
4545
font-size: 12px;
4646
font-weight: bold;
47-
cursor: pointer;
47+
cursor: pointer !important;
4848
transition: background-color 0.3s ease, transform 0.2s ease;
4949
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
5050
}

src/debugger/debugger-tester.js

Lines changed: 110 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ class DebuggerTester {
99
* 测试是否命中断点
1010
*
1111
* @param globalConfig
12-
* @param debuggerInformation
12+
* @param debuggerConfig
1313
* @param scriptContext {ScriptContext}
1414
*/
15-
test(globalConfig, debuggerInformation, scriptContext) {
15+
test(globalConfig, debuggerConfig, scriptContext) {
1616

1717
// 首先URL要能够匹配得上
18-
if (!this.testUrlPattern(debuggerInformation.urlPattern, scriptContext.url)) {
18+
if (!this.testUrlPattern(debuggerConfig.urlPatternType, debuggerConfig.urlPattern, scriptContext.url)) {
1919
return;
2020
}
2121

@@ -30,20 +30,48 @@ class DebuggerTester {
3030
}
3131

3232
// 请求断点
33-
if (debuggerInformation.enableRequestDebugger) {
33+
if (debuggerConfig.enableRequestDebugger) {
3434
// 把一些相关的上下文赋值到变量方便断点命中这里的时候观察
3535
// _scriptContext中存放的是与当前的script请求相关的一些上下文信息
3636
const _scriptContext = scriptContext;
3737
const humanReadableScriptInformation = scriptContext.toHumanReadable()
3838
console.log(humanReadableScriptInformation);
39+
// Example Output:
40+
// "Request Information:
41+
//
42+
// hostname: gcaptcha4.geetest.com
43+
//
44+
// path: /verify
45+
//
46+
// params:
47+
//
48+
// callback = geetest_1736090943102 <---- this param is jsonp callback function name
49+
//
50+
// captcha_id = ada5349b1590bc08b1bed97596609199
51+
//
52+
// client_type = web
53+
//
54+
// lot_number = e5e95b5b2b8940e6bb6968695d9eda95
55+
//
56+
// payload = AgFD8gWUUuHFx-XvpP7J2WzfgGoulbh3scPkfDrN7Ls-13ySGq_ISPvhk7HFpeI8B148K8UQEDx2jB_Fb7lAxAAgR9oVUofnxqYMuoaa1PYovz2fsXmG5FNyu8oMp8XSMK6zRUMqty7ZeYItsGJNMd2FW6DcZUb7R5OGAkWna2dxl3YboMMkz3g6VLgoLMjgRHEQIUIfi87jpgBZ-R7roQp3rkW0d8ja2Xtr20rNHx1TjjvkCzIrgfRjebptQ7yEuGj76kL1QPnUGbhZo4BfVTBeEC2w9O2-QUdtJfPZMnUnwWnVUsDMvqo_wzygw739JwxRGpbao3DmXCAJtIKjQvTUS3B4-_ecSFIRmk8jcruJ_g60QAOqeZ5KNCMNXWsqLbGQ3bJb7gwssZO6fytpzYq4qHoBq7e0cOrnOJ0ZaLsGtVPYJo8gfKVKQvsWSdWJcVBWoqkRS5-J7g338FaHXxA-VFJ4MxTLHVjiucGm5Trkt2LhXdqPITDXj89FCeXzFwVeDraETv6mh7kooR1a43SGhInkiKodiUriOqt886C1RgClV2HxcQNoI2IdqYQ9GyE8MLkXSKDjOH7A--A8Tx_jDYBDTV-Wy3blQgtsDty7dlU8sbtIdt2g7BGLAzj7vM4IwHVEap02u75OnWY71IanX4uuvm9nyFSrffOxEWRyOqMnYON5cmNzwPRhqoK2cPlpVFsh6Ypzkfssdc1QcWeiH2v20gARzzUQ7-gL9-X-Ws4v5CHxV8hwESaPmgZyd_Xw8wIwAdZddI8mKtI9ez_wMBY56GBh556JzLmZnmJpRRPIEq2raNOYLSZG4DkagQWSuglXuIgbv5_ufoI80uOqJcB_WfkfhhSreCtSVgletH5DarnmyaDUXRZRq1uHpyriRAUzJp8rLuaxufx9ukrjjM9Ii0YPJdWv5AAD_p0_33PutbjQeSEHKu4OfDhG5NO-j9_EptTINLDt9FVxFeP3QBv0rz6_HtUcMvkp6uY=
57+
//
58+
// process_token = b6ca31f953e1478a1383e0a7717ea1f79f2f52588a7517768e1e8e374004785d
59+
//
60+
// payload_protocol = 1
61+
//
62+
// pt = 1
63+
//
64+
// w = ac7939c00e952ef76eed20bc2a21798d1e58737ec099db0b4a38a369172e2bbf762b400943f092402f9cc4caaf1c49ebeb4783538d6d8f0fb786ea7d917c2c16098dfcef0cb52fe6864fcc474e05f9db8afc240505824cdac3ab4aa13c1bfc21d1602b0cfec4d86e7f6ffc135fd0bf84c30f7201cd097798204519ab54a98d3684f57398719176bc3bd27d88520aeb2e118d6fe6f5e9d793bfa05e1579981357a2e47bca18ace553e1b4a9baacd1881b5d4e28597c7861d78f38fc308573c3375e8994b240efc43c0791571e1ac9493af894c84954fe6b6243bef210b30becb4e4a63b0c18101b5c33f141d67eac3d96789a9e9ba8ceefc221b53ea12318850bf1a1b03587dfd90a3dc882bc7968be880046b0c9455bba27df2774bf94fcde7ad850aa12ab775c93aa6e2a3f69f755ff58d630c25fbe4758477482dd2753ca43f9f28d68563ea3c11536bab0c61649a13ed6a1371be1af54b7b9845bcef44ee5fe48d276381de74ed47894c407ae2a3f73afce1289a3c2579183b63267731c03f6408cce768ca9ae8880c31facb2057a4ddc48cfeaf55b0025a0fc369b5bf84c9d3def63b1abbfec0da22d2f4a3b05be267dd23aedb20ac784b14952a6a0296b357941962bc96de50b42d32467a4b9cd29b5db1e175b2464b049e59f0d87723e23d91f08419ca2812ad4dd22657041a2fde24aa2004e71978c65799da58c3544a5bb3a0c6424db59592ab2f9183993389cf2585652b12d6e1674386cb46f7226e8acf997bb41bbd6c213482f0f2af9c25a58d9ef37c4eb235dc427c3734f628d5a94be8a3c19c0982fdca98f2b23be0fe7c85f881284a0c83d783221db1f07cde5f5673261e729a4f2c25cda9a3b37353b5b1e11baaf1834a0698565faec711a
65+
//
66+
// "
3967
debugger;
4068
}
4169

4270
// 响应断点
43-
if (debuggerInformation.enableResponseDebugger) {
71+
if (debuggerConfig.enableResponseDebugger) {
4472

4573
// 如果没有指定jsonp函数的名字的话,则尝试自动抽取函数名字
46-
let jsonpCallbackFunctionName = debuggerInformation.callbackFunctionParamName;
74+
let jsonpCallbackFunctionName = debuggerConfig.callbackFunctionParamName;
4775
if (!jsonpCallbackFunctionName) {
4876
jsonpCallbackFunctionName = scriptContext.requestContext.getJsonpCallbackFuncName();
4977
}
@@ -62,22 +90,28 @@ class DebuggerTester {
6290
}
6391
// 跟进去这个 jsonpCallbackFunction 函数的代码位置就是jsonp的回调函数的逻辑,也是处理响应的逻辑
6492
new ObjectFunctionHook(getUnsafeWindow(), jsonpCallbackFunctionName).addHook(function () {
93+
const jsonpResponseToString = JSON.stringify(arguments, null, 4);
6594
// 这里是脚本的响应断点,已经拦截到响应,跟进去holder函数就行了
95+
console.log(jsonpCallbackFunction);
6696
console.log(jsonpCallbackFunctionName);
97+
console.log(jsonpResponseToString);
6798
debugger;
6899
});
69100
}
70101

71102
}
72103

104+
// ---------------------------------------------------------------------------------------------------------------------
105+
73106
/**
74107
* 测试请求的URL是否匹配
75108
*
76-
* @param urlPattern
109+
* @param urlPatternType 界面上配置的匹配类型
110+
* @param urlPattern 界面配置的匹配类型对应的值
77111
* @param url {String} 要测试匹配的URL
78112
* @return {boolean} 是否匹配得上
79113
*/
80-
testUrlPattern(urlPattern, url) {
114+
testUrlPattern(urlPatternType, urlPattern, url) {
81115

82116
if (!url) {
83117
return false;
@@ -87,16 +121,78 @@ class DebuggerTester {
87121
return true;
88122
}
89123

90-
if (typeof urlPattern === "string") {
91-
return urlPattern.indexOf(url) !== -1;
92-
} else if (urlPattern instanceof RegExp) {
93-
return urlPattern.test(url);
94-
} else {
95-
return false;
124+
// if (typeof urlPattern === "string") {
125+
// return urlPattern.indexOf(url) !== -1;
126+
// } else if (urlPattern instanceof RegExp) {
127+
// return urlPattern.test(url);
128+
// } else {
129+
// return false;
130+
// }
131+
132+
switch (urlPatternType) {
133+
case "equals-string":
134+
return this.testUrlPatternForEquals(urlPattern, url);
135+
case "contains-string":
136+
return this.testUrlPatternForContains(urlPattern, url);
137+
case "match-regexp":
138+
return this.testUrlPatternForMatchRegexp(urlPattern, url);
139+
case "match-all":
140+
return this.testUrlPatternForMatchAll(urlPattern, url);
141+
default:
142+
return false;
143+
}
144+
145+
}
146+
147+
/**
148+
* 完全匹配
149+
*
150+
* @param urlPattern
151+
* @param url
152+
* @return {*}
153+
*/
154+
testUrlPatternForEquals(urlPattern, url) {
155+
return url.equals(urlPattern);
156+
}
157+
158+
/**
159+
* 包含给定的关键字
160+
*
161+
* @param urlPattern
162+
* @param url
163+
* @return {boolean}
164+
*/
165+
testUrlPatternForContains(urlPattern, url) {
166+
return url.indexOf(urlPattern) !== -1;
167+
}
168+
169+
/**
170+
* 正则表达式方式匹配
171+
*
172+
* @param urlPattern
173+
* @param url
174+
* @return {boolean}
175+
*/
176+
testUrlPatternForMatchRegexp(urlPattern, url) {
177+
try {
178+
return new RegExp(urlPattern).test(url);
179+
} catch (e) {
180+
console.error(e);
96181
}
182+
}
97183

184+
/**
185+
* 直接匹配所有
186+
* @param urlPattern
187+
* @param url
188+
* @return {boolean}
189+
*/
190+
testUrlPatternForMatchAll(urlPattern, url) {
191+
return true;
98192
}
99193

194+
// ---------------------------------------------------------------------------------------------------------------------
195+
100196
}
101197

102198
module.exports = {

src/hook/object-function-hook.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ class ObjectFunctionHook {
3030
}
3131

3232
// 如果已经Hook过了则不重复hook,也就是说一次addHook只生效一次
33-
const hookDoneFlag = getGlobalConfig().prefix + "_hookDoneFlag";
33+
// const prefix = getGlobalConfig().prefix || "CC11001100_js_script_hook"
34+
const prefix = "CC11001100_js_script_hook"
35+
const hookDoneFlag = prefix + "_hookDoneFlag";
3436
if (functionHolder[hookDoneFlag]) {
3537
return;
3638
}
@@ -40,7 +42,7 @@ class ObjectFunctionHook {
4042

4143
try {
4244
// TODO 2023-8-21 22:15:09 在函数执行的时候尝试触发各种断点
43-
hookCallbackFunction.apply(this)
45+
hookCallbackFunction.apply(this, arguments)
4446
} catch (e) {
4547
console.error(e);
4648
}

0 commit comments

Comments
 (0)