-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
178 lines (154 loc) · 5.78 KB
/
script.js
File metadata and controls
178 lines (154 loc) · 5.78 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* Cache DOM elements for easy access.
*/
const recordTabButton = document.getElementById("tab");
const recordScreenButton = document.getElementById("screen");
const faceToggleSwitch = document.getElementById("faceToggle");
const bodyElement = document.body;
const modeToggleSwitch = document.getElementById("modeToggle");
const modeLabelElement = document.getElementById("modeLabel");
/**
* Sets the default theme mode (light/dark) based on system preference or localStorage.
*/
const setDefaultThemeMode = () => {
const prefersDarkMode = window.matchMedia("(prefers-color-scheme: dark)").matches;
const savedThemeMode = localStorage.getItem("modeToggle");
const isDarkMode = savedThemeMode !== null ? JSON.parse(savedThemeMode) : prefersDarkMode;
bodyElement.classList.toggle("light-mode", !isDarkMode);
modeToggleSwitch.checked = isDarkMode;
modeLabelElement.textContent = isDarkMode ? "🌙 Mode" : "🌞 Mode";
};
/**
* Toggles the light/dark theme mode and saves the preference to localStorage.
*/
const toggleThemeMode = () => {
const isDarkMode = modeToggleSwitch.checked;
bodyElement.classList.toggle("light-mode", !isDarkMode);
modeLabelElement.textContent = isDarkMode ? "🌙 Mode" : "🌞 Mode";
localStorage.setItem("modeToggle", JSON.stringify(isDarkMode));
};
/**
* Sets the initial state of the face recording toggle based on localStorage and removes camera if disabled.
*/
const setFaceToggleInitialState = async () => {
const savedFaceToggleState = localStorage.getItem("faceToggle");
const isFaceToggleEnabled = savedFaceToggleState !== null ? JSON.parse(savedFaceToggleState) : false;
faceToggleSwitch.checked = isFaceToggleEnabled;
if (!isFaceToggleEnabled) {
await removeCameraElement();
}
};
/**
* Saves the state of the face recording toggle to localStorage and updates the UI accordingly.
*/
const saveFaceToggleState = async () => {
const isFaceToggleEnabled = faceToggleSwitch.checked;
localStorage.setItem("faceToggle", JSON.stringify(isFaceToggleEnabled));
if (!isFaceToggleEnabled) {
await removeCameraElement();
} else {
await injectCameraScript();
}
};
/**
* Retrieves the active browser tab.
* @returns {Promise<chrome.tabs.Tab>} - The active tab object.
*/
const getActiveBrowserTab = async () => {
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
if (!tabs || tabs.length === 0) throw new Error("No active tab found");
return tabs[0];
};
/**
* Executes a script in the active tab.
* @param {Object} options - The script options.
* @returns {Promise<void>}
*/
const executeScriptInTab = async (options) => {
const activeTab = await getActiveBrowserTab();
options.target = { tabId: activeTab.id };
await chrome.scripting.executeScript(options);
};
/**
* Injects the camera content script into the current tab.
*/
const injectCameraScript = async () => {
await executeScriptInTab({ files: ["content.js"] });
};
/**
* Removes the camera element from the DOM by modifying the current tab's content.
*/
const removeCameraElement = async () => {
await executeScriptInTab({
func: () => {
const cameraElement = document.querySelector("#castaCamera");
if (cameraElement) cameraElement.style.display = "none";
},
});
};
/**
* Checks the recording status from Chrome storage.
* @returns {Promise<{ recording: boolean, type: string }>} - The recording status and type.
*/
const getRecordingStatus = async () => {
const { recording = false, type = "" } = await chrome.storage.local.get(["recording", "type"]);
return { recording, type };
};
/**
* Updates the recording status in Chrome storage.
* @param {boolean} isRecording - Whether recording is active.
* @param {string} [recordingType=""] - The type of recording (e.g., 'tab', 'screen').
* @returns {Promise<void>}
*/
const updateRecordingStatus = async (isRecording, recordingType = "") => {
await chrome.storage.local.set({ recording: isRecording, type: recordingType });
};
/**
* Updates the UI state based on the current recording and toggle states.
*/
const updateUIState = async () => {
const { recording, type } = await getRecordingStatus();
recordTabButton.innerText = recording && type === "tab" ? "Stop Recording" : "Record Tab";
recordScreenButton.innerText = recording && type === "screen" ? "Stop Recording" : "Record Screen";
if (!faceToggleSwitch.checked) {
await removeCameraElement();
}
};
/**
* Toggles recording state based on the current status.
* @param {string} recordingType - The type of recording to toggle ('tab' or 'screen').
*/
const toggleRecordingState = async (recordingType) => {
const { recording } = await getRecordingStatus();
if (recording) {
chrome.runtime.sendMessage({ type: "stop-recording" });
await removeCameraElement();
} else {
if (faceToggleSwitch.checked) {
await injectCameraScript();
}
chrome.runtime.sendMessage({ type: "start-recording", recordingType });
}
await updateUIState();
window.close();
};
/**
* Initializes the popup UI and event listeners.
*/
const initializePopup = async () => {
try {
await updateUIState();
recordScreenButton.addEventListener("click", () => toggleRecordingState("screen"));
recordTabButton.addEventListener("click", () => toggleRecordingState("tab"));
faceToggleSwitch.addEventListener("change", async () => {
await saveFaceToggleState();
await updateUIState();
});
modeToggleSwitch.addEventListener("change", toggleThemeMode);
} catch (error) {
console.error("[Popup Initialization] Error:", error);
}
};
setDefaultThemeMode();
setFaceToggleInitialState();
initializePopup();