( details: chrome.webRequest.WebResponseCacheDetails )
| 232 | } |
| 233 | |
| 234 | const checkIfUserSolvedProblem = async ( |
| 235 | details: chrome.webRequest.WebResponseCacheDetails |
| 236 | ) => { |
| 237 | // If the user has already solved the problem, then don't do anything |
| 238 | if (await storage.getProblemSolved()) return |
| 239 | // Get the current active tab's URL |
| 240 | let currentURL = "" |
| 241 | try { |
| 242 | const [activeTab] = await new Promise<chrome.tabs.Tab[]>((resolve) => { |
| 243 | chrome.tabs.query({ active: true, currentWindow: true }, resolve) |
| 244 | }) |
| 245 | |
| 246 | currentURL = activeTab.url |
| 247 | } catch (error) { |
| 248 | console.error("Error getting active tab:", error) |
| 249 | return |
| 250 | } |
| 251 | |
| 252 | const problemUrl = await storage.getProblemUrl() |
| 253 | |
| 254 | const sameUrl = |
| 255 | problemUrl === currentURL || problemUrl + "description/" === currentURL |
| 256 | |
| 257 | if (!sameUrl) { |
| 258 | return |
| 259 | } |
| 260 | |
| 261 | //lastCheckedUrl = details.url |
| 262 | //lastCheckedTimestamp = now |
| 263 | |
| 264 | if (state.solvedListenerActive) { |
| 265 | // Remove the listener so that it doesn't fire again, since the outcome will either be success or fail |
| 266 | // And we'll add it again when the user clicks submit |
| 267 | state.solvedListenerActive = false |
| 268 | chrome.webRequest.onCompleted.removeListener(checkIfUserSolvedProblem) |
| 269 | } |
| 270 | |
| 271 | if (isSubmissionSuccessURL(details.url)) { |
| 272 | try { |
| 273 | const hyperTortureMode = await getHyperTortureMode() |
| 274 | const response = await fetch(details.url) |
| 275 | const data = await response.json() |
| 276 | if (data.state === "STARTED" || data.state === "PENDING") { |
| 277 | if (!state.solvedListenerActive) { |
| 278 | state.solvedListenerActive = true |
| 279 | chrome.webRequest.onCompleted.addListener(checkIfUserSolvedProblem, { |
| 280 | urls: ["*://leetcode.com/submissions/detail/*/check/"] |
| 281 | }) |
| 282 | } |
| 283 | return |
| 284 | } |
| 285 | if (data.status_msg !== "Accepted") { |
| 286 | if (hyperTortureMode) { |
| 287 | await resetHyperTortureStreak() |
| 288 | sendUserFailedMessage() |
| 289 | } |
| 290 | return |
| 291 | } |
nothing calls this directly
no test coverage detected