diff --git a/src/background/background.ts b/src/background/background.ts index 78e1bec..7e09f05 100644 --- a/src/background/background.ts +++ b/src/background/background.ts @@ -1,4 +1,6 @@ -import "./controllers/camera_bubble_controller"; -import "./controllers/current_window_tab_controller"; -import "./controllers/recording_controller"; -import "./controllers/storage_controller"; +import "./services/active_tab_setter/active_tab_setter"; +import "./services/active_window_setter/active_window_setter"; +import "./services/camera_bubble_injector/camera_bubble_injector"; +import "./services/permissions_giver/permissions_giver"; +import "./services/recording_controller/recording_controller"; +import "./services/storage_controller/storage_controller"; diff --git a/src/background/controllers/current_window_tab_controller.ts b/src/background/controllers/current_window_tab_controller.ts deleted file mode 100644 index 2511fab..0000000 --- a/src/background/controllers/current_window_tab_controller.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { storage } from "@/app/storage"; - -chrome.tabs.onActivated.addListener((activatedTabInfo) => { - console.log("Handle 'chrome.tabs.onActivated'"); - (async () => { - await storage.current.tabId.set(activatedTabInfo.tabId); - })().catch((err) => { - console.error( - `Error in 'chrome.tabs.onActivated' handler: ${(err as Error).toString()}`, - ); - }); -}); - -chrome.windows.onFocusChanged.addListener((windowId) => { - console.log("Handle 'chrome.windows.onFocusChanged'"); - (async () => { - await storage.current.windowId.set(windowId); - })().catch((err) => { - console.error( - `Error in 'chrome.windows.onFocusChanged' handler: ${(err as Error).toString()}`, - ); - }); -}); diff --git a/src/background/offscreen.ts b/src/background/offscreen.ts index dac84b0..76b0b95 100644 --- a/src/background/offscreen.ts +++ b/src/background/offscreen.ts @@ -1 +1 @@ -import "./controllers/recorder_controller"; +import "./services/recorder_controller/recorder_controller"; diff --git a/src/background/services/active_tab_setter/active_tab_setter.ts b/src/background/services/active_tab_setter/active_tab_setter.ts new file mode 100644 index 0000000..7d68374 --- /dev/null +++ b/src/background/services/active_tab_setter/active_tab_setter.ts @@ -0,0 +1,12 @@ +import { storage } from "@/shared/storage"; + +chrome.tabs.onActivated.addListener((activatedTabInfo) => { + console.log("Handle 'chrome.tabs.onActivated'"); + (async () => { + await storage.current.tabId.set(activatedTabInfo.tabId); + })().catch((err) => { + console.error( + `Error in 'chrome.tabs.onActivated' handler: ${(err as Error).toString()}`, + ); + }); +}); diff --git a/src/background/services/active_window_setter/active_window_setter.ts b/src/background/services/active_window_setter/active_window_setter.ts new file mode 100644 index 0000000..e574b22 --- /dev/null +++ b/src/background/services/active_window_setter/active_window_setter.ts @@ -0,0 +1,12 @@ +import { storage } from "@/shared/storage"; + +chrome.windows.onFocusChanged.addListener((windowId) => { + console.log("Handle 'chrome.windows.onFocusChanged'"); + (async () => { + await storage.current.windowId.set(windowId); + })().catch((err) => { + console.error( + `Error in 'chrome.windows.onFocusChanged' handler: ${(err as Error).toString()}`, + ); + }); +}); diff --git a/src/background/controllers/camera_bubble_controller.ts b/src/background/services/camera_bubble_injector/camera_bubble_injector.ts similarity index 82% rename from src/background/controllers/camera_bubble_controller.ts rename to src/background/services/camera_bubble_injector/camera_bubble_injector.ts index bd5e124..98b2da8 100644 --- a/src/background/controllers/camera_bubble_controller.ts +++ b/src/background/services/camera_bubble_injector/camera_bubble_injector.ts @@ -3,14 +3,14 @@ import { MessageResponse, MessageResponseType, MessageType, -} from "@/app/messaging"; -import { storage } from "@/app/storage"; -import { Injection, InjectionElementId } from "@/app/injections"; -import { Injector } from "@/background/injector"; +} from "@/shared/messaging"; +import { storage } from "@/shared/storage"; +import { Injection, InjectionElementId } from "@/shared/injections"; +import { Injector } from "./injector"; -class CameraBubbleController { +class CameraBubbleInjector { static async show() { - console.log("CameraBubbleController.show()"); + console.log("CameraBubbleInjector.show()"); const [currentTab] = await chrome.tabs.query({ active: true, lastFocusedWindow: true, @@ -21,7 +21,7 @@ class CameraBubbleController { } static async hide() { - console.log("CameraBubbleController.hide()"); + console.log("CameraBubbleInjector.hide()"); await Injector.deinject( await storage.ui.cameraBubble.tabId.get(), InjectionElementId.CameraBubble, @@ -37,8 +37,8 @@ chrome.tabs.onActivated.addListener((_activeTabInfo) => { if (!(await storage.ui.cameraBubble.enabled.get())) { return; } - await CameraBubbleController.hide(); - await CameraBubbleController.show(); + await CameraBubbleInjector.hide(); + await CameraBubbleInjector.show(); })().catch((err) => { console.error( `Error in 'chrome.tabs.onActivated' handler: ${(err as Error).toString()}`, @@ -52,7 +52,7 @@ chrome.tabs.onUpdated.addListener((_tabId, _changeInfo, _tab) => { if (!(await storage.ui.cameraBubble.enabled.get())) { return; } - await CameraBubbleController.show(); + await CameraBubbleInjector.show(); })().catch((err) => { console.error( `Error in 'chrome.tabs.onUpdated' handler: ${(err as Error).toString()}`, @@ -66,7 +66,7 @@ chrome.tabs.onRemoved.addListener((_closedTabId, _removeInfo) => { if (!(await storage.ui.cameraBubble.enabled.get())) { return; } - await CameraBubbleController.show(); + await CameraBubbleInjector.show(); })().catch((err) => { console.error( `Error in 'chrome.tabs.onRemoved' handler: ${(err as Error).toString()}`, @@ -80,8 +80,8 @@ chrome.windows.onFocusChanged.addListener((_windowId) => { if (!(await storage.ui.cameraBubble.enabled.get())) { return; } - await CameraBubbleController.hide(); - await CameraBubbleController.show(); + await CameraBubbleInjector.hide(); + await CameraBubbleInjector.show(); })().catch((err) => { console.error( `Error in 'chrome.windows.onFocusChanged' handler: ${(err as Error).toString()}`, @@ -102,10 +102,10 @@ chrome.runtime.onMessage.addListener( } switch (type) { case MessageType.CameraBubbleShow: - await CameraBubbleController.show(); + await CameraBubbleInjector.show(); break; case MessageType.CameraBubbleHide: - await CameraBubbleController.hide(); + await CameraBubbleInjector.hide(); break; } })() diff --git a/src/background/injector.ts b/src/background/services/camera_bubble_injector/injector.ts similarity index 89% rename from src/background/injector.ts rename to src/background/services/camera_bubble_injector/injector.ts index 26aa7b5..6b36e7c 100644 --- a/src/background/injector.ts +++ b/src/background/services/camera_bubble_injector/injector.ts @@ -1,4 +1,4 @@ -import type { Injection, InjectionElementId } from "@/app/injections"; +import type { Injection, InjectionElementId } from "@/shared/injections"; export class Injector { static inject(tabId: number, injection: Injection) { diff --git a/src/background/services/permissions_giver/permissions_giver.ts b/src/background/services/permissions_giver/permissions_giver.ts new file mode 100644 index 0000000..417aa9d --- /dev/null +++ b/src/background/services/permissions_giver/permissions_giver.ts @@ -0,0 +1,13 @@ +chrome.runtime.onInstalled.addListener((_details) => { + console.log("Handle 'chrome.runtime.onInstalled'"); + (async () => { + await chrome.tabs.create({ + active: true, + url: chrome.runtime.getURL("permissions.html"), + }); + })().catch((err) => { + console.error( + `Error in 'chrome.runtime.onInstalled' handler: ${(err as Error).toString()}`, + ); + }); +}); diff --git a/src/background/controllers/recorder_controller.ts b/src/background/services/recorder_controller/recorder_controller.ts similarity index 97% rename from src/background/controllers/recorder_controller.ts rename to src/background/services/recorder_controller/recorder_controller.ts index e4a4bbe..85f1643 100644 --- a/src/background/controllers/recorder_controller.ts +++ b/src/background/services/recorder_controller/recorder_controller.ts @@ -5,7 +5,7 @@ import { MessageType, RecorderCreateOptions, sender, -} from "@/app/messaging"; +} from "@/shared/messaging"; class Recorder { #mimeType: string; @@ -77,7 +77,7 @@ class Recorder { }); for (const track of this.#mediaStream.getTracks()) { - track.addEventListener("ended", this.#onTrackEnded); + track.addEventListener("ended", this.#onTrackEnded.bind(this)); } this.#mediaRecorder.addEventListener( @@ -94,7 +94,6 @@ class Recorder { } this.#mediaRecorder.stop(); for (const track of this.#mediaStream.getTracks()) { - track.removeEventListener("ended", this.#onTrackEnded); track.stop(); } } diff --git a/src/background/controllers/recording_controller.ts b/src/background/services/recording_controller/recording_controller.ts similarity index 97% rename from src/background/controllers/recording_controller.ts rename to src/background/services/recording_controller/recording_controller.ts index 0bfa4d0..1542896 100644 --- a/src/background/controllers/recording_controller.ts +++ b/src/background/services/recording_controller/recording_controller.ts @@ -5,8 +5,8 @@ import { MessageType, RecordingSaveOptions, sender, -} from "@/app/messaging"; -import { RecordingState, storage } from "@/app/storage"; +} from "@/shared/messaging"; +import { RecordingState, storage } from "@/shared/storage"; class RecordingController { static async start() { @@ -99,6 +99,7 @@ class RecordingController { return; } await sender.offscreen.recorderDelete(); + await chrome.offscreen.closeDocument(); await storage.recording.state.set(RecordingState.NotStarted); await storage.recording.downloadId.set(0); } diff --git a/src/background/controllers/storage_controller.ts b/src/background/services/storage_controller/storage_controller.ts similarity index 86% rename from src/background/controllers/storage_controller.ts rename to src/background/services/storage_controller/storage_controller.ts index 44563ca..7e8badc 100644 --- a/src/background/controllers/storage_controller.ts +++ b/src/background/services/storage_controller/storage_controller.ts @@ -1,5 +1,5 @@ -import { config } from "@/app/config"; -import { RecordingState, storage } from "@/app/storage"; +import { config } from "@/shared/config"; +import { RecordingState, storage } from "@/shared/storage"; chrome.runtime.onInstalled.addListener((_details) => { console.log("Handle 'chrome.runtime.onInstalled'"); @@ -23,11 +23,6 @@ chrome.runtime.onInstalled.addListener((_details) => { console.log( `Storage: ${JSON.stringify(await storage.getEntireStorage(), undefined, 2)}`, ); - - await chrome.tabs.create({ - active: true, - url: chrome.runtime.getURL("permissions.html"), - }); })().catch((err) => { console.error( `Error in 'chrome.runtime.onInstalled' handler: ${(err as Error).toString()}`, diff --git a/src/app/config.ts b/src/shared/config.ts similarity index 100% rename from src/app/config.ts rename to src/shared/config.ts diff --git a/src/app/injections.ts b/src/shared/injections.ts similarity index 100% rename from src/app/injections.ts rename to src/shared/injections.ts diff --git a/src/app/messaging.ts b/src/shared/messaging.ts similarity index 100% rename from src/app/messaging.ts rename to src/shared/messaging.ts diff --git a/src/app/storage.ts b/src/shared/storage.ts similarity index 100% rename from src/app/storage.ts rename to src/shared/storage.ts diff --git a/src/ui/hooks/useStorageValue.tsx b/src/ui/hooks/useStorageValue.tsx index 4fcb739..60c3576 100644 --- a/src/ui/hooks/useStorageValue.tsx +++ b/src/ui/hooks/useStorageValue.tsx @@ -4,7 +4,7 @@ import { createStorageSetter, StorageKey, StorageValueType, -} from "@/app/storage"; +} from "@/shared/storage"; export default function useStorageValue( storageKey: Key, diff --git a/src/ui/pages/camera_bubble/CameraBubble.tsx b/src/ui/pages/camera_bubble/CameraBubble.tsx index 8b2bfac..f4534c3 100644 --- a/src/ui/pages/camera_bubble/CameraBubble.tsx +++ b/src/ui/pages/camera_bubble/CameraBubble.tsx @@ -1,4 +1,4 @@ -import { StorageKey } from "@/app/storage"; +import { StorageKey } from "@/shared/storage"; import useStorageValue from "@/ui/hooks/useStorageValue"; import { Draggable } from "./components/Draggable"; diff --git a/src/ui/pages/camera_bubble/camera_bubble.ts b/src/ui/pages/camera_bubble/camera_bubble.ts index f4ed57e..5889483 100644 --- a/src/ui/pages/camera_bubble/camera_bubble.ts +++ b/src/ui/pages/camera_bubble/camera_bubble.ts @@ -1,7 +1,7 @@ import React from "react"; import ReactDOM from "react-dom/client"; -import { Injection, InjectionElementId } from "@/app/injections"; -import { storage } from "@/app/storage"; +import { Injection, InjectionElementId } from "@/shared/injections"; +import { storage } from "@/shared/storage"; import { CameraBubble } from "./CameraBubble"; async function inject() { diff --git a/src/ui/pages/popup/Popup.tsx b/src/ui/pages/popup/Popup.tsx index c55f4a6..c4b178b 100644 --- a/src/ui/pages/popup/Popup.tsx +++ b/src/ui/pages/popup/Popup.tsx @@ -1,5 +1,5 @@ -import { RecordingState, StorageKey } from "@/app/storage"; -import { sender } from "@/app/messaging"; +import { RecordingState, StorageKey } from "@/shared/storage"; +import { sender } from "@/shared/messaging"; import useStorageValue from "@/ui/hooks/useStorageValue"; import { PauseButton } from "./components/PauseButton"; import { PlayButton } from "./components/PlayButton";