-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
292 lines (264 loc) · 11.5 KB
/
content.js
File metadata and controls
292 lines (264 loc) · 11.5 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// harrydbarnes/trainthemlater/TrainThemLater-main/content.js
let isRecording = false;
let audioSettingForNextStart = false;
let pageUrlForNextStart = '';
let overlayContainer;
let startButton;
let stopButton;
function initOverlay() {
if (document.getElementById('ttlOverlayContainer')) {
overlayContainer = document.getElementById('ttlOverlayContainer');
} else {
overlayContainer = document.createElement('div');
overlayContainer.id = 'ttlOverlayContainer';
if (document.body) {
document.body.appendChild(overlayContainer);
} else {
window.addEventListener('DOMContentLoaded', () => document.body.appendChild(overlayContainer));
return;
}
}
overlayContainer.style.cssText = `
position: fixed; bottom: 20px; right: 20px; z-index: 2147483647;
display: none; background-color: rgba(0, 0, 0, 0.7); padding: 12px 15px;
border-radius: 12px; box-shadow: 0px 4px 15px rgba(0,0,0,0.3); font-family: Arial, sans-serif;
`;
startButton = document.getElementById('ttlOverlayStartButton');
if (!startButton) {
startButton = document.createElement('button');
startButton.id = 'ttlOverlayStartButton';
overlayContainer.appendChild(startButton);
}
startButton.textContent = 'Start Record';
stopButton = document.getElementById('ttlOverlayStopButton');
if (!stopButton) {
stopButton = document.createElement('button');
stopButton.id = 'ttlOverlayStopButton';
overlayContainer.appendChild(stopButton);
}
stopButton.textContent = 'Stop Record';
const commonButtonStyle = `
padding: 10px 18px; color: white; border: none; border-radius: 8px;
cursor: pointer; margin: 0 8px; font-size: 15px; font-weight: bold;
transition: background-color 0.2s ease-in-out, transform 0.1s ease;
box-shadow: 0 2px 4px rgba(0,0,0,0.2);
`;
startButton.style.cssText = commonButtonStyle + `background-color: #28a745;`;
startButton.onmouseover = () => { startButton.style.backgroundColor = '#218838'; startButton.style.transform = 'scale(1.03)'; };
startButton.onmouseout = () => { startButton.style.backgroundColor = '#28a745'; startButton.style.transform = 'scale(1)'; };
stopButton.style.cssText = commonButtonStyle + `background-color: #dc3545;`;
stopButton.onmouseover = () => { stopButton.style.backgroundColor = '#c82333'; stopButton.style.transform = 'scale(1.03)'; };
stopButton.onmouseout = () => { stopButton.style.backgroundColor = '#dc3545'; stopButton.style.transform = 'scale(1)'; };
startButton.removeEventListener('click', handleStartClick);
startButton.addEventListener('click', handleStartClick);
stopButton.removeEventListener('click', handleStopClick);
stopButton.addEventListener('click', handleStopClick);
updateOverlayButtonsUI(isRecording);
}
function handleStartClick() {
if (isRecording || (startButton && startButton.disabled)) {
console.warn("Content.js: Start clicked, but already recording or start is in progress.");
return;
}
if (startButton) {
startButton.disabled = true;
startButton.textContent = 'Starting...';
}
if (stopButton) {
stopButton.style.display = 'none';
}
console.log("Content.js: Overlay Start Button clicked. Audio pref:", audioSettingForNextStart, "URL:", pageUrlForNextStart);
chrome.runtime.sendMessage({
action: 'startRecording',
recordAudio: audioSettingForNextStart,
pageUrl: pageUrlForNextStart
}, (response) => {
if (chrome.runtime.lastError) {
console.error("Content.js: Error sending 'startRecording' message to background:", chrome.runtime.lastError.message);
if (startButton) {
startButton.disabled = false;
startButton.textContent = 'Start Record';
}
} else if (response && response.success) {
console.log("Content.js: 'startRecording' message acknowledged by background. Waiting for 'recordingActuallyStarted'.");
} else {
console.error("Content.js: Failed to start recording (background response):", response ? response.error : "No response or error");
if (startButton) {
startButton.disabled = false;
startButton.textContent = 'Start Record';
}
}
});
}
function handleStopClick() {
if (!isRecording || (stopButton && stopButton.disabled)) {
console.warn("Content.js: Stop clicked, but not recording or stop is in progress.");
return;
}
if (stopButton) {
stopButton.disabled = true;
stopButton.textContent = 'Stopping...';
}
console.log("Content.js: Overlay Stop Button clicked.");
chrome.runtime.sendMessage({ action: 'stopRecording' }, (response) => {
if (chrome.runtime.lastError) {
console.error("Content.js: Error sending 'stopRecording' message:", chrome.runtime.lastError.message);
// Reset button even on error
if (stopButton) {
stopButton.disabled = false;
stopButton.textContent = 'Stop Record';
}
} else if (response && response.success) {
console.log("Content.js: 'stopRecording' message acknowledged by background.");
// UI update is now handled by 'recordingActuallyStopped' from background.js
} else {
console.error("Content.js: Failed to stop recording (background response):", response ? response.error : "No error message");
// Reset button on failure
if (stopButton) {
stopButton.disabled = false;
stopButton.textContent = 'Stop Record';
}
}
});
}
function updateOverlayButtonsUI(isRec) {
if (startButton) {
startButton.style.display = isRec ? 'none' : 'inline-block';
startButton.disabled = false;
startButton.textContent = 'Start Record';
}
if (stopButton) {
stopButton.style.display = isRec ? 'inline-block' : 'none';
stopButton.disabled = false;
stopButton.textContent = 'Stop Record';
}
console.log(`Content.js: Overlay buttons UI updated. isRecording visual state is now: ${isRec}`);
}
if (document.body) {
initOverlay();
} else {
document.addEventListener("DOMContentLoaded", initOverlay);
}
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
let responded = false;
const ensureResponse = (responseValue) => {
if (!responded) {
try { sendResponse(responseValue); } catch (e) { console.warn("Content.js: sendResponse failed for action '"+message.action+"':", e.message); }
responded = true;
}
};
console.log("Content.js: Received message:", message.action);
switch (message.action) {
case 'showOverlayButtons':
if (!overlayContainer && document.body) initOverlay();
if (overlayContainer) overlayContainer.style.display = 'block';
chrome.runtime.sendMessage({ action: 'getRecordingState' }, (response) => {
if (chrome.runtime.lastError) {
isRecording = false;
console.warn("Content.js: Error getting recording state on showOverlayButtons:", chrome.runtime.lastError.message);
updateOverlayButtonsUI(isRecording);
ensureResponse({success: false, error: chrome.runtime.lastError.message});
return;
}
if (response) {
isRecording = !!response.isRecording;
} else {
isRecording = false;
}
updateOverlayButtonsUI(isRecording);
ensureResponse({success: true});
});
return true;
case 'hideOverlayButtons':
if (overlayContainer) overlayContainer.style.display = 'none';
ensureResponse({success: true});
return false;
case 'setAudioAndUrlPreference':
audioSettingForNextStart = message.recordAudio;
pageUrlForNextStart = message.pageUrl;
console.log("Content.js: Audio preference set to", audioSettingForNextStart, "URL set to", pageUrlForNextStart);
ensureResponse({success: true});
return false;
case 'recordingActuallyStarted':
console.log("Content.js: 'recordingActuallyStarted' received from background.");
isRecording = true;
updateOverlayButtonsUI(true);
ensureResponse({success: true});
return false;
case 'recordingActuallyStopped':
console.log("Content.js: 'recordingActuallyStopped' received from background.");
isRecording = false;
updateOverlayButtonsUI(false); // Reset buttons to initial state
if (overlayContainer) {
overlayContainer.style.display = 'none'; // Hide the overlay
}
ensureResponse({success: true});
return false;
case 'recordingStateChanged':
isRecording = message.newIsRecordingState;
updateOverlayButtonsUI(isRecording);
ensureResponse({success: true});
return false;
case 'triggerStartRecording':
if (!isRecording && startButton && !startButton.disabled) {
audioSettingForNextStart = message.recordAudio;
pageUrlForNextStart = message.pageUrl;
console.log("Content.js: Triggering overlay start button click. Audio pref:", audioSettingForNextStart, "URL:", pageUrlForNextStart);
startButton.click();
} else {
console.warn("Content.js: triggerStartRecording received but already recording, no start button, or start already in progress.");
}
ensureResponse({success: true});
return false;
case 'triggerStopRecording':
if (isRecording && stopButton && !stopButton.disabled) {
console.log("Content.js: Triggering overlay stop button click.");
stopButton.click();
} else {
console.warn("Content.js: triggerStopRecording received but not recording, no stop button, or stop already in progress.");
}
ensureResponse({success: true});
return false;
default:
console.log("Content.js: Unknown action received:", message.action);
ensureResponse({error: "Unknown action in content.js"});
return false;
}
});
chrome.runtime.sendMessage({ action: 'getRecordingState' }, (response) => {
if (chrome.runtime.lastError) { return; }
if (response) {
isRecording = !!response.isRecording;
if (!overlayContainer && document.body) {
initOverlay();
} else if (overlayContainer && overlayContainer.style.display === 'block') {
updateOverlayButtonsUI(isRecording);
} else if (overlayContainer) {
updateOverlayButtonsUI(isRecording);
}
}
});
document.addEventListener('click', (event) => {
if (overlayContainer && overlayContainer.contains(event.target)) return;
let overlayWasVisible = false;
if (overlayContainer && overlayContainer.style.display !== 'none') {
overlayWasVisible = true;
overlayContainer.style.display = 'none';
}
if (isRecording) {
const clickX = event.clientX; const clickY = event.clientY;
setTimeout(() => {
chrome.runtime.sendMessage({ action: 'captureScreenshot', clickX, clickY }, (response) => {
if (chrome.runtime.lastError) console.error("Content.js: Error sending captureScreenshot:", chrome.runtime.lastError.message);
if (overlayWasVisible && overlayContainer) {
overlayContainer.style.display = 'block';
}
});
}, 50);
} else {
if (overlayWasVisible && overlayContainer) {
overlayContainer.style.display = 'block';
}
}
});
console.log("TrainThemLater content script loaded.");