-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
198 lines (174 loc) · 5.26 KB
/
background.js
File metadata and controls
198 lines (174 loc) · 5.26 KB
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
// background.js (ES module)
import {checks} from "./checks/index.js";
const MAX_LOGS = 2000;
const LOGS = [];
const ports = new Set();
let requestCount = 0;
const navStarts = new Map();
function pushLog(rec) {
LOGS.push(rec);
if (LOGS.length > MAX_LOGS) LOGS.shift();
for (const port of ports) {
try {
port.postMessage({type: "NEW_RECORD", record: rec});
} catch {
}
}
}
setInterval(() => {
for (const port of ports) {
try {
port.postMessage({type: "COUNT_REQUESTS", requestCount});
} catch {
}
}
}, 1000)
chrome.runtime.onConnect.addListener((port) => {
ports.add(port);
port.onDisconnect.addListener(() => ports.delete(port));
});
const starts = new Map();
const responseHeadersMap = new Map();
function toHeaderMap(headersArr = []) {
const m = {};
for (const h of headersArr) if (h?.name) m[h.name.toLowerCase()] = h.value ?? "";
return m;
}
chrome.webRequest.onBeforeRequest.addListener(
(d) => {
starts.set(d.requestId, {time: Date.now()});
},
{urls: ["<all_urls>"]}
);
chrome.webRequest.onHeadersReceived.addListener(
(d) => {
responseHeadersMap.set(d.requestId, toHeaderMap(d.responseHeaders || []));
},
{urls: ["<all_urls>"]},
["responseHeaders"]
);
async function runChecks(record, type = 'request') {
const failedChecks = [];
const checkGroup = checks[type];
for (const c of checkGroup) {
const res = await c.check(record);
if (res) failedChecks.push(res);
}
record.failedChecks = failedChecks;
pushLog(record);
}
chrome.webRequest.onCompleted.addListener(
async (d) => {
requestCount++
const s = starts.get(d.requestId);
const duration = s ? Date.now() - s.time : undefined;
let pageUrl = '';
let content = ''
if (d.tabId >= 0) {
try {
const tab = await chrome.tabs.get(d.tabId);
pageUrl = tab.url;
} catch (e) {
pageUrl = ''
}
}
const record = {
url: d.url,
method: d.method,
statusCode: d.statusCode,
type: d.type,
time: s ? s.time : Date.now(),
duration,
tabId: d.tabId,
frameId: d.frameId,
fromCache: !!d.fromCache,
ip: d.ip || undefined,
responseHeaders: responseHeadersMap.get(d.requestId),
pageUrl
};
await runChecks(record);
starts.delete(d.requestId);
responseHeadersMap.delete(d.requestId);
},
{urls: ["<all_urls>"]},
["responseHeaders"]
);
chrome.webRequest.onErrorOccurred.addListener(
async (d) => {
const s = starts.get(d.requestId);
const duration = s ? Date.now() - s.time : undefined;
const record = {
url: d.url,
method: d.method,
type: d.type,
time: s ? s.time : Date.now(),
duration,
tabId: d.tabId,
frameId: d.frameId,
error: d.error,
responseHeaders: responseHeadersMap.get(d.requestId),
content: ''
};
await runChecks(record);
starts.delete(d.requestId);
responseHeadersMap.delete(d.requestId);
},
{urls: ["<all_urls>"]}
);
chrome.webNavigation.onCommitted.addListener((details) => {
if (details.frameId === 0) { // nur main_frame
navStarts.set(details.tabId, Date.now());
}
});
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
if (changeInfo.status === "complete" && /^https?:/.test(tab.url)) {
const duration = Date.now() - navStarts.get(tabId);
navStarts.delete(tabId);
const [result] = await chrome.scripting.executeScript({
target: {tabId, frameIds: [0]}, // frameId 0 = main_frame
func: () => document.documentElement.outerHTML
});
const content = result?.result ?? '';
const record = {
url: tab.url,
pageUrl: tab.url,
time: Date.now(),
tabId: tab.id,
duration,
type: 'main_frame',
method: 'GET',
requestCount,
content
}
await runChecks(record, 'frame')
}
});
chrome.runtime.onMessage.addListener(async (msg, sender) => {
switch (msg?.type) {
case "GET_LOGS": {
let logs = [...LOGS];
if (typeof msg.filterTabId === "number") {
logs = logs.filter(r => r && r.tabId === msg.filterTabId);
}
logs.sort((a, b) => b.time - a.time);
return {success: true, logs};
}
case "CLEAR_LOGS": {
if (typeof msg.filterTabId === "number") {
for (let i = LOGS.length - 1; i >= 0; i--) {
if (LOGS[i]?.tabId === msg.filterTabId) LOGS.splice(i, 1);
}
} else {
LOGS.length = 0;
}
requestCount = 0;
return {success: true};
}
case "CLEAR_REQUEST_COUNT": {
requestCount = 0;
return {success: true};
}
default:
return {success: false, error: "Unknown message type"};
}
});