-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
137 lines (120 loc) · 3.79 KB
/
background.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
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
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
if (
changeInfo.status === "complete" &&
/leetcode\.com\/problems\//.test(tab.url)
) {
chrome.scripting.executeScript({
target: { tabId: tabId },
files: ["content.js"],
});
}
});
chrome.webRequest.onBeforeRequest.addListener(
async function (details) {
if (details.method === "POST" && details.requestBody) {
const rawBody = details.requestBody.raw[0]?.bytes;
if (rawBody) {
const decodedBody = new TextDecoder().decode(new Uint8Array(rawBody));
try {
const bodyObj = JSON.parse(decodedBody);
if (bodyObj && bodyObj.operationName == "updateSubmissionNote") {
// [ Time taken: 1 m 9 s ]
const note = bodyObj.variables.note;
const timeTken = calculateTImeTaken(note);
console.log("Time taken: ", timeTken);
if (timeTken == 0) return;
const question_details = await getQuestionDetails();
console.log(question_details);
storeQuestionSolvedDetails(question_details, timeTken);
}
} catch (e) {
console.warn("Request body is not valid JSON:", decodedBody);
}
}
}
},
{ urls: ["*://leetcode.com/graphql*"] },
["requestBody"]
);
const getQuestionDetails = () => {
return new Promise((resolve, reject) => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs.length > 0) {
const tabId = tabs[0].id;
chrome.tabs.sendMessage(
tabId,
{ action: "fetchQuestionDetails" },
function (response) {
if (response) {
resolve(response);
} else {
resolve({ title: "0", questionId: 0, difficulty: "Easy" });
}
}
);
} else {
reject(new Error("No active tab found"));
}
});
});
};
const calculateTImeTaken = (note) => {
console.log(note);
const parts = note.split("[ Time taken:");
if (parts.length < 2) {
console.warn("Unable to parse: ", note);
return;
}
const timePart = parts[1].replace("]", "").trim();
let totalSeconds = 0;
const minuteMatch = timePart.match(/(\d+)\s*m/); // Matches "X m"
const secondMatch = timePart.match(/(\d+)\s*s/); // Matches "Y s"
if (minuteMatch) {
totalSeconds += parseInt(minuteMatch[1], 10) * 60;
}
if (secondMatch) {
totalSeconds += parseInt(secondMatch[1], 10);
}
return totalSeconds;
};
const fetchQuestionsSolvedArr = (key) => {
return new Promise((resolve, reject) => {
chrome.storage.local.get([key], (result) => {
if (chrome.runtime.lastError) {
resolve([]);
} else {
console.log(`Array retrieved for key "${key}":`, result[key]);
resolve(result[key]);
}
});
});
};
const storeQuestionArr = (key, array) => {
chrome.storage.local.set({ [key]: array }, () => {
if (chrome.runtime.lastError) {
console.error("Error storing data:", chrome.runtime.lastError);
} else {
console.log(`Array stored under key "${key}":`, array);
}
});
};
const key = "questionSolvedDetails";
const storeQuestionSolvedDetails = async (question_details, timeTken) => {
if (!question_details || question_details?.title == "0") return;
try {
const obj = {
questionId: question_details.questionId,
title: question_details.title,
difficulty: question_details.difficulty,
timeTaken: timeTken,
date: JSON.stringify(new Date()),
};
const data = await fetchQuestionsSolvedArr(key);
const updatedArray = data ? [...data, obj] : [obj];
storeQuestionArr(key, updatedArray);
} catch (error) {
console.log("Error while storing question details");
console.log(error);
}
};
console.log("Extension working!");