Skip to content

Commit bb7098a

Browse files
added a clock
1 parent b8ce810 commit bb7098a

File tree

5 files changed

+107
-125
lines changed

5 files changed

+107
-125
lines changed

public/clock.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// contentScript.js
2+
3+
function createClock(remainingTimeSeconds) {
4+
const clockElement = document.createElement('div');
5+
clockElement.style.position = 'fixed';
6+
clockElement.style.top = '0';
7+
clockElement.style.left = '50%';
8+
clockElement.style.transform = 'translateX(-50%)';
9+
clockElement.style.backgroundColor = 'white';
10+
clockElement.style.padding = '10px';
11+
clockElement.style.fontFamily = 'Arial, sans-serif';
12+
clockElement.style.fontSize = '18px';
13+
clockElement.style.zIndex = '9999'; // Set a high z-index value
14+
15+
const updateClock = () => {
16+
const minutes = Math.floor(remainingTimeSeconds / 60);
17+
const seconds = remainingTimeSeconds % 60;
18+
clockElement.textContent = `Remaining Time: ${minutes}m ${seconds}s`;
19+
20+
if (remainingTimeSeconds <= 0) {
21+
clearInterval(timer);
22+
clockElement.textContent = 'Time is up!';
23+
}
24+
25+
remainingTimeSeconds--;
26+
};
27+
28+
updateClock();
29+
const timer = setInterval(updateClock, 1000);
30+
31+
document.body.appendChild(clockElement);
32+
}
33+
34+
createClock();

public/manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
"action": {
1313
"default_title": "Let's go!!",
1414
"default_popup": "ui/start_room/landingPage.html"
15-
}
15+
},
16+
"host_permissions": ["https://leetcode.com/*"]
1617
}

public/newtab.html

Lines changed: 0 additions & 38 deletions
This file was deleted.

public/newtab.js

Lines changed: 0 additions & 57 deletions
This file was deleted.

src/background/background.ts

Lines changed: 71 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,104 @@
1-
import { questions } from "./all_questions";
1+
import {questions} from "./all_questions";
22

