Skip to content

Commit 7ad3e34

Browse files
Merge pull request #19 from Monadical-SAS/nikita-sidepanel-archiving-sync
feat: start archiving new pages as the user starts to visit them with archiving enabled
2 parents 2477863 + 6db5c77 commit 7ad3e34

File tree

2 files changed

+87
-39
lines changed

2 files changed

+87
-39
lines changed

src/ext/bg.ts

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ let newRecCollId = null;
1818
// @ts-expect-error - TS7034 - Variable 'defaultCollId' implicitly has type 'any' in some locations where its type cannot be determined.
1919
let defaultCollId = null;
2020
let autorun = false;
21+
let isRecordingEnabled = false;
2122

2223
const openWinMap = new Map();
2324

@@ -159,16 +160,49 @@ function sidepanelHandler(port) {
159160
}
160161

161162
case "startRecording": {
162-
const { collId, autorun } = message;
163-
// @ts-expect-error - TS2554 - Expected 2 arguments, but got 3.
164-
startRecorder(tabId, { collId, port, autorun }, message.url);
163+
isRecordingEnabled = true;
164+
defaultCollId = message.collId;
165+
autorun = message.autorun;
166+
167+
// @ts-expect-error - tabs doesn't have type definitions
168+
chrome.tabs.query({ active: true, currentWindow: true }, async (tabs) => {
169+
for (const tab of tabs) {
170+
if (!isValidUrl(tab.url)) continue;
171+
172+
// @ts-expect-error - TS2554 - Expected 2 arguments, but got 3.
173+
await startRecorder(tab.id, { collId: defaultCollId, port: null, autorun }, tab.url);
174+
}
175+
176+
port.postMessage({
177+
type: "status",
178+
recording: true,
179+
autorun,
180+
// @ts-expect-error - defaultCollId implicitly has an 'any' type.
181+
collId: defaultCollId,
182+
});
183+
});
184+
165185
break;
166186
}
167187

168-
case "stopRecording":
169-
// @ts-expect-error - TS7005 - Variable 'tabId' implicitly has an 'any' type.
170-
stopRecorder(tabId);
188+
case "stopRecording": {
189+
isRecordingEnabled = false;
190+
191+
for (const [tabIdStr, rec] of Object.entries(self.recorders)) {
192+
const tabId = parseInt(tabIdStr);
193+
stopRecorder(tabId);
194+
}
195+
196+
port.postMessage({
197+
type: "status",
198+
recording: false,
199+
autorun,
200+
// @ts-expect-error - defaultCollId implicitly has an 'any' type.
201+
collId: defaultCollId,
202+
});
203+
171204
break;
205+
}
172206

173207
case "toggleBehaviors":
174208
// @ts-expect-error - TS7005 - Variable 'tabId' implicitly has an 'any' type.
@@ -203,6 +237,21 @@ chrome.debugger.onDetach.addListener((tab, reason) => {
203237
}
204238
});
205239

240+
// @ts-expect-error - TS7006 - Parameter 'tab' implicitly has an 'any' type.
241+
chrome.tabs.onActivated.addListener(async ({ tabId }) => {
242+
if (!isRecordingEnabled) return;
243+
244+
// @ts-expect-error - chrome doesn't have type definitions
245+
const tab = await new Promise<chrome.tabs.Tab>((resolve) => chrome.tabs.get(tabId, resolve));
246+
247+
if (!isValidUrl(tab.url)) return;
248+
249+
if (!self.recorders[tabId]) {
250+
// @ts-expect-error - TS2554 - Expected 2 arguments, but got 3.
251+
startRecorder(tabId, { collId: defaultCollId, port: null, autorun }, tab.url);
252+
}
253+
});
254+
206255
// ===========================================================================
207256
// @ts-expect-error - TS7006 - Parameter 'tab' implicitly has an 'any' type.
208257
chrome.tabs.onCreated.addListener((tab) => {
@@ -278,14 +327,20 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo) => {
278327
return;
279328
}
280329
}
281-
} else if (changeInfo.url && openWinMap.has(changeInfo.url)) {
282-
const collId = openWinMap.get(changeInfo.url);
283-
openWinMap.delete(changeInfo.url);
284-
if (!tabId || !isValidUrl(changeInfo.url)) {
330+
} else if (changeInfo.url) {
331+
if (isRecordingEnabled && isValidUrl(changeInfo.url) && !self.recorders[tabId]) {
332+
// @ts-expect-error - TS2554 - Expected 2 arguments, but got 3.
333+
startRecorder(tabId, { collId: defaultCollId, autorun }, changeInfo.url);
285334
return;
286335
}
287-
// @ts-expect-error - TS2554 - Expected 2 arguments, but got 3.
288-
startRecorder(tabId, { collId, autorun }, changeInfo.url);
336+
if (openWinMap.has(changeInfo.url)) {
337+
const collId = openWinMap.get(changeInfo.url);
338+
openWinMap.delete(changeInfo.url);
339+
if (!tabId || !isValidUrl(changeInfo.url)) return;
340+
341+
// @ts-expect-error - TS2554 - Expected 2 arguments, but got 3.
342+
startRecorder(tabId, { collId, autorun }, changeInfo.url);
343+
}
289344
}
290345
});
291346

src/sidepanel.ts

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -301,39 +301,32 @@ class ArgoViewer extends LitElement {
301301
<md-divider></md-divider>
302302
<div style="padding:1rem; display:flex; align-items:center; justify-content:space-between;">
303303
${
304-
// @ts-expect-error - TS2339 - Property 'canRecord' does not exist on type 'RecPopup'.
305-
this.canRecord
304+
// @ts-expect-error - TS2339 - Property 'recording' does not exist on type 'RecPopup'.
305+
!this.recording
306306
? html`
307-
${
308-
// @ts-expect-error - TS2339 - Property 'recording' does not exist on type 'RecPopup'.
309-
!this.recording
310-
? html`
311-
<md-filled-button
312-
style="
307+
<md-filled-button
308+
style="
313309
--md-sys-color-primary-container: #7b1fa2;
314310
color: white;
315311
border-radius: 9999px;
316312
"
317-
?disabled=${this.actionButtonDisabled}
318-
@click=${this.onStart}
319-
>
320-
<md-icon slot="icon" style="color:white">public</md-icon>
321-
Resume Archiving
322-
</md-filled-button>
323-
`
324-
: html`
325-
<md-outlined-button
326-
style="--md-sys-color-primary: #b00020; --md-sys-color-outline: #b00020; border-radius: 9999px;"
327-
?disabled=${this.actionButtonDisabled}
328-
@click=${this.onStop}
329-
>
330-
<md-icon slot="icon" style="color:#b00020">pause</md-icon>
331-
Pause Archiving
332-
</md-outlined-button>
333-
`
334-
}
313+
?disabled=${this.actionButtonDisabled}
314+
@click=${this.onStart}
315+
>
316+
<md-icon slot="icon" style="color:white">public</md-icon>
317+
Resume Archiving
318+
</md-filled-button>
319+
`
320+
: html`
321+
<md-outlined-button
322+
style="--md-sys-color-primary: #b00020; --md-sys-color-outline: #b00020; border-radius: 9999px;"
323+
?disabled=${this.actionButtonDisabled}
324+
@click=${this.onStop}
325+
>
326+
<md-icon slot="icon" style="color:#b00020">pause</md-icon>
327+
Pause Archiving
328+
</md-outlined-button>
335329
`
336-
: html`<span></span>`
337330
}
338331
339332
<md-icon-button aria-label="Settings">

0 commit comments

Comments
 (0)