Skip to content

Commit b8abe1b

Browse files
committed
Cleanup and more robust audio scribe
1 parent a388ba0 commit b8abe1b

File tree

8 files changed

+25
-6
lines changed

8 files changed

+25
-6
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ node_modules
55
_debug
66
_metadata
77
dist
8-
*.swp # vim temp files
8+
**/*.swp

functional-samples/ai.gemini-on-device-alt-texter/background.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => {
3333
generateAltText(info.srcUrl),
3434
chrome.action.openPopup()
3535
]);
36-
console.log(result);
3736
chrome.runtime.sendMessage({
3837
action: 'alt-text',
3938
text: result.status === 'fulfilled' ? result.value : result.reason.message

functional-samples/ai.gemini-on-device-audio-scribe/bridge.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,8 @@
1515
// Forward messages from the content script in the MAIN world to the
1616
// side panel
1717
window.addEventListener('message', ({ data }) => {
18+
if (data.type !== 'audio-scribe') {
19+
return;
20+
}
1821
chrome.runtime.sendMessage({ data });
1922
});

functional-samples/ai.gemini-on-device-audio-scribe/demo-chat-app/script.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,13 @@ document.addEventListener('DOMContentLoaded', () => {
3232
const audioElement = document.createElement('audio');
3333
audioElement.preload = 'metadata'; // Important for getting duration
3434
const sourceElement = document.createElement('source');
35-
sourceElement.src = msg.audioSrc;
3635
sourceElement.type = 'audio/mpeg'; // Assuming MP3
3736
audioElement.appendChild(sourceElement);
3837

3938
const response = await fetch(msg.audioSrc);
4039
const data = await response.arrayBuffer();
4140
const blob = new Blob([data], { type: 'audio/wav' });
42-
URL.createObjectURL(blob);
41+
sourceElement.src = URL.createObjectURL(blob);
4342
// Keep the audio element in the DOM but hidden for playback logic
4443
audioElement.style.display = 'none';
4544
messageElement.appendChild(audioElement);

functional-samples/ai.gemini-on-device-audio-scribe/demo-chat-app/style.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,7 @@ body {
274274
text-align: right;
275275
flex-shrink: 0; /* Prevent duration from shrinking */
276276
}
277+
278+
#chat {
279+
background-color: red;
280+
}
Binary file not shown.

functional-samples/ai.gemini-on-device-audio-scribe/override-createobject-url.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ const originalCreateObjectURL = URL.createObjectURL;
1919
// and not for all (as we do in this demo)
2020
URL.createObjectURL = (object) => {
2121
const objectUrl = originalCreateObjectURL.call(URL, object);
22-
window.postMessage({ objectUrl });
22+
window.postMessage({ type: 'audio-scribe', objectUrl });
2323
return objectUrl;
2424
};

functional-samples/ai.gemini-on-device-audio-scribe/sidepanel.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
chrome.runtime.onMessage.addListener(async ({ data }) => {
1818
let content;
1919
try {
20+
if (data.type != 'audio-scribe' || !data || !isValidUrl(data.objectUrl)) {
21+
return;
22+
}
2023
// Check if it's an audio file
2124
const audio = await fetch(data.objectUrl);
2225
content = await audio.blob();
@@ -28,7 +31,7 @@ chrome.runtime.onMessage.addListener(async ({ data }) => {
2831
}
2932

3033
// Setup message UI
31-
const messages = document.gelElementById('messages');
34+
const messages = document.getElementById('messages');
3235
const li = document.createElement('li');
3336
li.append('...');
3437
messages.append(li);
@@ -62,3 +65,14 @@ chrome.runtime.onMessage.addListener(async ({ data }) => {
6265
li.textContent = error.message;
6366
}
6467
});
68+
69+
function isValidUrl(string) {
70+
let url;
71+
72+
try {
73+
url = new URL(string);
74+
return true;
75+
} catch (_) {
76+
return false;
77+
}
78+
}

0 commit comments

Comments
 (0)