33
const LEETCODE_PATH = 'https://leetcode.com/problems/';
4-
function getRandomNumbers(min: number, max: number, count: number): number[] {
4+
function getRandomNumbers(min : number, max : number, count : number): number[] {
55
const result: number[] = [];
6-
6+
77
for (let i = 0; i < count; i++) {
8-
const randomNum = Math.floor(Math.random() * (max - min + 1)) + min;
9-
result.push(randomNum);
8+
const randomNum = Math.floor(Math.random() * (max - min + 1)) + min;
9+
result.push(randomNum);
1010
}
11-
11+
1212
return result;
1313
}
1414

15-
function replaceAll(str: string, search: string, replacement: string): string {
15+
function replaceAll(str : string, search : string, replacement : string): string {
1616
return str.replace(new RegExp(search, 'g'), replacement);
17-
}
17+
}
18+
19+
20+
function createClock(remainingTimeSeconds : number) {
21+
const clockElement = document.createElement('div');
22+
clockElement.style.position = 'fixed';
23+
clockElement.style.top = '0';
24+
clockElement.style.left = '50%';
25+
clockElement.style.transform = 'translateX(-50%)';
26+
clockElement.style.backgroundColor = 'white';
27+
clockElement.style.padding = '10px';
28+
clockElement.style.fontFamily = 'Arial, sans-serif';
29+
clockElement.style.fontSize = '18px';
30+
clockElement.style.zIndex = '9999'; // Set a high z-index value
31+
32+
const updateClock = () => {
33+
const minutes = Math.floor(remainingTimeSeconds / 60);
34+
const seconds = remainingTimeSeconds % 60;
35+
clockElement.textContent = `Remaining Time: ${minutes}m ${seconds}s`;
36+
37+
if (remainingTimeSeconds <= 0) {
38+
clearInterval(timer);
39+
clockElement.textContent = 'Time is up!';
40+
}
41+
42+
remainingTimeSeconds--;
43+
};
44+
45+
updateClock();
46+
const timer = setInterval(updateClock, 1000);
47+
48+
document.body.appendChild(clockElement);
49+
}
50+
1851

1952
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
2053
console.log(message);
21-
if (message.type == 'StartContest'){
54+
if (message.type == 'StartContest') {
2255
const questionType = parseInt(message.data.question);
2356
const time = parseInt(message.data.time);
24-
var todoQuestions : string[] = [];
57+
var todoQuestions: string[] = [];
2558
const easyLength = questions['Easy'].length;
2659
const mediumLength = questions['Medium'].length;
2760
const hardLength = questions['Hard'].length;
2861

29-
switch(questionType){
30-
case 0:
31-
todoQuestions = [...getRandomNumbers(0, easyLength, 4).map(index => questions['Easy'][index])];
62+
switch (questionType) {
63+
case 0: todoQuestions = [... getRandomNumbers(0, easyLength, 4).map(index => questions['Easy'][index])];
3264
break;
33-
case 1:
34-
todoQuestions = [...getRandomNumbers(0, easyLength, 2).map(index => questions['Easy'][index])];
35-
todoQuestions = [...getRandomNumbers(0, mediumLength, 2).map(index => questions['Medium'][index])];
65+
case 1: todoQuestions = [... getRandomNumbers(0, easyLength, 2).map(index => questions['Easy'][index])];
66+
todoQuestions = [... getRandomNumbers(0, mediumLength, 2).map(index => questions['Medium'][index])];
3667
break;
37-
case 2:
38-
todoQuestions = [...getRandomNumbers(0, mediumLength, 4).map(index => questions['Medium'][index])];
68+
case 2: todoQuestions = [... getRandomNumbers(0, mediumLength, 4).map(index => questions['Medium'][index])];
3969
break;
40-
case 3:
41-
todoQuestions = [...getRandomNumbers(0, mediumLength, 2).map(index => questions['Medium'][index])];
42-
todoQuestions = [...getRandomNumbers(0, hardLength, 2).map(index => questions['Hard'][index])];
70+
case 3: todoQuestions = [... getRandomNumbers(0, mediumLength, 2).map(index => questions['Medium'][index])];
71+
todoQuestions = [... getRandomNumbers(0, hardLength, 2).map(index => questions['Hard'][index])];
4372
break;
44-
case 4:
45-
todoQuestions = [...getRandomNumbers(0, hardLength, 4).map(index => questions['Hard'][index])];
73+
case 4: todoQuestions = [... getRandomNumbers(0, hardLength, 4).map(index => questions['Hard'][index])];
4674
break;
4775
default:
4876
console.error("Wrong question type");
4977
}
50-
for(var questionTitle of todoQuestions){
78+
for (var questionTitle of todoQuestions) {
5179
questionTitle = replaceAll(questionTitle, ' ', '-');
5280
console.log(questionTitle);
53-
chrome.tabs.create({'url': `${LEETCODE_PATH}${questionTitle}`}, (tab) => {
54-
81+
82+
chrome.tabs.create({
83+
'url': `${LEETCODE_PATH}${questionTitle}`
84+
}, (tab) => {
85+
chrome.tabs.onUpdated.addListener(function onUpdatedListener(tabId, changeInfo) {
86+
if (tabId === tab.id && changeInfo.status === 'complete') {
87+
chrome.tabs.onUpdated.removeListener(onUpdatedListener); // Remove the listener
88+
chrome.scripting.executeScript({
89+
target: {
90+
tabId: tab !.id ?? -1
91+
},
92+
func: createClock,
93+
args : [time]
94+
});
95+
}
96+
});
5597
});
98+
5699
}
57-
58-
}
59-
else {
100+
101+
} else {
60102
console.error('non parsable');
61103
}
62104
});

0 commit comments

Comments
 (0)