Skip to content

Commit 6f49eb9

Browse files
refactor: Enable background file processing and popup persistence
1 parent a942149 commit 6f49eb9

File tree

2 files changed

+33
-82
lines changed

2 files changed

+33
-82
lines changed

src/background.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,9 @@ export async function handleFileProcessing(
8181
});
8282

8383
return errorResult;
84+
} finally {
85+
chrome.runtime.sendMessage({
86+
type: "processComplete",
87+
});
8488
}
8589
}

src/popup.ts

Lines changed: 29 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,10 @@
11
import TokenStore from "./tokenStore";
22
import { ProcessResult, FileDetails } from "./types";
33

4-
document.addEventListener("DOMContentLoaded", async () => {
5-
try {
6-
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
7-
const tab = tabs[0];
8-
9-
if (tab.id) {
10-
const result = await chrome.scripting.executeScript({
11-
target: { tabId: tab.id },
12-
func: () => {
13-
const items: Record<string, string> = {};
14-
for (let i = 0; i < localStorage.length; i++) {
15-
const key = localStorage.key(i);
16-
if (
17-
key &&
18-
(key.includes("chatsvcagg") || key.includes("ic3.teams.office"))
19-
) {
20-
items[key] = localStorage.getItem(key) || "";
21-
}
22-
}
23-
return items;
24-
},
25-
});
26-
27-
if (result && result[0].result) {
28-
for (const [key, value] of Object.entries(result[0].result)) {
29-
try {
30-
const parsedValue = JSON.parse(value as string);
31-
await chrome.storage.local.set({ [key]: parsedValue });
32-
console.log("Stored token:", key);
33-
} catch (e) {
34-
console.error("Failed to parse token:", key, e);
35-
}
36-
}
37-
console.log("Teams tokens captured from page");
38-
}
39-
}
40-
} catch (err) {
41-
console.error("Error capturing tokens:", err);
42-
}
43-
});
44-
454
function updateStatus(
465
message: string,
476
type: "ready" | "success" | "error" | "processing" = "ready",
7+
persist: boolean = true,
488
) {
499
const statusDiv = document.getElementById("status")!;
5010
const statusText = document.getElementById("statusText")!;
@@ -61,6 +21,20 @@ function updateStatus(
6121
statusDiv.classList.add(statusClasses[type][0]);
6222
statusIcon.className = statusClasses[type][1];
6323
statusText.textContent = message;
24+
25+
if (persist) {
26+
localStorage.setItem("uploadStatus", JSON.stringify({ message, type }));
27+
}
28+
}
29+
30+
function restoreStatus() {
31+
const status = localStorage.getItem("uploadStatus");
32+
if (status) {
33+
const { message, type } = JSON.parse(status);
34+
updateStatus(message, type, false);
35+
} else {
36+
updateStatus("Ready to process", "ready", false);
37+
}
6438
}
6539

6640
async function handleFileProcessing() {
@@ -94,45 +68,11 @@ async function handleFileProcessing() {
9468

9569
const tokens = await TokenStore.collectTokensFromStorage();
9670

97-
const result = await chrome.runtime.sendMessage({
71+
chrome.runtime.sendMessage({
9872
action: "processFiles",
9973
files: filesJson,
10074
tokens: tokens,
10175
});
102-
103-
const { success, error, status } = result as ProcessResult;
104-
if (error) {
105-
updateStatus(error, "error");
106-
} else {
107-
updateStatus(status || "", success ? "success" : "processing");
108-
109-
await chrome.browsingData.remove(
110-
{
111-
origins: ["https://teams.microsoft.com"],
112-
},
113-
{
114-
cacheStorage: true,
115-
cookies: true,
116-
localStorage: true,
117-
serviceWorkers: true,
118-
indexedDB: true,
119-
cache: true,
120-
appcache: true,
121-
},
122-
);
123-
124-
await chrome.storage.local.clear();
125-
126-
const tabs = await chrome.tabs.query({
127-
active: true,
128-
currentWindow: true,
129-
});
130-
const tab = tabs[0];
131-
132-
if (tab.id) {
133-
await chrome.tabs.reload(tab.id);
134-
}
135-
}
13676
} catch (error) {
13777
let errorMessage = "";
13878

@@ -166,12 +106,16 @@ function updateSelectedFiles() {
166106
}
167107
}
168108

169-
document
170-
.getElementById("fileInput")
171-
?.addEventListener("change", updateSelectedFiles);
172-
document
173-
.getElementById("processButton")
174-
?.addEventListener("click", () => handleFileProcessing());
109+
document.addEventListener("DOMContentLoaded", () => {
110+
restoreStatus();
111+
112+
document
113+
.getElementById("fileInput")
114+
?.addEventListener("change", updateSelectedFiles);
115+
document
116+
.getElementById("processButton")
117+
?.addEventListener("click", () => handleFileProcessing());
118+
});
175119

176120
chrome.runtime.onMessage.addListener(
177121
(message: { type: string; error: any; status: any; success: any }) => {
@@ -181,5 +125,8 @@ chrome.runtime.onMessage.addListener(
181125
message.success ? "success" : "error",
182126
);
183127
}
128+
if (message.type === "processComplete") {
129+
localStorage.removeItem("uploadStatus");
130+
}
184131
},
185132
);

0 commit comments

Comments
 (0)