|
| 1 | +// Import fetchLeetCodeTags from leetcodeApi.js |
| 2 | +// (Assumes leetcodeApi.js is loaded before this script as per manifest) |
| 3 | + |
| 4 | +async function getProblemData() { |
| 5 | + const titleEl = document.querySelector('div[class*="text-title-large"]'); |
| 6 | + const title = titleEl ? titleEl.textContent.trim() : "Unknown Title"; |
| 7 | + |
| 8 | + const difficultyEl = Array.from(document.querySelectorAll('div[class*="text-difficulty"]')).find((el) => |
| 9 | + el.textContent?.match(/Easy|Medium|Hard/) |
| 10 | + ); |
| 11 | + const difficulty = difficultyEl |
| 12 | + ? difficultyEl.textContent.trim() |
| 13 | + : "Unknown Difficulty"; |
| 14 | + |
| 15 | + const pathParts = window.location.pathname.split("/").filter(Boolean); |
| 16 | + const problemSlug = pathParts[1] || "unknown-problem"; |
| 17 | + |
| 18 | + // Fetch tags using the API helper |
| 19 | + let tags = []; |
| 20 | + try { |
| 21 | + tags = await fetchLeetCodeTags(problemSlug); |
| 22 | + } catch (e) { |
| 23 | + tags = []; |
| 24 | + } |
| 25 | + |
| 26 | + // Only return valid data if title and slug are valid |
| 27 | + if (!title || title === "Unknown Title" || !problemSlug || problemSlug === "unknown-problem") { |
| 28 | + return null; |
| 29 | + } |
| 30 | + |
| 31 | + const problemData = { |
| 32 | + title, |
| 33 | + difficulty, |
| 34 | + slug: problemSlug, |
| 35 | + url: window.location.href, |
| 36 | + timestamp: Date.now(), |
| 37 | + tags, |
| 38 | + }; |
| 39 | + |
| 40 | + console.log("LC Problem Detected:", problemData); |
| 41 | + return problemData; |
| 42 | +} |
| 43 | + |
| 44 | +// Wait for content and store problem data (with tags) |
| 45 | +function waitForContentAndStore() { |
| 46 | + const observer = new MutationObserver(async () => { |
| 47 | + const titleEl = document.querySelector("div"); |
| 48 | + if (titleEl && titleEl.textContent.trim()) { |
| 49 | + observer.disconnect(); |
| 50 | + const data = await getProblemData(); |
| 51 | + if (!data) return; // Don't store invalid/undefined problems |
| 52 | + browser.storage.local.set({ [data.slug]: data }).then(() => { |
| 53 | + console.log("Saved to storage:", data); |
| 54 | + }).catch((err) => { |
| 55 | + console.error("Storage error:", err); |
| 56 | + }); |
| 57 | + // Start watching for submission result after we have the slug |
| 58 | + waitForSubmissionResult(data.slug); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + observer.observe(document.body, { childList: true, subtree: true }); |
| 63 | +} |
| 64 | + |
| 65 | +function waitForSubmissionResult(slug) { |
| 66 | + const observer = new MutationObserver(() => { |
| 67 | + const resultEl = document.querySelector('span[data-e2e-locator="submission-result"]'); |
| 68 | + if (resultEl && resultEl.textContent.includes("Accepted")) { |
| 69 | + console.log("✅ Accepted detected via submission result!"); |
| 70 | + |
| 71 | + browser.storage.local.get(slug).then((existing) => { |
| 72 | + const data = existing[slug] || {}; |
| 73 | + data.status = "Solved"; |
| 74 | + data.solvedAt = Date.now(); |
| 75 | + browser.storage.local.set({ [slug]: data }) |
| 76 | + |
| 77 | + browser.runtime.sendMessage({ type: 'PROBLEM_SOLVED', slug}); |
| 78 | + observer.disconnect(); |
| 79 | + }); |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + observer.observe(document.body, {childList: true, subtree: true}); |
| 84 | +} |
| 85 | + |
| 86 | +// Fix: browser.runtime.onMessage must handle async getProblemData |
| 87 | +browser.runtime.onMessage.addListener((message, sender, sendResponse) => { |
| 88 | + if (message.type === 'GET_PROBLEM_DATA') { |
| 89 | + getProblemData().then((data) => { |
| 90 | + sendResponse(data); |
| 91 | + }); |
| 92 | + return true; // Indicate async response |
| 93 | + } |
| 94 | +}); |
| 95 | + |
| 96 | +waitForContentAndStore(); |
0 commit comments