-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbg.js
368 lines (268 loc) · 8.59 KB
/
bg.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Page size inspector - background page
// by tomi.mickelsson@iki.fi
// current tab being inspected
var gtabid = 0;
var is_loading = false;
var is_cache_disabled = false;
// map of tabid -> requests
var netList = {};
// list of requestIds that were cached
var cachedList = {};
//--------------------------------------------------------------------------
// chrome events
// tab changed
chrome.tabs.onActivated.addListener(function(info) {
// seems chrome.tabs.get always causes exception...
return;
chrome.tabs.get(info.tabId, function(tab) {
tab_updated(tab.id);
});
});
// tab deleted
chrome.tabs.onRemoved.addListener(function(tabid){
clear_tab_data(tabid);
});
// page load start
// https://developer.chrome.com/extensions/webNavigation
chrome.webNavigation.onBeforeNavigate.addListener(function(details) {
if (details.frameId === 0 && details.tabId == gtabid) {
// main frame loaded
deb("onStart");
is_loading = true;
clear_tab_data(details.tabId);
// remember time
var t = details.timeStamp;
netList[details.tabId+"t0"] = t;
}
});
// page completed
chrome.webNavigation.onCompleted.addListener(function(details) {
if (details.frameId === 0 && details.tabId == gtabid) {
deb("onCompleted");
is_loading = false;
chrome.runtime.sendMessage({load_completed: 1});
// remember time
var t = details.timeStamp;
netList[details.tabId+"t1"] = t;
}
});
//--------------------------------------------------------------------------
// methods
function update_badge(tabid) {
var is_active = (gtabid !== 0 && (gtabid == tabid));
var count = get_requests_count(tabid);
var txt = is_active ? ""+count : "";
chrome.browserAction.setBadgeText({"text":txt});
var inred = count >= 100;
var col = inred ? "#a00" : "#1656E4";
chrome.browserAction.setBadgeBackgroundColor({"color":col});
var iconname = is_active ? "icon.png" : "icon0.png";
chrome.browserAction.setIcon({path:iconname});
}
function tab_updated(tabid) {
deb('active tab: '+tabid);
update_badge(tabid);
}
// remove all network requests of this tab
function clear_tab_data(tabid) {
// deb("cleared "+tabid);
delete netList[tabid+"list"];
delete netList[tabid];
cachedList = {};
}
// add or get a single request item from list
function get_item(tabid, reqid) {
if (!netList[tabid])
netList[tabid] = {};
if (!netList[tabid+"list"])
netList[tabid+"list"] = [];
var obj = netList[tabid][reqid];
if (!obj) {
obj = {};
netList[tabid][reqid] = obj;
}
return obj;
}
// add a request to the list
function add_file(tabid, reqid, url, code, from_cache, type) {
if (url && url.startsWith("chrome-extension:"))
return;
var urlpart = url.split("/").pop();
// is request cached even though from_cache = false!?
if (cachedList[reqid])
from_cache = true;
// deb("add", reqid, urlpart, "cached:", from_cache);
var obj = get_item(tabid, reqid);
obj["url"] = url;
obj["code"] = code;
obj["type"] = type;
obj["req"] = reqid;
obj["cached"] = from_cache;
obj["size"] = 0;
// add to request list
netList[tabid+"list"].push(obj);
chrome.runtime.sendMessage({file: url}, function(response) {
});
}
// set size of the item
function set_file_size(tabid, reqid, size, is_chunk) {
size = isNaN(size) ? 0 : size || 0;
var obj = get_item(tabid, reqid);
// var isdoub = (is_chunk && obj.size);
if (size)
obj.size = size + (is_chunk? (obj.size || 0) : 0);
// if (isdoub)
// deb(" double ", reqid, size, obj.size);
// else
// deb(" single ", reqid, size, obj.size);
}
// provide list of network requests
function get_tab_network_requests(tabid) {
tabid = parseInt(tabid);
return netList[tabid+"list"];
}
function get_tab_load_time(tabid) {
var t0 = netList[tabid+"t0"];
var t1 = netList[tabid+"t1"];
return t1-t0;
}
// count non-cached network request
function get_requests_count(tabid) {
tabid = parseInt(tabid);
var c = 0;
var list = netList[tabid+"list"];
if (list && list.length) {
for (const obj of list) {
if (!obj.cached)
c++;
}
}
return c;
}
//--------------------------------------------------------------------------
// debugger logic
chrome.debugger.onDetach.addListener(onDetachDebugger);
chrome.debugger.onEvent.addListener(onNetworkEvent);
function startDebugger(tabid) {
deb("startDebugger");
if (gtabid) {
// first detach from current tab
stopDebugger(gtabid);
}
var version = "1.0";
var cb = onAttachDebugger.bind(null, tabid);
chrome.debugger.attach({"tabId":tabid}, version, cb);
if (chrome.runtime.lastError)
return false;
chrome.debugger.sendCommand({tabId: tabid}, "Network.enable");
return true;
}
function stopDebugger(tabid) {
deb("stopDebugger");
gtabid = 0;
is_cache_disabled = false;
// does not call onDetachDebugger!
chrome.debugger.detach({tabId:tabid});
update_badge(tabid);
}
function setCache(tabid, state) {
deb("setCache ", state);
// if (!is_debugger_on)
// startDebugger(tabid);
is_cache_disabled = state;
chrome.debugger.sendCommand(
{ tabId: tabid },
"Network.setCacheDisabled", { "cacheDisabled": state });
}
function reload(tabid) {
deb("reload");
chrome.debugger.sendCommand(
{ tabId: tabid },
"Page.reload", { "ignoreCache": false });
}
function navigate(tabid, url) {
deb("navigate");
chrome.debugger.sendCommand(
{ tabId: tabid },
"Page.navigate", { "url": url });
}
function getState(tabid) {
var r = {"is_debugger_on": gtabid==tabid,
"is_cache_disabled":is_cache_disabled,
"is_loading":is_loading};
// deb("state", tabid, gtabid, r);
return r;
}
function onAttachDebugger(tabid) {
if (chrome.runtime.lastError) {
var msg = chrome.runtime.lastError.message;
// err(msg);
chrome.runtime.sendMessage({"attach_error":msg});
} else {
deb( "onAttach ok");
gtabid = tabid;
}
}
function onDetachDebugger(source, reason) {
err("onDetach");
is_cache_disabled = false;
// ask to refresh popup
chrome.runtime.sendMessage({load_completed: 1});
gtabid = 0;
update_badge(0); // can be zero
}
//https://chromedevtools.github.io/debugger-protocol-viewer/tot/Network/
function onNetworkEvent(debuggeeId, message, params) {
var tabid = debuggeeId.tabId;
if (message == "Network.responseReceived") {
// remember request id + url
var resp = params.response;
var reqid = params.requestId;
// resp.encodedDataLength -1!
// resp.fromDiskCache may be false even though came from cache!
// A Chrome bug? Double check with Network.requestServedFromCache
add_file(tabid, reqid, resp.url, resp.status, resp.fromDiskCache,
params.type);
// console.debug("responseReceived", resp.url, resp.fromDiskCache,
// resp.encodedDataLength);
/*
if (resp.fromDiskCache) {
chrome.debugger.sendCommand({tabId: tabid},
"Network.getResponseBody", {"requestId": reqid},
function(reply) {
if (!reply)
return;
var size = reply.body? reply.body.length : 0;
if (!size)
size = reply.base64Encoded? reply.base64Encoded.length : 0;
set_file_size(tabid, reqid, size);
console.debug(" BODY", size);
});
}*/
} else if (message == "Network.requestServedFromCache") {
// fired if request ended up loading from cache.
//deb("fromcache" + params.requestId);
cachedList[params.requestId] = 1;
} else if (message == "Network.dataReceived") {
// console.debug("data", params);
// a chunk received!
var reqid = params.requestId
var size = params.dataLength; // is uncompressed!
// var size = params.encodedDataLength; // zero!
set_file_size(tabid, reqid, size, 1);
update_badge(tabid);
} else if (message == "Network.loadingFinished") {
var reqid = params.requestId
var size = params.encodedDataLength;
set_file_size(tabid, reqid, size);
update_badge(tabid);
}
// else {
// deb(message, params);
// }
}
function err(msg) {
console.debug("ERR "+ msg);
}
deb = console.debug;
deb('inspector loaded');