-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
115 lines (93 loc) · 3.03 KB
/
popup.js
File metadata and controls
115 lines (93 loc) · 3.03 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
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
document.addEventListener("DOMContentLoaded", async () => {
const listElement = document.getElementById("list");
const metaElement = document.getElementById("meta");
const titleElement = document.getElementById("pageTitle");
const refreshButton = document.getElementById("refreshBtn");
const state = globalThis.PopupState.createState();
async function copyText(value) {
if (!value) {
return;
}
try {
if (navigator.clipboard?.writeText) {
await navigator.clipboard.writeText(value);
return;
}
} catch {
// fall through to execCommand fallback
}
const textArea = document.createElement("textarea");
textArea.value = value;
document.body.appendChild(textArea);
textArea.select();
document.execCommand("copy");
textArea.remove();
}
function render() {
if (state.loading) {
globalThis.PopupRender.renderStatus(listElement, "Searching emails...", "status status-loading");
return;
}
globalThis.PopupRender.renderEmailList(listElement, state.emails, copyText);
}
async function getActiveTab() {
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
return tabs?.[0] || null;
}
async function requestStop(tabId) {
if (!tabId) {
return;
}
await chrome.tabs.sendMessage(tabId, { action: "stopScanning" }).catch(() => undefined);
}
async function injectScanner(tabId) {
await chrome.scripting.executeScript({
target: { tabId },
files: ["shared/email-utils.js", "content/scanner.js", "content.js"]
});
}
async function runScan() {
if (!state.activeTabId) {
globalThis.PopupState.setLoading(state, false);
globalThis.PopupState.setEmails(state, []);
render();
return;
}
globalThis.PopupState.setLoading(state, true);
render();
try {
await requestStop(state.activeTabId);
await injectScanner(state.activeTabId);
} catch {
globalThis.PopupState.setLoading(state, false);
globalThis.PopupState.setEmails(state, []);
render();
}
}
const onMessage = (message, sender) => {
if (message?.action !== "saveEmailsForTab") {
return;
}
if (sender.tab?.id && sender.tab.id !== state.activeTabId) {
return;
}
globalThis.PopupState.setEmails(state, message.emails || []);
globalThis.PopupState.setLoading(state, !message.done);
render();
};
chrome.runtime.onMessage.addListener(onMessage);
const activeTab = await getActiveTab();
globalThis.PopupState.setActiveTabId(state, activeTab?.id);
titleElement.textContent = activeTab?.title || "Email Finder";
metaElement.textContent = activeTab?.url || "No active tab";
refreshButton.addEventListener("click", runScan);
window.addEventListener("unload", () => {
chrome.runtime.onMessage.removeListener(onMessage);
if (state.activeTabId) {
chrome.tabs.sendMessage(state.activeTabId, { action: "stopScanning" }, () => {
void chrome.runtime.lastError;
});
}
});
await runScan();
});