-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
51 lines (43 loc) · 1.6 KB
/
Copy pathbackground.js
File metadata and controls
51 lines (43 loc) · 1.6 KB
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
chrome.runtime.onInstalled.addListener(async () => {
await chrome.sidePanel.setPanelBehavior({ openPanelOnActionClick: true });
});
function isLeetCodeProblemUrl(url) {
if (!url) return false;
try {
const u = new URL(url);
return (
(u.hostname === "leetcode.com" || u.hostname === "www.leetcode.com") &&
u.pathname.startsWith("/problems/")
);
} catch {
return false;
}
}
function normalizeTitle(tabTitle) {
if (!tabTitle) return "Untitled";
return tabTitle.replace(" - LeetCode", "").trim();
}
async function broadcast(type, payload) {
// Persist so side panel can load it anytime
if (type === "LC_META") await chrome.storage.local.set({ lastLcMeta: payload });
if (type === "LC_PROBLEM_TEXT") await chrome.storage.local.set({ lastLcProblemText: payload });
// Best-effort push to any open views (side panel). If none are open, no problem.
try {
chrome.runtime.sendMessage({ type, payload });
} catch {
// ignore
}
}
// Fallback detection from tab URL/title (works even if content script fails)
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
if (changeInfo.status !== "complete" && !changeInfo.url) return;
const url = tab.url || changeInfo.url;
if (!isLeetCodeProblemUrl(url)) return;
const meta = { title: normalizeTitle(tab.title), url };
await broadcast("LC_META", meta);
});
// Content script messages (for description/constraints)
chrome.runtime.onMessage.addListener((msg) => {
if (msg?.type === "LC_META") broadcast("LC_META", msg.payload);
if (msg?.type === "LC_PROBLEM_TEXT") broadcast("LC_PROBLEM_TEXT", msg.payload);
});