Skip to content

Commit 5bdf946

Browse files
committed
version 0.2
1. 更换了html处理库,之前的有bug 2. 兼容了窗口跨域有多个线程栈的情况 3. 扩大关键词,能够搜到更多结果
1 parent 121b975 commit 5bdf946

File tree

7 files changed

+334
-60
lines changed

7 files changed

+334
-60
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ npm install
6767
```text
6868
src/proxy-server/proxy-server.js
6969
```
70-
要用anyproxy抓取https请求需要信任它的证书,访问它的web管理界面:
70+
要用anyproxy抓取https请求需要信任它的证书,在运行这个文件之前,先用`anyproxy ca`选项启动,访问它的web管理界面:
7171
```text
7272
http://localhost:8002/
7373
```

package-lock.json

Lines changed: 118 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"@babel/types": "^7.12.12",
1515
"anyproxy": "^4.1.3",
1616
"body-parser": "^1.19.0",
17+
"cheerio": "^1.0.0-rc.5",
1718
"express": "^4.17.1",
1819
"jssoup": "0.0.12",
1920
"shelljs": "^0.8.4"

src/components/global-assign-hook-component/core/global-assign-hook-component-main.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const fs = require("fs");
22
const shell = require("shelljs");
33
const crypto = require("crypto");
4-
const JSSoup = require("jssoup").default;
4+
const cheerio = require("cheerio");
55

66
const {loadPluginsAsStringWithCache} = require("./plugins-manager");
77
const {injectHook} = require("./inject-hook");
@@ -75,6 +75,7 @@ function isHtmlResponse(responseDetail) {
7575
// HTML中可能会夹带script标签,里面的JS代码也要能Hook到
7676
function processHtmlResponse(requestDetail, responseDetail) {
7777
// 使用了这个库来解析HTML https://github.com/chishui/JSSoup
78+
// 上面那个库有bug,替换为这个库: https://github.com/cheeriojs/cheerio/
7879

7980
// 对所有的内嵌JavaScript内容注入Hook
8081
const url = requestDetail.url;
@@ -84,35 +85,45 @@ function processHtmlResponse(requestDetail, responseDetail) {
8485
return;
8586
}
8687

87-
const soup = new JSSoup(body);
88-
const scriptArray = soup.findAll("script");
88+
const $ = cheerio.load(body);
89+
const scriptArray = $("script");
8990
if (!scriptArray?.length) {
9091
return;
9192
}
9293
let alreadyInjectHookContext = false;
9394
for (let script of scriptArray) {
95+
9496
// 对于是src引用的外部,其标签内容都会被忽略
95-
if (script.attrs.src) {
97+
if (script.attribs.src) {
9698
continue;
9799
}
98-
const jsCode = script.contents.join("\n");
99-
if (!script.contents.length || !jsCode) {
100+
101+
// 空script
102+
if (!script.children.length) {
100103
continue;
101104
}
102-
// 要保留住原来的属性
103-
let oldAttrs = "";
104-
for(let key in script.attrs) {
105-
oldAttrs += ` "${key.replace("\"", "\\\"")}"="${script.attrs[key].replace("\"", "\\\"")}" `
105+
106+
// script的内容
107+
let jsCode = "";
108+
for(let child of script.children) {
109+
jsCode += child.data;
106110
}
111+
if (!jsCode) {
112+
return;
113+
}
114+
107115
let newJsCode = injectHook(jsCode);
108116
// 随着script替换时注入,不创建新的script标签
109117
if (!alreadyInjectHookContext) {
110118
newJsCode = loadPluginsAsStringWithCache() + newJsCode;
111119
alreadyInjectHookContext = true;
112120
}
113-
script.replaceWith("<script "+ oldAttrs +">" + newJsCode + "</script>")
121+
122+
const newScript = cheerio.load("<script>" + newJsCode + "</script>")("script");
123+
newScript.attribs = script.attribs;
124+
$(script).replaceWith(newScript);
114125
}
115-
responseDetail.response.body = soup.prettify();
126+
responseDetail.response.body = $.html();
116127
}
117128

118129
/**

src/components/global-assign-hook-component/core/plugins-manager.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@ function loadPluginsAsStringWithCache() {
2525
function loadPluginsAsString() {
2626

2727
// 用来保证Hook代码只被加载一次
28+
// TODO 妥善处理Worker环境
2829
const loadOnce = "\n" +
29-
// 在Worker环境下会出现window未定义
30-
"if (!window || window.cc11001100_hook_done) {\n" +
31-
" return;\n" +
32-
"}\n" +
33-
"window.cc11001100_hook_done = true;\n\n";
30+
" if (!window) {\n" +
31+
" return; \n" +
32+
" } \n" +
33+
" if (window.cc11001100_hook_done) {\n" +
34+
" return;\n" +
35+
" }\n" +
36+
" window.cc11001100_hook_done = true;\n\n";
3437

3538
const hookJsCode = fs.readFileSync("../components/global-assign-hook-component/core/hook.js").toString();
3639

@@ -42,7 +45,7 @@ function loadPluginsAsString() {
4245
pluginsJsContentArray.push(pluginJsContent);
4346
}
4447

45-
return "// ----------------------------------------- Hook代码开始 ----------------------------------------------------- \n" +
48+
return "\n// ----------------------------------------- Hook代码开始 ----------------------------------------------------- \n" +
4649
"\n(() => {\n" +
4750

4851
loadOnce +

0 commit comments

Comments
 (0)