|
1 |
| -import { questions } from "./all_questions"; |
| 1 | +import {questions} from "./all_questions"; |
2 | 2 |
|
3 | 3 | 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[] { |
5 | 5 | const result: number[] = [];
|
6 |
| - |
| 6 | + |
7 | 7 | 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); |
10 | 10 | }
|
11 |
| - |
| 11 | + |
12 | 12 | return result;
|
13 | 13 | }
|
14 | 14 |
|
15 |
| -function replaceAll(str: string, search: string, replacement: string): string { |
| 15 | +function replaceAll(str : string, search : string, replacement : string): string { |
16 | 16 | 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 | + |
18 | 51 |
|
19 | 52 | chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
|
20 | 53 | console.log(message);
|
21 |
| - if (message.type == 'StartContest'){ |
| 54 | + if (message.type == 'StartContest') { |
22 | 55 | const questionType = parseInt(message.data.question);
|
23 | 56 | const time = parseInt(message.data.time);
|
24 |
| - var todoQuestions : string[] = []; |
| 57 | + var todoQuestions: string[] = []; |
25 | 58 | const easyLength = questions['Easy'].length;
|
26 | 59 | const mediumLength = questions['Medium'].length;
|
27 | 60 | const hardLength = questions['Hard'].length;
|
28 | 61 |
|
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])]; |
32 | 64 | 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])]; |
36 | 67 | 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])]; |
39 | 69 | 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])]; |
43 | 72 | 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])]; |
46 | 74 | break;
|
47 | 75 | default:
|
48 | 76 | console.error("Wrong question type");
|
49 | 77 | }
|
50 |
| - for(var questionTitle of todoQuestions){ |
| 78 | + for (var questionTitle of todoQuestions) { |
51 | 79 | questionTitle = replaceAll(questionTitle, ' ', '-');
|
52 | 80 | 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 | + }); |
55 | 97 | });
|
| 98 | + |
56 | 99 | }
|
57 |
| - |
58 |
| - } |
59 |
| - else { |
| 100 | + |
| 101 | + } else { |
60 | 102 | console.error('non parsable');
|
61 | 103 | }
|
62 | 104 | });
|
0 commit comments