-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
71 lines (62 loc) · 2.47 KB
/
popup.js
File metadata and controls
71 lines (62 loc) · 2.47 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// popup.js — robust version
const toggle = document.getElementById("toggle");
const openSide = document.getElementById("openSide");
const modeSel = document.getElementById("mode");
const langSel = document.getElementById("lang");
function getActiveTab() {
return new Promise((resolve) => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
resolve(tabs && tabs[0] ? tabs[0] : null);
});
});
}
function isSupportedUrl(url = "") {
// content_scripts only match *://*/* and won't inject into chrome://, chromewebstore://, etc.
return /^(https?:|file:)/i.test(url);
}
// Initialize UI
chrome.storage.local.get({ learningMode: false, highlightMode: "basic" }, (res) => {
if (toggle) toggle.checked = !!res.learningMode;
if (modeSel) modeSel.value = res.highlightMode || "basic";
});
chrome.storage.local.get({ targetLang: "zh-CN" }, (res) => {
if (langSel) langSel.value = res.targetLang;
});
async function notifyContent(payload) {
const tab = await getActiveTab();
if (!tab || !isSupportedUrl(tab.url)) {
// On unsupported pages, show a friendly warning instead of throwing an error
console.warn("LexiLearn: current page doesn't allow content scripts.", tab?.url);
// Optionally you could display this message in the popup
// document.body.insertAdjacentHTML("beforeend", "<p style='color:#c00'>Open a normal webpage (http/https) and try again.</p>");
return;
}
chrome.tabs.sendMessage(tab.id, { type: "PREFS_CHANGED", payload }, () => {
// swallow possible lastError (e.g. page just refreshed and script not injected yet)
void chrome.runtime.lastError;
});
}
// Learning Mode toggle
toggle.addEventListener("change", async () => {
await chrome.storage.local.set({ learningMode: toggle.checked });
await notifyContent({ learningMode: toggle.checked });
});
// Highlight mode dropdown
if (modeSel) {
modeSel.addEventListener("change", async () => {
await chrome.storage.local.set({ highlightMode: modeSel.value });
await notifyContent({ highlightMode: modeSel.value });
});
}
// Listen for language changes, save & notify the current page
if (langSel) {
langSel.addEventListener("change", async () => {
await chrome.storage.local.set({ targetLang: langSel.value });
await notifyContent({ targetLang: langSel.value });
});
}
// Open Side Panel
openSide.addEventListener("click", async (e) => {
e.preventDefault();
await chrome.sidePanel.open({ windowId: (await chrome.windows.getCurrent()).id });
});