-
Notifications
You must be signed in to change notification settings - Fork 4
/
background.js
108 lines (99 loc) · 3.1 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
let currentTabId;
let version = "1.0";
let server = '';
const requests = new Map();
chrome.runtime.onMessage.addListener((request,sender,sendResponse)=>{
if(currentTabId){
chrome.debugger.detach({tabId:currentTabId})
}
currentTabId = parseInt(request.id);
if(currentTabId<0) {
return;
}
server = request.server;
chrome.debugger.attach({
tabId: currentTabId
}, version, onAttach.bind(null, currentTabId));
chrome.debugger.onDetach.addListener(debuggerDetachHandler)
console.log("attach")
sendResponse({status:0});
})
function debuggerDetachHandler(){
console.log("detach")
requests.clear()
}
function onAttach(tabId) {
chrome.debugger.sendCommand({ //first enable the Network
tabId: tabId
}, "Network.enable");
chrome.debugger.onEvent.addListener(allEventHandler);
}
// https://chromedevtools.github.io/devtools-protocol/tot/Network
function allEventHandler(debuggeeId, message, params) {
if (currentTabId != debuggeeId.tabId) {
return;
}
// get request data
if(message == "Network.requestWillBeSent") {
if(params.request && filter(params.request.url)) {
const detail = new Map();
detail.set('request',params.request)
requests.set(params.requestId,detail)
}
}
// get response data
if (message == "Network.responseReceived") {
if(params.response && filter(params.response.url)){
const request = requests.get(params.requestId);
if( request===undefined ){
console.log(params.requestId,"#not found request")
return;
}
request.set("response",params.response)
chrome.debugger.sendCommand({
tabId: debuggeeId.tabId
}, "Network.getCookies", {
"urls": [params.response.url]
}, function(response) {
request.set("cookies",response.cookies)
});
requests.set(params.requestId,request)
}
}
if(message == "Network.loadingFinished") {
const request = requests.get(params.requestId);
if( request===undefined ){
console.log(params.requestId,"#not found request")
return;
}
chrome.debugger.sendCommand({
tabId: debuggeeId.tabId
}, "Network.getResponseBody", {
"requestId": params.requestId
}, function(response) {
if(response){
request.set("response_body",response)
requests.set(params.requestId,request)
sendData(request)
requests.delete(params.requestId)
}else{
console.log("empty")
}
});
}
}
function sendData(data) {
if(server.length===0){
return
}
fetch(server,{
method:'post',
headers:{
'Content-Type':'application/json'
},
body:JSON.stringify(Object.fromEntries(data))
})
}
function filter(url){
return url.startsWith("http") && !url.endsWith('css') && !url.endsWith('js')
}