-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathoffscreen.js
More file actions
332 lines (279 loc) · 9.79 KB
/
Copy pathoffscreen.js
File metadata and controls
332 lines (279 loc) · 9.79 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import {
buildTimestampedFilename,
getUnsupportedFormatMessage,
normalizeExportFormat,
resolveRecorderProfile,
} from './media-format.js';
// Offscreen document for MediaRecorder (needs DOM context)
let mediaRecorder = null;
let recordedChunks = [];
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'checkFormatSupport') {
sendResponse({
supported: resolveRecorderProfile(message.format) !== null,
});
return true;
}
if (message.action === 'startCapture') {
startCapture(message.streamId, message.tabId, message.duration, message.delay, message.format);
return true;
}
if (message.action === 'stopCapture') {
console.log('Received stopCapture, stopping now');
if (mediaRecorder && mediaRecorder.state !== 'inactive') {
mediaRecorder.stop();
}
return true;
}
// Don't handle other messages — let the service worker respond
});
async function startCapture(streamId, tabId, duration, delay, format) {
format = normalizeExportFormat(format);
try {
// Get the media stream from the streamId
const stream = await navigator.mediaDevices.getUserMedia({
audio: false,
video: {
mandatory: {
chromeMediaSource: 'tab',
chromeMediaSourceId: streamId,
minFrameRate: 30,
maxFrameRate: 60,
},
},
});
const recorderProfile = resolveRecorderProfile(format);
if (!recorderProfile) {
throw new Error(getUnsupportedFormatMessage(format));
}
// Set up MediaRecorder
recordedChunks = [];
mediaRecorder = new MediaRecorder(stream, {
mimeType: recorderProfile.mimeType,
videoBitsPerSecond: 16000000,
});
mediaRecorder.ondataavailable = (event) => {
console.log('Data available:', event.data.size, 'bytes');
if (event.data.size > 0) {
recordedChunks.push(event.data);
}
};
mediaRecorder.onstop = async () => {
console.log('MediaRecorder stopped, chunks:', recordedChunks.length);
stream.getTracks().forEach(track => track.stop());
const blob = new Blob(recordedChunks, { type: recorderProfile.blobType });
console.log('Blob size:', blob.size, 'bytes');
if (blob.size === 0) {
console.error('Empty recording - no data captured');
chrome.runtime.sendMessage({ action: 'recordingComplete', tabId });
return;
}
if (format === 'gif') {
await convertToGif(blob);
} else {
await downloadVideo(blob, recorderProfile.extension);
}
chrome.runtime.sendMessage({ action: 'recordingComplete', tabId });
};
mediaRecorder.onerror = (event) => {
console.error('MediaRecorder error:', event.error);
};
// Start recording
mediaRecorder.start(100);
console.log('Recording started, waiting for delay...');
// Wait for initial delay
await sleep(delay * 1000);
// Ask service worker to inject scroll script (scripting API not available in offscreen)
console.log('Requesting scroll injection for tab', tabId, 'duration', duration);
chrome.runtime.sendMessage({
action: 'injectScroll',
tabId,
duration,
}, (response) => {
console.log('Scroll injection response:', response);
});
// Stop recording after scroll completes + short buffer.
// This is the primary stop mechanism — the offscreen document's timers
// are reliable (unlike the service worker which sleeps after 30s in MV3).
await sleep((duration + 2) * 1000);
if (mediaRecorder && mediaRecorder.state !== 'inactive') {
console.log('Stopping recording (scroll duration + 2s buffer elapsed)');
mediaRecorder.stop();
}
} catch (error) {
console.error('Capture error:', error);
chrome.runtime.sendMessage({ action: 'recordingComplete', tabId });
}
}
// Download captured video to disk
async function downloadVideo(blob, extension) {
const filename = buildTimestampedFilename(extension);
// Download via blob URL. Try multiple approaches since offscreen
// documents have limited API access and base64 via sendMessage
// silently fails for large recordings (100MB+ at 16Mbps).
const blobUrl = URL.createObjectURL(blob);
console.log('Downloading via blob URL, blob size:', blob.size);
let downloaded = false;
// Approach 1: chrome.downloads API directly in offscreen
if (typeof chrome.downloads?.download === 'function') {
try {
const downloadId = await chrome.downloads.download({
url: blobUrl,
filename: filename,
saveAs: true,
});
console.log('chrome.downloads succeeded, ID:', downloadId);
downloaded = true;
} catch (e) {
console.warn('chrome.downloads failed:', e.message);
}
}
// Approach 2: anchor element click (DOM-based download)
if (!downloaded) {
try {
console.log('Trying anchor click download...');
const a = document.createElement('a');
a.href = blobUrl;
a.download = filename;
document.body.appendChild(a);
a.click();
a.remove();
console.log('Anchor click download triggered');
downloaded = true;
} catch (e) {
console.warn('Anchor click failed:', e.message);
}
}
// Approach 3: send blob URL to service worker (small message)
if (!downloaded) {
console.log('Sending blob URL to service worker for download...');
chrome.runtime.sendMessage({
action: 'downloadVideo',
dataUrl: blobUrl,
filename: filename,
});
}
// Revoke after delay to let download read the blob
setTimeout(() => URL.revokeObjectURL(blobUrl), 60000);
}
// Convert WebM blob to GIF and download
async function convertToGif(webmBlob) {
console.log('Converting to GIF...');
chrome.runtime.sendMessage({
action: 'updateBadge',
text: 'GIF',
});
try {
const video = document.getElementById('gifVideo');
const canvas = document.getElementById('gifCanvas');
const ctx = canvas.getContext('2d');
// Load the WebM into the video element
const videoUrl = URL.createObjectURL(webmBlob);
video.src = videoUrl;
await new Promise((resolve, reject) => {
video.onloadedmetadata = resolve;
video.onerror = () => reject(new Error('Failed to load video'));
setTimeout(() => reject(new Error('Video load timeout')), 30000);
});
// Calculate output dimensions (max 960px wide)
const MAX_WIDTH = 960;
const GIF_FPS = 10;
let outWidth = video.videoWidth;
let outHeight = video.videoHeight;
if (outWidth > MAX_WIDTH) {
const scale = MAX_WIDTH / outWidth;
outWidth = MAX_WIDTH;
outHeight = Math.round(video.videoHeight * scale);
}
// Ensure even dimensions
outWidth = outWidth & ~1;
outHeight = outHeight & ~1;
canvas.width = outWidth;
canvas.height = outHeight;
const frameDelay = Math.round(1000 / GIF_FPS);
const encoder = new GifEncoder(outWidth, outHeight, { delay: frameDelay });
// Get video duration
// Some WebM files report Infinity duration; seek to end to get real duration
if (!isFinite(video.duration)) {
video.currentTime = 1e10;
await new Promise(resolve => {
video.onseeked = resolve;
setTimeout(resolve, 5000);
});
video.currentTime = 0;
await new Promise(resolve => {
video.onseeked = resolve;
setTimeout(resolve, 2000);
});
}
const videoDuration = video.duration;
console.log('Video duration:', videoDuration, 'Output:', outWidth, 'x', outHeight, '@', GIF_FPS, 'fps');
const totalFrames = Math.floor(videoDuration * GIF_FPS);
let framesEncoded = 0;
// Seek through video and capture frames
for (let i = 0; i < totalFrames; i++) {
const seekTime = i / GIF_FPS;
video.currentTime = seekTime;
await new Promise((resolve) => {
video.onseeked = resolve;
setTimeout(resolve, 1000); // timeout fallback
});
// Draw frame to canvas
ctx.drawImage(video, 0, 0, outWidth, outHeight);
const imageData = ctx.getImageData(0, 0, outWidth, outHeight);
encoder.addFrame(imageData.data);
framesEncoded++;
// Update badge with progress
if (framesEncoded % 5 === 0 || framesEncoded === totalFrames) {
const pct = Math.round((framesEncoded / totalFrames) * 100);
chrome.runtime.sendMessage({
action: 'updateBadge',
text: `${pct}%`,
});
console.log(`GIF encoding: ${pct}% (${framesEncoded}/${totalFrames} frames)`);
}
}
encoder.finish();
const gifBlob = encoder.getBlob();
console.log('GIF encoded:', gifBlob.size, 'bytes,', framesEncoded, 'frames');
URL.revokeObjectURL(videoUrl);
// Download the GIF
const timestamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, 19);
const filename = `scrollywood-${timestamp}.gif`;
const gifUrl = URL.createObjectURL(gifBlob);
let downloaded = false;
if (typeof chrome.downloads?.download === 'function') {
try {
await chrome.downloads.download({
url: gifUrl,
filename: filename,
saveAs: true,
});
downloaded = true;
} catch (e) {
console.warn('chrome.downloads failed for GIF:', e.message);
}
}
if (!downloaded) {
const a = document.createElement('a');
a.href = gifUrl;
a.download = filename;
document.body.appendChild(a);
a.click();
a.remove();
}
setTimeout(() => URL.revokeObjectURL(gifUrl), 60000);
} catch (error) {
console.error('GIF conversion failed:', error);
console.log('Falling back to WebM download...');
chrome.runtime.sendMessage({
action: 'updateBadge',
text: 'ERR',
});
// Fall back to WebM
await downloadVideo(webmBlob, 'webm');
}
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}