Skip to content

Commit

Permalink
version 0.2
Browse files Browse the repository at this point in the history
1. 更换了html处理库,之前的有bug
2. 兼容了窗口跨域有多个线程栈的情况
3. 扩大关键词,能够搜到更多结果
  • Loading branch information
CC11001100 committed Jan 19, 2021
1 parent 121b975 commit 5bdf946
Show file tree
Hide file tree
Showing 7 changed files with 334 additions and 60 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ npm install
```text
src/proxy-server/proxy-server.js
```
要用anyproxy抓取https请求需要信任它的证书,访问它的web管理界面:
要用anyproxy抓取https请求需要信任它的证书,在运行这个文件之前,先用`anyproxy ca`选项启动,访问它的web管理界面:
```text
http://localhost:8002/
```
Expand Down
118 changes: 118 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@babel/types": "^7.12.12",
"anyproxy": "^4.1.3",
"body-parser": "^1.19.0",
"cheerio": "^1.0.0-rc.5",
"express": "^4.17.1",
"jssoup": "0.0.12",
"shelljs": "^0.8.4"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require("fs");
const shell = require("shelljs");
const crypto = require("crypto");
const JSSoup = require("jssoup").default;
const cheerio = require("cheerio");

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

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

const soup = new JSSoup(body);
const scriptArray = soup.findAll("script");
const $ = cheerio.load(body);
const scriptArray = $("script");
if (!scriptArray?.length) {
return;
}
let alreadyInjectHookContext = false;
for (let script of scriptArray) {

// 对于是src引用的外部,其标签内容都会被忽略
if (script.attrs.src) {
if (script.attribs.src) {
continue;
}
const jsCode = script.contents.join("\n");
if (!script.contents.length || !jsCode) {

// 空script
if (!script.children.length) {
continue;
}
// 要保留住原来的属性
let oldAttrs = "";
for(let key in script.attrs) {
oldAttrs += ` "${key.replace("\"", "\\\"")}"="${script.attrs[key].replace("\"", "\\\"")}" `

// script的内容
let jsCode = "";
for(let child of script.children) {
jsCode += child.data;
}
if (!jsCode) {
return;
}

let newJsCode = injectHook(jsCode);
// 随着script替换时注入,不创建新的script标签
if (!alreadyInjectHookContext) {
newJsCode = loadPluginsAsStringWithCache() + newJsCode;
alreadyInjectHookContext = true;
}
script.replaceWith("<script "+ oldAttrs +">" + newJsCode + "</script>")

const newScript = cheerio.load("<script>" + newJsCode + "</script>")("script");
newScript.attribs = script.attribs;
$(script).replaceWith(newScript);
}
responseDetail.response.body = soup.prettify();
responseDetail.response.body = $.html();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ function loadPluginsAsStringWithCache() {
function loadPluginsAsString() {

// 用来保证Hook代码只被加载一次
// TODO 妥善处理Worker环境
const loadOnce = "\n" +
// 在Worker环境下会出现window未定义
"if (!window || window.cc11001100_hook_done) {\n" +
" return;\n" +
"}\n" +
"window.cc11001100_hook_done = true;\n\n";
" if (!window) {\n" +
" return; \n" +
" } \n" +
" if (window.cc11001100_hook_done) {\n" +
" return;\n" +
" }\n" +
" window.cc11001100_hook_done = true;\n\n";

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

Expand All @@ -42,7 +45,7 @@ function loadPluginsAsString() {
pluginsJsContentArray.push(pluginJsContent);
}

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

loadOnce +
Expand Down
Loading

0 comments on commit 5bdf946

Please sign in to comment.