forked from RishiRaj22/coding_shout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
134 lines (127 loc) · 4.11 KB
/
Copy pathscript.js
File metadata and controls
134 lines (127 loc) · 4.11 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
"use strict";
var listening = [];
// For cross-browser compatibility
window.browser = (function () {
return window.msBrowser ||
window.browser ||
window.chrome;
})();
// For cross-browser Text to Speech
var speak = (function(text) {
if(browser.tts) {
browser.tts.speak(text);
} else {
speechSynthesis.speak(new SpeechSynthesisUtterance(text));
}
});
/**
* Fetch the result of codechef submission
* @param {string} id The ID of the codechef submission whose result is to be fetched
*/
function fetch_codechef_result(id) {
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://www.codechef.com/get_submission_status/" + id, true);
xhr.send();
xhr.onload = function(e) {
var result = JSON.parse(xhr.response);
if(result.result_code === 'wait') {
setTimeout(function() {
fetch_codechef_result(id);
},200);
} else {
browser.storage.sync.get({
sound: 'tts',
type: 'all'
}, function(items) {
var sound = items.sound;
var type = items.type;
if(result.result_code === 'time') {
result.result_code = 'Time Limit Exceeded';
}
result.result_code = result.result_code
.split("_")
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(" ");
notify(result.result_code,result.score,result.time,null,result.id);
});
listening.splice(listening.indexOf(id),1);
}
};
}
/**
* Handles messages sent from content-scripts running on Codeforces and AtCoder
* @param {Object} request Contains verdict, time taken, memory consumed by the submission
* @param {*} sender
* @param {*} sendResponse
*/
function handleMessage(request, sender, sendResponse) {
var verdict = request.verdict;
var time = request.time;
var mem = request.mem;
var id = request.id;
var score = request.score;
notify(verdict,score,time,mem,id);
}
/**
* Notifies about the result of the submission
* @param {string} verdict The result of the submission.
* @param {*} score The score obtained through the submission (optional)
* @param {*} time The time taken in code execution (optional)
* @param {*} mem The memory required in code execution (optional)
* @param {string} id The id of the submission (optional)
*/
function notify(verdict,score,time,mem,id) {
var details = [];
if (score != null && typeof score != 'undefined') {
details.push("Score " + score);
}
if (time != null && typeof time != 'undefined') {
details.push("Time taken " + time);
}
if (mem != null && typeof mem != 'undefined') {
details.push("Memory used " + mem);
}
details = details.join("\n");
browser.storage.sync.get({
sound: 'tts',
type: 'all'
}, function(items) {
var sound = items.sound;
var type = items.type;
if(sound === "tts") {
var message = verdict;
if(type === "all") {
message += "\n"+details;
}
speak(message);
} else {
//If ID is there, use that for a unique ID or else generate a random one
if(typeof id == 'undefined') {
id = Math.random().toString(36);
}
browser.notifications.create(id, {
type: "basic",
iconUrl: "icon_128.png",
title: verdict,
message: details
})
}
});
}
browser.runtime.onMessage.addListener(handleMessage);
/**
* As soon as there is a request for the result of a submission from Codechef,
* start fetching the result for that particular ID
*/
browser.webRequest.onBeforeRequest.addListener(function (request) {
var url = request.url;
var arr = url.split("/");
var id = arr[arr.length - 1];
if(id === "") {
id = arr[arr.length-2];
}
if(!listening.includes(id)) {
listening.push(id);
fetch_codechef_result(id);
}
},{"urls":["*://*.codechef.com/submit/complete/*"]})