-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
81b4d30
commit 2656434
Showing
6 changed files
with
169 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
functional-samples/ai.gemini-in-the-cloud/sidepanel/screenshot.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const getScreenshot = async () => { | ||
const imageDataUri = await chrome.tabs.captureVisibleTab({ format: 'png' }); | ||
const byteString = atob(imageDataUri.split(',')[1]); | ||
const ab = new ArrayBuffer(byteString.length); | ||
const ia = new Uint8Array(ab); | ||
for (let i = 0; i < byteString.length; i++) { | ||
ia[i] = byteString.charCodeAt(i); | ||
} | ||
return { | ||
data: ia, | ||
base64: await blobToBase64(new Blob([ia], { type: 'image/png' })) | ||
}; | ||
}; | ||
|
||
const blobToBase64 = async (imageDataUri) => { | ||
return new Promise((resolve, reject) => { | ||
const reader = new FileReader(); | ||
reader.readAsDataURL(imageDataUri); | ||
reader.onloadend = () => resolve(reader.result.split(',')[1]); | ||
reader.onerror = reject; | ||
}); | ||
}; | ||
|
||
export default getScreenshot; |
60 changes: 60 additions & 0 deletions
60
functional-samples/ai.gemini-in-the-cloud/sidepanel/tab-audio-recorder.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
export default class TabAudioRecorder { | ||
constructor() { | ||
this.recorder = null; | ||
this.data = []; | ||
} | ||
|
||
async start() { | ||
/* | ||
const currentTab = await chrome.tabs.query({ | ||
active: true, | ||
currentWindow: true | ||
}); | ||
console.log('currentab', currentTab); | ||
const streamId = await chrome.tabCapture.getMediaStreamId({ | ||
targetTabId: currentTab.id | ||
}); | ||
const media = await navigator.mediaDevices.getUserMedia({ | ||
audio: { | ||
mandatory: { | ||
chromeMediaSource: 'tab', | ||
chromeMediaSourceId: streamId | ||
} | ||
} | ||
}); | ||
*/ | ||
|
||
const media = await navigator.mediaDevices.getDisplayMedia({ | ||
audio: true, | ||
video: false | ||
}); | ||
|
||
// Continue to play the captured audio to the user. | ||
const output = new AudioContext(); | ||
const source = output.createMediaStreamSource(media); | ||
source.connect(output.destination); | ||
// Start recording. | ||
this.recorder = new MediaRecorder(media, { mimeType: 'audio/wav' }); | ||
this.recorder.ondataavailable = (event) => this.data.push(event.data); | ||
this.recorder.onstop = () => { | ||
const blob = new Blob(this.data, { type: 'video/wav' }); | ||
window.open(URL.createObjectURL(blob), '_blank'); | ||
|
||
// Clear state ready for next recording | ||
this.recorder = undefined; | ||
this.data = []; | ||
}; | ||
this.recorder.start(); | ||
} | ||
|
||
stop() { | ||
if (!this.recorder) { | ||
return; | ||
} | ||
this.recorder.stop(); | ||
// Stopping the tracks makes sure the recording icon in the tab is removed. | ||
this.recorder.stream.getTracks().forEach((t) => t.stop()); | ||
} | ||
} |