-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathintercept.js
51 lines (47 loc) · 1.84 KB
/
intercept.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
(() => {
const XHR = XMLHttpRequest.prototype;
const open = XHR.open;
const send = XHR.send;
XHR.open = function(method, url) {
this.method = method;
this.url = url;
return open.apply(this, arguments);
};
XHR.send = function() {
this.addEventListener('load', function() {
try {
if (this.url.includes('/api/onlineJudge/submitCode') &&
this.method.toLowerCase() === 'post') {
const response = JSON.parse(this.responseText);
const submissionData = {
success: response.success,
totalTestCases: response.failedTestCase?.totalTestCases || 0,
results: (response.results || []).map(result => ({
time: parseFloat(result.time),
memoryStorage: result.memoryStorage
}))
};
if (submissionData.results.length > 0) {
const avgTime = submissionData.results.reduce((sum, r) => sum + r.time, 0) /
submissionData.results.length;
const avgMemory = submissionData.results.reduce((sum, r) => sum + r.memoryStorage, 0) /
submissionData.results.length;
submissionData.averageTime = `${avgTime.toFixed(3)}ms`;
submissionData.averageMemory = `${avgMemory.toFixed(2)}KB`;
} else {
submissionData.averageTime = '0ms';
submissionData.averageMemory = '0KB';
}
// Send data back to content script
window.postMessage({
type: 'SUBMISSION_RESPONSE',
payload: submissionData
}, '*');
}
} catch (error) {
console.error('Error in interceptor:', error);
}
});
return send.apply(this, arguments);
};
})();