forked from cilame/v_jstools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
115 lines (115 loc) · 4.75 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// importScripts("./tools/babel_asttool.js", "./tools/cheerio.js", "./tools/replacer.js", "./tools/error_front.js")
// chrome.contextMenus.create({
// id: "v_menu",
// title: "打开 v_jstools 动态调试",
// contexts: ['all']
// });
// chrome.contextMenus.onClicked.addListener(function(info, tab) {
// if (info.menuItemId == "v_menu") {
// AttachDebugger();
// }
// });
// background.js
chrome.contextMenus.create({
title: "打开 v_jstools 动态调试",
contexts: ['all'],
onclick: function(){
AttachDebugger();
}
});
function sendCommand(method, params, source, chainfun){
chrome.debugger.sendCommand(source, method, params, function(result){
if (chrome.runtime.lastError) {
console.error('chrome.runtime.lastError', chrome.runtime.lastError)
if (chrome.runtime.lastError.message.indexOf('Cannot access a chrome://') != -1){ attached = false }
} else { if (chainfun){ chainfun(result) } }
});
}
function fillresponse(params, source, body){
sendCommand("Fetch.fulfillRequest", {
requestId: params.requestId, responseCode: params.responseStatusCode, responseHeaders: params.responseHeaders,
body: body, // body 只能传 base64(指定代码)
}, source);
}
chrome.debugger.onEvent.addListener(function (source, method, params){
switch(method){
case "Fetch.requestPaused":
var itheaders = params.responseHeaders;
if (itheaders.find(function(v){return v.name == "Location"})) {
sendCommand("Fetch.continueRequest", { requestId: params.requestId, url: itheaders.value }, source);
break; }
if ((params.responseStatusCode || params.responseErrorReason)) {
if (params.responseErrorReason) {
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{
var respboby = decodeURIComponent(escape(atob(result.body)))
var replacer = eval((res["config-fetch_hook"]||'')+';fetch_hook')
if (params.resourceType == 'Script'){ var replbody = btoa(unescape(encodeURIComponent(replacer(respboby, params.request.url)))) }
if (params.resourceType == 'Document'){ var replbody = btoa(unescape(encodeURIComponent(html_script_replacer(respboby, replacer, params.request.url)))) }
fillfunc(replbody) }
catch(e){
send_error_info_to_front(e.stack, currtab.tabId, params.request.url)
fillfunc(result.body) }
})
return }
fillfunc(result.body) // body 只能传 base64(指定代码)
});
break; }
}
})
chrome.debugger.onDetach.addListener(function(){ attached = false })
var attached = false
var currtab;
function AttachDebugger() {
if (attached){ return }
attached = true
chrome.tabs.query(
{ active: true, currentWindow: true },
function (tabs) {
currtab = { tabId: tabs[0].id };
chrome.debugger.attach(currtab, "1.2", function () {
sendCommand("Network.enable", {}, currtab, function(){ sendCommand("Network.setCacheDisabled", {cacheDisabled: true}, currtab)} ) // 确保 Fetch.getResponseBody 一定能收到东西
sendCommand("Fetch.enable", { patterns: [
// 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);
});
}
);
}
var currtabid;
var currdomain;
function get_cookie(){
chrome.cookies.getAll({}, function(cookie){
var cookies = []
for (var i = 0; i < cookie.length; i++) {
var _domain = cookie[i].domain
if (_domain.charAt() == '.'){
_domain = _domain.slice(1)
}
if (currdomain.indexOf(_domain) != -1){
cookies.push(cookie[i])
}
}
chrome.tabs.sendMessage(currtabid, {action: {type:'getcookie', info: cookies}}, function(){})
});
}
chrome.extension.onMessage.addListener(function (req, sender, sendResponse){
if (req.getcookie){
currtabid = sender.tab.id
currdomain = req.domain
get_cookie()
sendResponse({})
}
})
chrome.cookies.onChanged.addListener(function(info){
if (currtabid){
get_cookie()
}
});