-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoffscreen.js
More file actions
113 lines (104 loc) · 4.21 KB
/
Copy pathoffscreen.js
File metadata and controls
113 lines (104 loc) · 4.21 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
/**
* Offscreen document serves to take care of the tab capture as service worker is prohibited from doing so.
*/
let mediaRecorder = null;
let audioChunks = [];
let captureStream = null;
let geminiApiKey = null;
// Receive messages to know when to setup media and start/stop recording
chrome.runtime.onMessage.addListener((message) => {
if (message.type === 'SETUP_STREAM') {
geminiApiKey = message.geminiApiKey;
setupStream(message.streamId);
}
if (message.type === 'START_RECORDING') {
if (captureStream && (!mediaRecorder || mediaRecorder.state === 'inactive')) {
audioChunks = [];
mediaRecorder = new MediaRecorder(captureStream);
mediaRecorder.ondataavailable = (e) => audioChunks.push(e.data);
mediaRecorder.onstop = () => {
const audioBlob = new Blob(audioChunks, { type: 'audio/webm' });
console.log('Audio captured, blob size:', audioBlob.size);
// Next step: send audioBlob to Gemini for transcription
if (audioBlob.size === 0){
console.log('Audio uncaptured, transcription cancelled.');
return;
}
try {
transcribeAndSummarize(audioBlob);
console.log("Transcription and summarization complete.");
} catch (err){
console.error("Failed to transcribe:", err);
}
};
mediaRecorder.start();
}
}
if (message.type === 'STOP_RECORDING') {
if (mediaRecorder && mediaRecorder.state !== 'inactive') {
mediaRecorder.stop();
}
}
});
// Set up capture stream to obtain tab audio
async function setupStream(streamId) {
captureStream = await navigator.mediaDevices.getUserMedia({
audio: {
mandatory: {
chromeMediaSource: 'tab',
chromeMediaSourceId: streamId
}
}
});
const audioContext = new AudioContext();
audioContext.createMediaStreamSource(captureStream).connect(audioContext.destination);
console.log('Stream ready');
}
// Transcribe the audio blob, have Gemini return a summary.
async function transcribeAndSummarize(audioBlob) {
if (!geminiApiKey){
console.error("Gemini API key not found for transcription.");
}
const base64Audio = await blobToBase64(audioBlob);
const response = await fetch(
`https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=${geminiApiKey}`,
{
method: 'POST',
headers: { 'Content-Type': 'application/json'},
body: JSON.stringify({
contents: [{
parts: [
{
inline_data: {
mime_type: 'audio/webm',
data: base64Audio
}
},
{
text: 'Transcribe this audio. If it is not in English, translate \
it into English first. Do not give me the transcription of what was said, just \
use it to present the educational summary of what was said. \
You are to act as a a mentor / learning assistant \
to help the user retain the information that has been presented in the audio. \
Provide a concise summary of the key points covered in an educational format.'
}
]
}]
})
}
);
const data = await response.json();
console.log(data);
const summary = data.candidates[0].content.parts[0].text;
console.log('Summary:', summary);
chrome.runtime.sendMessage({type: 'SUMMARY_COMPLETE', summary: summary});
}
// Audio needs to be converted to Base 64
function blobToBase64(blob) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result.split(',')[1]);
reader.onerror = reject;
reader.readAsDataURL(blob);
});
}