From 3f5025eba18d07c319fee9d28e215d315ef5d1b2 Mon Sep 17 00:00:00 2001 From: MDN Date: Fri, 11 Mar 2022 00:14:44 +0000 Subject: [CRON] sync translated content --- .../web/api/mediadevices/ondevicechange/index.html | 202 --------------------- .../window/beforeinstallprompt_event/index.html | 67 +++++++ .../api/window/onbeforeinstallprompt/index.html | 66 ------- 3 files changed, 67 insertions(+), 268 deletions(-) delete mode 100644 files/zh-cn/web/api/mediadevices/ondevicechange/index.html create mode 100644 files/zh-cn/web/api/window/beforeinstallprompt_event/index.html delete mode 100644 files/zh-cn/web/api/window/onbeforeinstallprompt/index.html (limited to 'files/zh-cn/web/api') diff --git a/files/zh-cn/web/api/mediadevices/ondevicechange/index.html b/files/zh-cn/web/api/mediadevices/ondevicechange/index.html deleted file mode 100644 index 1bf012abf9..0000000000 --- a/files/zh-cn/web/api/mediadevices/ondevicechange/index.html +++ /dev/null @@ -1,202 +0,0 @@ ---- -title: MediaDevices.ondevicechange -slug: Web/API/MediaDevices/ondevicechange -translation_of: Web/API/MediaDevices/ondevicechange ---- -

{{APIRef("Media Capture and Streams")}}

- -

 MediaDevices.ondevicechange 属性是一种{{event("Event_handlers", "event handler")}},当{{domxref("MediaDevices")}} 接口的{{event("devicechange")}}事件被触发时会被调用. 不论{{Glossary("user agent")}}媒体设备的设置是否可用, 或者网站或者应用发生变了都会触发。无论何时你都可以使用 {{domxref("MediaDevices.enumerateDevices", "enumerateDevices()")}}  去更新可用设备列表.

- -

Syntax

- -
MediaDevices.ondevicechange = eventHandler;
-
- -

Value

- -

A function you provide which accepts as input a {{domxref("Event")}} object describing the {{domxref("devicehange")}} event that occurred. There is no information about the change included in the event object; to get the updated list of devices, you'll have to use {{domxref("MediaDevices.enumerateDevices", "enumerateDevices()")}}.

- -

Example

- -

In this example, we create a function called updateDeviceList(), which is called once when {{domxref("MediaDevices.getUserMedia()")}} successfully obtains a stream, and then is called any time the device list changes. It displays in the browser window two lists: one of audio devices and one of video devices, with both the device's label (name) and whether it's an input or an output device. Because the example provides a handler for the {{event("devicechange")}} event, the list is refreshed any time a media device is attached to or removed from the device running the sample.

- - - -

We set up global variables that contain references to the {{HTMLElement("ul")}} elements that are used to list the audio and video devices:

- -
let audioList = document.getElementById("audioList");
-let videoList = document.getElementById("videoList");
- -

Getting and drawing the device list

- -

Now let's take a look at updateDeviceList() itself. This method is called any time we want to fetch the current list of media devices and then update the displayed lists of audo and video devices using that information.

- -
function updateDeviceList() {
-  navigator.mediaDevices.enumerateDevices()
-  .then(function(devices) {
-    audioList.innerHTML = "";
-    videoList.innerHTML = "";
-
-    devices.forEach(function(device) {
-      let elem = document.createElement("li");
-      let [kind, type, direction] = device.kind.match(/(\w+)(input|output)/i);
-
-      elem.innerHTML = "<strong>" + device.label + "</strong> (" + direction + ")";
-      if (type === "audio") {
-        audioList.appendChild(elem);
-      } else if (type === "video") {
-        videoList.appendChild(elem);
-      }
-    });
-  });
-}
- -

updateDeviceList() consists entirely of a call to the function {{domxref("MediaDevices.enumerateDevices", "enumerateDevices()")}} on the {{domxref("MediaDevices")}} object referenced in the {{domxref("navigator.mediaDevices")}} property, as well as the code that's run when the {{jsxref("promise")}} returned by enumerateDevices() is fulfilled. The fulfillment handler is called when the device list is ready. The list is passed into the fulfillment handler as an array of {{domxref("MediaDeviceInfo")}} objects, each describing one media input or output device.

- -

A {{jsxref("Array.forEach", "forEach()")}} loop is used to scan through all the devices. For each device, we create a new {{HTMLElement("li")}} object to be used to display it to the user.

- -

The line let [kind, type, direction] = device.kind.match(/(\w+)(input|output)/i); deserves special notice. This uses destructuring assignment (a new feature of ECMAScript 6) to assign the values of the first three items in the array returned by {{jsxref("String.match()")}} to the variables kind, type, and direction. We do this because the value of {{domxref("MediaDeviceInfo.kind")}} is a single string that includes both the media type and the direction the media flows, such as "audioinput" or "videooutput". This line, then, pulls out the type ("audio" or "video") and direction ("input" or "output") so they can be used to construct the string displayed in the list.

- -

