-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
107 lines (97 loc) · 2.23 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
recording=false
site=''
requests=[]
headers=[]
responses=[]
function download(name, data){
var result = JSON.stringify(data);
var url = 'data:application/json;base64,' + btoa(unescape(encodeURIComponent(result)));
chrome.downloads.download({
url: url,
filename: name
});
}
function downloadAll(){
download('web-requests.json', requests)
download('web-headers.json', headers)
download('web-responses.json', responses)
requests=[]
headers=[]
responses=[]
}
function messageHandler(msg, sender, sendResponse ){
if(msg==null || msg==undefined)
return false;
if(msg.msg=='record' && msg.site!==undefined){
site=msg.site
recording=true
sendResponse({msg:"ok"})
}
else if(msg.msg=='stop'){
site=''
recording=false
}
else if(msg.msg=='check'){
if(recording)
sendResponse({msg:"recording", site_: site})
else
sendResponse({msg:"not recording"})
}
else if(msg.msg=='download'){
downloadAll()
}
}
function checkName(url){
if((pos=url.indexOf('/'))<=6){
newUrl=url.substring(pos+2)
pos=newUrl.indexOf('/')
if(pos==-1)
return newUrl.indexOf(site)!==-1
else
return newUrl.substring(0,pos).indexOf(site)!==-1
}
else
return url.substring(0,pos).indexOf(site)!==-1
}
function recordRequest(requestDetails) {
if(!recording)
return
url=requestDetails.url
if(checkName(url)){
requests.push(requestDetails)
}
}
function recordHeaders(requestDetails) {
if(!recording)
return
url=requestDetails.url
if(checkName(url)){
headers.push(requestDetails)
}
}
function recordResponses(responseDetails){
if(!recording)
return
url=responseDetails.url
if(checkName(url)){
chrome.tabs.sendMessage(responseDetails.tabId, {msg: 'DOMContent'}, function(DOMContent){
responseDetails.httpResponse=DOMContent
responses.push(responseDetails)
})
}
}
chrome.webRequest.onBeforeRequest.addListener(
recordRequest,
{urls: ["<all_urls>"]},
["requestBody"]
)
chrome.webRequest.onBeforeSendHeaders.addListener(
recordHeaders,
{urls: ["<all_urls>"]},
["requestHeaders"]
)
chrome.webRequest.onCompleted.addListener(
recordResponses,
{urls: ["<all_urls>"]}
)
chrome.runtime.onMessage.addListener(messageHandler)