توضیحات
از API chrome.tabCapture برای تعامل با جریانهای رسانهای تب استفاده کنید.
مجوزها
tabCaptureنمای کلی
The chrome.tabCapture API allows you to access a MediaStream containing video and audio of the current tab. It can only be called after the user invokes an extension, such as by clicking the extension's action button . This is similar to the behavior of the activeTab permission.
حفظ صدای سیستم
When a MediaStream is obtained for a tab, audio in that tab will no longer be played to the user. This is similar to the behavior of the getDisplayMedia() function when the suppressLocalAudioPlayback flag is set to true.
برای ادامه پخش صدا برای کاربر، از موارد زیر استفاده کنید:
const output = new AudioContext();
const source = output.createMediaStreamSource(stream);
source.connect(output.destination);
این یک AudioContext جدید ایجاد میکند و صدای MediaStream تب را به مقصد پیشفرض متصل میکند.
شناسههای جریان
فراخوانی chrome.tabCapture.getMediaStreamId یک شناسه جریان (stream ID) را برمیگرداند. برای دسترسی بعدی به MediaStream از طریق شناسه، از کد زیر استفاده کنید:
navigator.mediaDevices.getUserMedia({
audio: {
mandatory: {
chromeMediaSource: "tab",
chromeMediaSourceId: id,
},
},
video: {
mandatory: {
chromeMediaSource: "tab",
chromeMediaSourceId: id,
},
},
});
محدودیتهای استفاده
پس از فراخوانی getMediaStreamId() ، محدودیتهایی در مورد محل استفاده از شناسه جریان برگشتی وجود دارد:
- اگر
consumerTabIdمشخص شده باشد، شناسه میتواند توسط فراخوانیgetUserMedia()در هر فریمی در تب داده شده که منشأ امنیتی یکسانی دارد، استفاده شود. - When this is not specified, begininning in Chrome 116, the ID can be used in any frame with the same security origin in the same render process as the caller. This means that a stream ID obtained in a service worker can be used in an offscreen document .
قبل از کروم ۱۱۶، وقتی consumerTabId مشخص نشده بود، شناسه جریان به مبدا امنیتی، فرآیند رندر و فریم رندر فراخواننده محدود میشد.
بیشتر بدانید
To learn more about how to use the chrome.tabCapture API, see Audio recording and screen capture . This demonstrates how to use tabCapture and related APIs to solve a number of common use cases.
انواع
CaptureInfo
خواص
- تمام صفحه
بولی
اینکه آیا عنصری در برگهای که در حال ضبط شدن است، در حالت تمام صفحه قرار دارد یا خیر.
- وضعیت
وضعیت جدید ضبط تب.
- شناسه برگه
شماره
شناسهی برگهای که وضعیت آن تغییر کرده است.
CaptureOptions
خواص
- صوتی
بولی اختیاری
- محدودیتهای صوتی
محدودیت MediaStream اختیاری است
- ویدئو
بولی اختیاری
- محدودیتهای ویدیو
محدودیت MediaStream اختیاری است
GetMediaStreamOptions
خواص
- تبآیدی مصرفکننده
شماره اختیاری
Optional tab id of the tab which will later invoke
getUserMedia()to consume the stream. If not specified then the resulting stream can be used only by the calling extension. The stream can only be used by frames in the given tab whose security origin matches the consumber tab's origin. The tab's origin must be a secure origin, eg HTTPS. - شناسه هدف
شماره اختیاری
Optional tab id of the tab which will be captured. If not specified then the current active tab will be selected. Only tabs for which the extension has been granted the
activeTabpermission can be used as the target tab.
MediaStreamConstraint
خواص
- اجباری
شیء
- اختیاری
شیء اختیاری
TabCaptureState
شمارشی
«در انتظار» "فعال" "متوقف شد" "خطا"
روشها
capture()
chrome.tabCapture.capture(
options: CaptureOptions,
callback: function,
): void
Captures the visible area of the currently active tab. Capture can only be started on the currently active tab after the extension has been invoked , similar to the way that activeTab works. Capture is maintained across page navigations within the tab, and stops when the tab is closed, or the media stream is closed by the extension.
پارامترها
- گزینهها
جریان رسانهای برگشتی را پیکربندی میکند.
- تماس برگشتی
تابع
پارامتر
callbackبه شکل زیر است:(stream: LocalMediaStream) => void
- جریان
پخش محلی مدیا
getCapturedTabs()
chrome.tabCapture.getCapturedTabs(
callback?: function,
): Promise<CaptureInfo[]>
Returns a list of tabs that have requested capture or are being captured, ie status != stopped and status != error. This allows extensions to inform the user that there is an existing tab capture that would prevent a new tab capture from succeeding (or to prevent redundant requests for the same tab).
پارامترها
- تماس برگشتی
تابع اختیاری
پارامتر
callbackبه شکل زیر است:(result: CaptureInfo[]) => void
- نتیجه
اطلاعات ضبط []
بازگشتها
قول< CaptureInfo []>
کروم ۱۱۶+یک Promise برمیگرداند که با CaptureInfo[] برای تبهای گرفتهشده، اجرا میشود.
Promiseها فقط برای Manifest V3 و نسخههای بعدی پشتیبانی میشوند، سایر پلتفرمها باید از callbackها استفاده کنند.
getMediaStreamId()
chrome.tabCapture.getMediaStreamId(
options?: GetMediaStreamOptions,
callback?: function,
): Promise<string>
یک شناسه جریان برای ضبط تب هدف ایجاد میکند. مشابه متد chrome.tabCapture.capture() است، اما به جای یک جریان رسانه، یک شناسه جریان رسانه را به تب مصرفکننده برمیگرداند.
پارامترها
- گزینهها
GetMediaStreamOptions اختیاری است
- تماس برگشتی
تابع اختیاری
پارامتر
callbackبه شکل زیر است:(streamId: string) => void
- استریم آیدی
رشته
بازگشتها
قول<string>
کروم ۱۱۶+Returns a Promise which resolves with the result. If successful, the result is an opaque string that can be passed to the
getUserMedia()API to generate a media stream that corresponds to the target tab. The createdstreamIdcan only be used once and expires after a few seconds if it is not used.Promiseها فقط برای Manifest V3 و نسخههای بعدی پشتیبانی میشوند، سایر پلتفرمها باید از callbackها استفاده کنند.
رویدادها
onStatusChanged
chrome.tabCapture.onStatusChanged.addListener(
callback: function,
)
این رویداد زمانی فعال میشود که وضعیت ضبط یک تب تغییر کند. این به نویسندگان افزونه اجازه میدهد تا وضعیت ضبط تبها را پیگیری کنند تا عناصر رابط کاربری مانند اقدامات صفحه را همگام نگه دارند.
پارامترها
- تماس برگشتی
تابع
پارامتر
callbackبه شکل زیر است:(info: CaptureInfo) => void
- اطلاعات