Once the string is assembled, containing the device's name in bold and the direction in parentheses, it's appended to the appropriate list by calling {{domxref("Node.appendChild", "appendChild()")}} on either audioList or videoList, as appropriate based on the device type.

- -

Handling device list changes

- -

We call updateDeviceList() in two places. The first is in the {{domxref("MediaDevices.getUserMedia", "getUserMedia()")}} promise's fulfillment handler, to initially fill out the list when the stream is opened. The second is in the event handler for {{event("devicechange")}}:

- -
navigator.mediaDevices.ondevicechange = function(event) {
-  updateDeviceList();
-}
- -

With this code in place, each time the user plugs in a camera, microphone, or other media device, or turns one on or off, we call updateDeviceList() to redraw the list of connected devices.

- -

Result

- -

{{ EmbedLiveSample('Example', 600, 460) }}

- -

Specifications

- - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{ SpecName('Media Capture', '#dom-mediadevices-ondevicechange', 'ondevicechange') }}{{ Spec2('Media Capture') }}Initial specification.
- -

Browser compatibility

- -{{Compat("api.MediaDevices.ondevicechange")}} - -

See also

- - diff --git a/files/zh-cn/web/api/window/beforeinstallprompt_event/index.html b/files/zh-cn/web/api/window/beforeinstallprompt_event/index.html new file mode 100644 index 0000000000..b9f089174d --- /dev/null +++ b/files/zh-cn/web/api/window/beforeinstallprompt_event/index.html @@ -0,0 +1,67 @@ +--- +title: Window.onbeforeinstallprompt +slug: Web/API/Window/beforeinstallprompt_event +tags: + - Window.onbeforeinstallprompt + - beforeinstallprompt +translation_of: Web/API/Window/onbeforeinstallprompt +original_slug: Web/API/Window/onbeforeinstallprompt +--- +

{{ ApiRef() }}

+ +

Window.onbeforeinstallprompt 属性是一个事件处理程序, 用于处理一个{{event("beforeinstallprompt")}}, 当一个Web清单存在时,它将在移动设备上发送,但是在提示用户将网站保存到主屏幕之前。

+ +

句法

+ +
window.addEventListener("beforeinstallprompt", function(event) { ... });
+
+window.onbeforeinstallprompt = function(event) { ...};
+ +

范例

+ +

The following example uses the beforeinstallprompt function to verify that it is an appropriate time to display an installation prompt to the user. If it is not, the event is redispatched.

+ +
let isTooSoon = true;
+window.addEventListener("beforeinstallprompt", function(e) {
+  if (isTooSoon) {
+    e.preventDefault(); // Prevents prompt display
+    // Prompt later instead:
+    setTimeout(function() {
+      isTooSoon = false;
+      e.prompt(); // Shows prompt
+    }, 10000);
+  }
+
+  // The event was re-dispatched in response to our request
+  // ...
+});
+ +

浏览器兼容性

+ +{{Compat("api.Window.onbeforeinstallprompt")}} + +

规范

+ + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Manifest', '#onbeforeinstallprompt-attribute', 'Window.onbeforeinstallprompt')}}{{Spec2('Manifest')}}Initial specification.
+ +

也可以看看

+ + diff --git a/files/zh-cn/web/api/window/onbeforeinstallprompt/index.html b/files/zh-cn/web/api/window/onbeforeinstallprompt/index.html deleted file mode 100644 index 9fe0053476..0000000000 --- a/files/zh-cn/web/api/window/onbeforeinstallprompt/index.html +++ /dev/null @@ -1,66 +0,0 @@ ---- -title: Window.onbeforeinstallprompt -slug: Web/API/Window/onbeforeinstallprompt -tags: - - Window.onbeforeinstallprompt - - beforeinstallprompt -translation_of: Web/API/Window/onbeforeinstallprompt ---- -

{{ ApiRef() }}

- -

Window.onbeforeinstallprompt 属性是一个事件处理程序, 用于处理一个{{event("beforeinstallprompt")}}, 当一个Web清单存在时,它将在移动设备上发送,但是在提示用户将网站保存到主屏幕之前。

- -

句法

- -
window.addEventListener("beforeinstallprompt", function(event) { ... });
-
-window.onbeforeinstallprompt = function(event) { ...};
- -

范例

- -

The following example uses the beforeinstallprompt function to verify that it is an appropriate time to display an installation prompt to the user. If it is not, the event is redispatched.

- -
let isTooSoon = true;
-window.addEventListener("beforeinstallprompt", function(e) {
-  if (isTooSoon) {
-    e.preventDefault(); // Prevents prompt display
-    // Prompt later instead:
-    setTimeout(function() {
-      isTooSoon = false;
-      e.prompt(); // Shows prompt
-    }, 10000);
-  }
-
-  // The event was re-dispatched in response to our request
-  // ...
-});
- -

浏览器兼容性

- -{{Compat("api.Window.onbeforeinstallprompt")}} - -

规范

- - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('Manifest', '#onbeforeinstallprompt-attribute', 'Window.onbeforeinstallprompt')}}{{Spec2('Manifest')}}Initial specification.
- -

也可以看看

- - -- cgit v1.2.3-54-g00ecf