Skip to content

Commit f13959a

Browse files
committed
feat: 支持获取用户代码位置
1 parent 11fdf64 commit f13959a

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

src/config/ui/component/language.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ const chinese = {
7474
paramName: "参数名称",
7575
paramValue: "参数值",
7676
isJsonpCallback: "是否是jsonp回调函数",
77+
codeLocation: "代码位置",
7778
}
7879
};
7980

src/formatter/request-formatter.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const {repeat, fillToLength} = require("../utils/string-util");
33
const {getLanguage} = require("../config/ui/component/language");
44
const {getGlobalConfig} = require("../config/config");
55
const {printStyledTable} = require("./table-formatter");
6+
const {getUserCodeLocation} = require("../utils/code-util");
67

78
/**
89
* 用于第请求进行格式化
@@ -15,6 +16,8 @@ class RequestFormatter {
1516
*/
1617
format(scriptContext) {
1718

19+
const codeLocation = getUserCodeLocation();
20+
1821
const requestContext = scriptContext.requestContext;
1922
const language = getLanguage(getGlobalConfig().language);
2023

@@ -32,6 +35,7 @@ class RequestFormatter {
3235
[language.console.hostname, requestContext.hostname, ""],
3336
[language.console.path, requestContext.path, ""],
3437
[language.console.hash, requestContext.hash, ""],
38+
[language.console.codeLocation, codeLocation, ""],
3539
// [language.console.param, requestContext.params.length],
3640
];
3741

src/hook/watch-hook.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* 监控对象
3+
*/
4+
class WatchHook {
5+
6+
watch(object) {
7+
8+
return new Proxy();
9+
10+
}
11+
12+
}

src/utils/code-util.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,30 @@ function generateRandomFunctionName(length = 8) {
6868
return result;
6969
}
7070

71+
const tampermonkeyChromeExtensionId = "dhdgffkkebhmkfjojejmpbldmpobfkfo";
72+
73+
/**
74+
* 获取用户代码的位置,用户代码的定义就是调用栈里从用户的代码进入到插件代码的第一行代码
75+
*/
76+
function getUserCodeLocation() {
77+
78+
// 把调用栈一个栈帧一个栈帧的弹掉
79+
const stack = new Error().stack.split("\n");
80+
let index = stack.length - 1;
81+
while (index >= 0) {
82+
const frame = stack[index];
83+
if (frame.includes(tampermonkeyChromeExtensionId) && index < stack.length) {
84+
return stack[index + 1].trim();
85+
} else {
86+
index--;
87+
}
88+
}
89+
return null;
90+
}
91+
7192
module.exports = {
7293
getFunctionBody,
7394
getParameterNames,
7495
generateRandomFunctionName,
96+
getUserCodeLocation,
7597
};

0 commit comments

Comments
 (0)