-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogress.js
More file actions
154 lines (136 loc) · 4.47 KB
/
Copy pathprogress.js
File metadata and controls
154 lines (136 loc) · 4.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const statusEl = document.getElementById("status");
const barWrap = document.getElementById("barWrap");
const barFill = document.getElementById("barFill");
const detailEl = document.getElementById("detail");
const closeBtn = document.getElementById("closeBtn");
/** @type {string | null} */
let myRunId = null;
let replacedNoticeShown = false;
/**
* @param {{
* phase?: string;
* runId?: string;
* windowsDone?: number;
* totalWindows?: number;
* tabsDone?: number;
* totalTabs?: number;
* currentGroup?: string;
* label?: string;
* } | undefined} p
*/
function renderProgress(p) {
if (!p) return;
if (p.runId != null && myRunId != null && p.runId !== myRunId) {
if (!replacedNoticeShown) {
replacedNoticeShown = true;
statusEl.textContent =
"This run was replaced by a newer Apply from the extension. You can close this window.";
detailEl.textContent = "";
barFill.style.width = "0%";
closeBtn.hidden = false;
}
return;
}
const total = p.totalTabs ?? 0;
const done = p.tabsDone ?? 0;
const pct = total > 0 ? Math.min(100, Math.round((100 * done) / total)) : 0;
barFill.style.width = `${pct}%`;
barWrap.setAttribute("aria-valuenow", String(pct));
statusEl.textContent = p.label || `${done} / ${total} tabs`;
detailEl.textContent = p.currentGroup ? `Last: ${p.currentGroup}` : "";
if (p.phase === "error") {
statusEl.textContent = p.label || "Apply could not run.";
detailEl.textContent = "";
barFill.style.width = "0%";
closeBtn.hidden = false;
closeBtn.focus();
return;
}
if (p.phase === "superseded") {
statusEl.textContent = p.label || "This run was stopped.";
closeBtn.hidden = false;
closeBtn.focus();
return;
}
if (p.phase === "done") {
statusEl.textContent = `Done — ${p.tabsDone ?? 0} tab(s) in ${p.totalWindows ?? 0} window(s).`;
closeBtn.hidden = false;
closeBtn.focus();
}
}
document.addEventListener("DOMContentLoaded", async () => {
closeBtn.addEventListener("click", async () => {
await chrome.storage.session.remove(["organizeJob", "organizeProgress", "organizeActiveRunId"]);
window.close();
});
const { organizeJob } = await chrome.storage.session.get("organizeJob");
if (!organizeJob?.groups?.length) {
statusEl.textContent =
"No organize job found. Close this window and run Preview → Apply from the extension popup.";
closeBtn.hidden = false;
return;
}
myRunId = organizeJob.runId ?? null;
if (!myRunId) {
statusEl.textContent = "Outdated job format. Run Preview → Apply again from the extension.";
closeBtn.hidden = false;
return;
}
chrome.storage.onChanged.addListener((changes, area) => {
if (area !== "session" || !changes.organizeProgress) return;
renderProgress(changes.organizeProgress.newValue);
});
const existing = await chrome.storage.session.get("organizeProgress");
if (existing.organizeProgress) renderProgress(existing.organizeProgress);
renderProgress({
phase: "apply",
runId: myRunId,
tabsDone: 0,
totalTabs: organizeJob.groups.reduce(
(n, g) => n + g.tabIds.filter((id) => id != null).length,
0
),
label: "Connecting…",
});
let res;
try {
res = await chrome.runtime.sendMessage({
type: "ORGANIZE_APPLY",
groups: organizeJob.groups,
runId: myRunId,
});
} catch (e) {
statusEl.textContent = `Error: ${e?.message || e}`;
detailEl.textContent = "";
closeBtn.hidden = false;
return;
}
if (!res?.ok) {
statusEl.textContent = `Failed: ${res?.error || "unknown error"}`;
closeBtn.hidden = false;
return;
}
if (res.runId !== myRunId) {
statusEl.textContent = "Response was for a different run. Close this window.";
closeBtn.hidden = false;
return;
}
if (res.superseded) {
const final = await chrome.storage.session.get("organizeProgress");
if (final.organizeProgress?.runId === myRunId) {
renderProgress(final.organizeProgress);
} else {
statusEl.textContent = "This run was replaced by a newer Apply. Close this window.";
closeBtn.hidden = false;
}
return;
}
const { organizeActiveRunId } = await chrome.storage.session.get("organizeActiveRunId");
if (organizeActiveRunId === myRunId) {
await chrome.storage.session.remove("organizeJob");
}
const final = await chrome.storage.session.get("organizeProgress");
if (final.organizeProgress?.runId === myRunId) {
renderProgress(final.organizeProgress);
}
});