mirror of
https://git.mirrors.martin98.com/https://github.com/cilame/v_jstools
synced 2025-08-16 20:36:00 +08:00
add
This commit is contained in:
parent
1d3731a5d8
commit
82dc7a18e6
@ -4,6 +4,8 @@
|
||||
<title></title>
|
||||
<meta charset="utf-8">
|
||||
<script src="./tools/babel_asttool.js"></script>
|
||||
<script src="./tools/cheerio.js"></script>
|
||||
<script src="./tools/replacer.js"></script>
|
||||
<script src="background.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
|
@ -32,13 +32,23 @@ chrome.debugger.onEvent.addListener(function (source, method, params){
|
||||
sendCommand("Fetch.failRequest", { requestId: params.requestId, errorReason: params.responseErrorReason }, source);
|
||||
break; }
|
||||
sendCommand("Fetch.getResponseBody", { requestId: params.requestId }, source, function(result){
|
||||
var fillfunc = fillresponse.bind(null, params, source)
|
||||
if (result.body !== undefined){ // 收到的 result.body 是 base64(代码) 的代码,使用时需要解码一下
|
||||
chrome.storage.local.get(["config-fetch_hook"], function (res) {
|
||||
try{ fillresponse.bind(null, params, source)(btoa(eval((res["config-fetch_hook"]||'')+';fetch_hook')(atob(result.body), params.resourceType, params.request.url))) }
|
||||
catch(e){ fillresponse.bind(null, params, source)(result.body) }
|
||||
try{
|
||||
var respboby = atob(result.body)
|
||||
var replacer = eval((res["config-fetch_hook"]||'')+';fetch_hook')
|
||||
if (params.resourceType == 'Script'){ var replbody = btoa(replacer(respboby, params.request.url)) }
|
||||
if (params.resourceType == 'Document'){ var replbody = btoa(html_script_replacer(respboby, replacer, params.request.url)) }
|
||||
fillfunc(replbody)
|
||||
}
|
||||
catch(e){
|
||||
// hook 修改失败,将错误信息传入前端,直接在前端反馈错误信息
|
||||
fillfunc(result.body)
|
||||
}
|
||||
})
|
||||
return }
|
||||
fillresponse.bind(null, params, source)(result.body) // body 只能传 base64(指定代码)
|
||||
fillfunc(result.body) // body 只能传 base64(指定代码)
|
||||
});
|
||||
break; }
|
||||
}
|
||||
@ -53,11 +63,11 @@ function AttachDebugger() {
|
||||
function (tabs) {
|
||||
var currtab = { tabId: tabs[0].id };
|
||||
chrome.debugger.attach(currtab, "1.2", function () {
|
||||
// Document, Stylesheet, Image, Media, Font, Script, TextTrack, XHR, Fetch, EventSource, WebSocket, Manifest, SignedExchange, Ping, CSPViolationReport, Preflight, Other
|
||||
sendCommand("Network.enable", {}, currtab, function(){ sendCommand("Network.setCacheDisabled", {cacheDisabled: true}, currtab)} ) // 确保 Fetch.getResponseBody 一定能收到东西
|
||||
sendCommand("Fetch.enable", { patterns: [
|
||||
{urlPattern:"*",resourceType:"Script",requestStage:"Response"}, // 暂时先只 hook Script 类型的脚本
|
||||
// {urlPattern:"*",resourceType:"Document",requestStage:"Response"},
|
||||
// Document, Stylesheet, Image, Media, Font, Script, TextTrack, XHR, Fetch, EventSource, WebSocket, Manifest, SignedExchange, Ping, CSPViolationReport, Preflight, Other
|
||||
{urlPattern:"*",resourceType:"Script",requestStage:"Response"}, // 暂时先只 hook 少量携带 js 数据类型的请求
|
||||
{urlPattern:"*",resourceType:"Document",requestStage:"Response"},
|
||||
] }, currtab);
|
||||
});
|
||||
}
|
||||
|
@ -115,7 +115,7 @@
|
||||
<div>能直接用插件的方式实现 ast 修改代码,用处还是非常大的。</div>
|
||||
<HR>
|
||||
<div>
|
||||
请定义一个名字为 fetch_hook 函数,接受参数为 1.代码 2.类型 3.url,return 修改后的代码
|
||||
请定义一个名字为 fetch_hook 函数,接受参数为 1.代码 2.url,return 修改后的代码
|
||||
</div>
|
||||
<textarea id='fetch_hook' data-key="config-fetch_hook" style="width: 100%; height: 500px"></textarea>
|
||||
</section>
|
||||
|
1
tools/cheerio.js
Normal file
1
tools/cheerio.js
Normal file
File diff suppressed because one or more lines are too long
27
tools/replacer.js
Normal file
27
tools/replacer.js
Normal file
@ -0,0 +1,27 @@
|
||||
function html_script_replacer(body, replacer, url){
|
||||
var html = cheerio.load(body)
|
||||
var scripts = html("script");
|
||||
if (!scripts.length){
|
||||
return
|
||||
}
|
||||
for (var i = 0; i < scripts.length; i++) {
|
||||
var script = scripts[i]
|
||||
if (script.attribs.src){
|
||||
continue
|
||||
}
|
||||
if (!script.children.length){
|
||||
continue
|
||||
}
|
||||
var jscode = ''
|
||||
for (var i = 0; i < script.children.length; i++) {
|
||||
jscode += script.children[i].data
|
||||
}
|
||||
if (!jscode){
|
||||
return
|
||||
}
|
||||
var newscript = cheerio.load("<script>" + replacer(jscode, url) + "</script>")("script");
|
||||
newscript.attribs = script.attribs;
|
||||
html(script).replaceWith(newscript);
|
||||
}
|
||||
return html.html()
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user