Skip to content

Commit 3e7964b

Browse files
committed
status card and status update on tab change
1 parent 22a4373 commit 3e7964b

5 files changed

Lines changed: 203 additions & 116 deletions

File tree

src/ext/bg.ts

Lines changed: 39 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ const collLoader = new CollectionLoader();
2626

2727
const disabledCSPTabs = new Set();
2828

29+
// @ts-expect-error - TS7034 - Variable 'sidepanelPort' implicitly has type 'any' in some locations where its type cannot be determined.
30+
let sidepanelPort = null;
31+
2932
// ===========================================================================
3033

3134
function main() {
@@ -54,72 +57,13 @@ chrome.sidePanel
5457
// @ts-expect-error - TS7006 - Parameter 'port' implicitly has an 'any' type.
5558
chrome.runtime.onConnect.addListener((port) => {
5659
switch (port.name) {
57-
case "popup-port":
58-
popupHandler(port);
59-
break;
6060
case "sidepanel-port":
6161
sidepanelHandler(port);
6262
break;
6363
}
6464
});
6565

66-
// @ts-expect-error - TS7006 - Parameter 'port' implicitly has an 'any' type.
67-
function popupHandler(port) {
68-
if (!port.sender || port.sender.url !== chrome.runtime.getURL("popup.html")) {
69-
return;
70-
}
71-
72-
// @ts-expect-error - TS7034 - Variable 'tabId' implicitly has type 'any' in some locations where its type cannot be determined.
73-
let tabId = null;
74-
75-
// @ts-expect-error - TS7006 - Parameter 'message' implicitly has an 'any' type.
76-
port.onMessage.addListener(async (message) => {
77-
switch (message.type) {
78-
case "startUpdates":
79-
tabId = message.tabId;
80-
if (self.recorders[tabId]) {
81-
// @ts-expect-error - TS2339 - Property 'port' does not exist on type 'BrowserRecorder'.
82-
self.recorders[tabId].port = port;
83-
self.recorders[tabId].doUpdateStatus();
84-
}
85-
port.postMessage(await listAllMsg(collLoader));
86-
break;
87-
88-
case "startRecording": {
89-
const { collId, autorun } = message;
90-
// @ts-expect-error - TS2554 - Expected 2 arguments, but got 3.
91-
startRecorder(tabId, { collId, port, autorun }, message.url);
92-
break;
93-
}
94-
95-
case "stopRecording":
96-
// @ts-expect-error - TS7005 - Variable 'tabId' implicitly has an 'any' type.
97-
stopRecorder(tabId);
98-
break;
99-
100-
case "toggleBehaviors":
101-
// @ts-expect-error - TS7005 - Variable 'tabId' implicitly has an 'any' type.
102-
toggleBehaviors(tabId);
103-
break;
104-
105-
case "newColl": {
106-
const { name } = await collLoader.initNewColl({ title: message.title });
107-
defaultCollId = name;
108-
port.postMessage(await listAllMsg(collLoader, { defaultCollId }));
109-
await setLocalOption("defaultCollId", defaultCollId);
110-
break;
111-
}
112-
}
113-
});
11466

115-
port.onDisconnect.addListener(() => {
116-
// @ts-expect-error - TS2538 - Type 'null' cannot be used as an index type.
117-
if (self.recorders[tabId]) {
118-
// @ts-expect-error - TS2538 - Type 'null' cannot be used as an index type.
119-
self.recorders[tabId].port = null;
120-
}
121-
});
122-
}
12367
// @ts-expect-error - TS7006 - Parameter 'port' implicitly has an 'any' type.
12468
function sidepanelHandler(port) {
12569
if (!port.sender || port.sender.url !== chrome.runtime.getURL("sidepanel.html")) {
@@ -134,6 +78,7 @@ function sidepanelHandler(port) {
13478
switch (message.type) {
13579
case "startUpdates":
13680
tabId = message.tabId;
81+
sidepanelPort = port;
13782
if (self.recorders[tabId]) {
13883
// @ts-expect-error - TS2339 - Property 'port' does not exist on type 'BrowserRecorder'.
13984
self.recorders[tabId].port = port;
@@ -226,8 +171,32 @@ function sidepanelHandler(port) {
226171
self.recorders[tabId].port = null;
227172
}
228173
});
174+
175+
229176
}
177+
// ===========================================================================
178+
chrome.runtime.onMessage.addListener(
179+
// @ts-expect-error - TS7006 - Parameter 'message' implicitly has an 'any' type.
180+
(message /*sender, sendResponse*/) => {
181+
console.log("onMessage", message);
182+
switch (message.msg) {
183+
case "startNew":
184+
(async () => {
185+
newRecUrl = message.url;
186+
newRecCollId = message.collId;
187+
autorun = message.autorun;
188+
defaultCollId = await getLocalOption("defaultCollId");
189+
chrome.tabs.create({ url: "about:blank" });
190+
})();
191+
break;
230192

193+
case "disableCSP":
194+
disableCSPForTab(message.tabId);
195+
break;
196+
}
197+
return true;
198+
},
199+
);
231200
// ===========================================================================
232201
// @ts-expect-error - TS7006 - Parameter 'tab' implicitly has an 'any' type. | TS7006 - Parameter 'reason' implicitly has an 'any' type.
233202
chrome.debugger.onDetach.addListener((tab, reason) => {
@@ -239,16 +208,20 @@ chrome.debugger.onDetach.addListener((tab, reason) => {
239208

240209
// @ts-expect-error - TS7006 - Parameter 'tab' implicitly has an 'any' type.
241210
chrome.tabs.onActivated.addListener(async ({ tabId }) => {
211+
212+
// @ts-expect-error - TS7034 - Variable 'err' implicitly has type 'any' in some locations where its type cannot be determined.
213+
if (sidepanelPort) {
214+
sidepanelPort.postMessage({ type: "update" });
215+
}
242216
if (!isRecordingEnabled) return;
243217

244218
// @ts-expect-error - chrome doesn't have type definitions
245219
const tab = await new Promise<chrome.tabs.Tab>((resolve) => chrome.tabs.get(tabId, resolve));
246220

247221
if (!isValidUrl(tab.url)) return;
248-
249222
if (!self.recorders[tabId]) {
250223
// @ts-expect-error - TS2554 - Expected 2 arguments, but got 3.
251-
startRecorder(tabId, { collId: defaultCollId, port: null, autorun }, tab.url);
224+
await startRecorder(tabId, { collId: defaultCollId, port: null, autorun }, tab.url);
252225
}
253226
});
254227

@@ -384,9 +357,13 @@ async function startRecorder(tabId, opts) {
384357
}
385358

386359
let err = null;
387-
360+
// @ts-expect-error - TS7034 - Variable 'err' implicitly has type 'any' in some locations where its type cannot be determined.
361+
if (sidepanelPort) {
362+
sidepanelPort.postMessage({ type: "update" });
363+
}
388364
const { waitForTabUpdate } = opts;
389365

366+
390367
// @ts-expect-error - TS2339 - Property 'running' does not exist on type 'BrowserRecorder'.
391368
if (!waitForTabUpdate && !self.recorders[tabId].running) {
392369
try {
@@ -435,25 +412,7 @@ function isValidUrl(url) {
435412
return url && (url === "about:blank" || url.startsWith("https:") || url.startsWith("http:"));
436413
}
437414

438-
// ===========================================================================
439-
chrome.runtime.onMessage.addListener(
440-
// @ts-expect-error - TS7006 - Parameter 'message' implicitly has an 'any' type.
441-
async (message /*sender, sendResponse*/) => {
442-
switch (message.msg) {
443-
case "startNew":
444-
newRecUrl = message.url;
445-
newRecCollId = message.collId;
446-
autorun = message.autorun;
447-
defaultCollId = await getLocalOption("defaultCollId");
448-
chrome.tabs.create({ url: "about:blank" });
449-
break;
450415

451-
case "disableCSP":
452-
disableCSPForTab(message.tabId);
453-
break;
454-
}
455-
},
456-
);
457416

458417
// ===========================================================================
459418
// @ts-expect-error - TS7006 - Parameter 'tabId' implicitly has an 'any' type.

src/recorder.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,10 @@ class Recorder {
424424
// @ts-expect-error - TS2339 - Property 'numPending' does not exist on type 'Recorder'.
425425
numPending: this.numPending,
426426
// @ts-expect-error - TS2339 - Property 'pageInfo' does not exist on type 'Recorder'.
427+
favIconUrl: this.pageInfo.favIconUrl,
428+
// @ts-expect-error - TS2339 - Property 'pageInfo' does not exist on type 'Recorder'.
429+
pageTitle: this.pageInfo.title,
430+
// @ts-expect-error - TS2339 - Property 'pageInfo' does not exist on type 'Recorder'.
427431
pageUrl: this.pageInfo.url,
428432
// @ts-expect-error - TS2339 - Property 'pageInfo' does not exist on type 'Recorder'.
429433
pageTs: this.pageInfo.ts,
@@ -433,6 +437,8 @@ class Recorder {
433437
collId: this.collId,
434438
// @ts-expect-error - TS2339 - Property 'stopping' does not exist on type 'Recorder'.
435439
stopping: this.stopping,
440+
// @ts-expect-error - TS2339 - Property 'tabId' does not exist on type 'Recorder'.
441+
tabId: this.tabId,
436442
type: "status",
437443
};
438444
}

0 commit comments

Comments
 (0)