mirror of
https://github.com/GoogleChrome/chrome-extensions-samples.git
synced 2026-03-27 13:29:34 +07:00
* Add sample for using tabCapture in offscreen document. * Add recording item to tabCapture demo. Adds an icon which indicates if recording is currently in progress. * Addressed a number of pieces of feedback. * Apply suggestions from code review Co-authored-by: amysteamdev <37001393+AmySteam@users.noreply.github.com> * Add step to pin extension. * Add comments. --------- Co-authored-by: amysteamdev <37001393+AmySteam@users.noreply.github.com>
58 lines
1.8 KiB
JavaScript
58 lines
1.8 KiB
JavaScript
// Copyright 2023 Google LLC
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
chrome.action.onClicked.addListener(async (tab) => {
|
|
const existingContexts = await chrome.runtime.getContexts({});
|
|
let recording = false;
|
|
|
|
const offscreenDocument = existingContexts.find(
|
|
(c) => c.contextType === 'OFFSCREEN_DOCUMENT'
|
|
);
|
|
|
|
// If an offscreen document is not already open, create one.
|
|
if (!offscreenDocument) {
|
|
// Create an offscreen document.
|
|
await chrome.offscreen.createDocument({
|
|
url: 'offscreen.html',
|
|
reasons: ['USER_MEDIA'],
|
|
justification: 'Recording from chrome.tabCapture API'
|
|
});
|
|
} else {
|
|
recording = offscreenDocument.documentUrl.endsWith('#recording');
|
|
}
|
|
|
|
if (recording) {
|
|
chrome.runtime.sendMessage({
|
|
type: 'stop-recording',
|
|
target: 'offscreen'
|
|
});
|
|
chrome.action.setIcon({ path: 'icons/not-recording.png' });
|
|
return;
|
|
}
|
|
|
|
// Get a MediaStream for the active tab.
|
|
const streamId = await chrome.tabCapture.getMediaStreamId({
|
|
targetTabId: tab.id
|
|
});
|
|
|
|
// Send the stream ID to the offscreen document to start recording.
|
|
chrome.runtime.sendMessage({
|
|
type: 'start-recording',
|
|
target: 'offscreen',
|
|
data: streamId
|
|
});
|
|
|
|
chrome.action.setIcon({ path: '/icons/recording.png' });
|
|
});
|