CaptureController: setFocusBehavior() 方法

可用性有限

此特性不是基線特性,因為它在一些最廣泛使用的瀏覽器中不起作用。

實驗性: 這是一項實驗性技術
在生產中使用此技術之前,請仔細檢查瀏覽器相容性表格

安全上下文: 此功能僅在安全上下文(HTTPS)中可用,且支援此功能的瀏覽器數量有限。

CaptureController 介面的 setFocusBehavior() 方法控制在關聯的 MediaDevices.getDisplayMedia() Promise fulfilled 時,捕獲的選項卡或視窗是否會獲得焦點,還是說焦點將保留在包含捕獲應用程式的選項卡上。

您可以在呼叫 MediaDevices.getDisplayMedia() 之前多次設定此行為,或在其 Promise 解析後立即設定一次。在那之後,焦點行為被認為是最終確定的,無法再更改。

語法

js
setFocusBehavior(focusBehavior)

引數

focusBehavior

一個列舉值,用於描述使用者代理應將焦點轉移到捕獲的顯示錶面,還是將捕獲的應用程式保持焦點。可能的值為 focus-captured-surface(轉移焦點)和 no-focus-change(將焦點保留在捕獲的應用程式上)。

返回值

無 (undefined)。

異常

InvalidStateError DOMException

在以下情況下丟擲

  • 捕獲流已停止。
  • 使用者選擇共享螢幕(displaySurface 型別為 monitor),而不是共享 browser 選項卡或 window — 您無法使監視器獲得焦點。在這種情況下,異常將在 MediaDevices.getDisplayMedia() Promise 解析後丟擲。
  • MediaDevices.getDisplayMedia() Promise fulfilled 後已過去足夠長的時間,焦點行為已最終確定。

示例

基本的 setFocusBehavior() 用法

js
// Create a new CaptureController instance
const controller = new CaptureController();

// Prompt the user to share a tab, window, or screen.
const stream = await navigator.mediaDevices.getDisplayMedia({ controller });

// Query the displaySurface value of the captured video track
const [track] = stream.getVideoTracks();
const displaySurface = track.getSettings().displaySurface;

if (displaySurface === "browser") {
  // Focus the captured tab.
  controller.setFocusBehavior("focus-captured-surface");
} else if (displaySurface === "window") {
  // Do not move focus to the captured window.
  // Keep the capturing page focused.
  controller.setFocusBehavior("no-focus-change");
}

規範

規範
螢幕捕獲
# dom-capturecontroller-setfocusbehavior

瀏覽器相容性

另見