ReportingObserver

这是 实验性技术
检查 浏览器兼容性表格 在生产中使用这之前。

ReportingObserver interface of the Reporting API allows you to collect and access reports.

构造函数

ReportingObserver()
创建新的 ReportingObserver object instance, which can be used to collect and access reports.

特性

This interface has no properties defined on it.

方法

ReportingObserver.disconnect()
Stops a reporting observer that had previously started observing from collecting reports.
ReportingObserver.observe()
Instructs a reporting observer to start collecting reports in its report queue.
ReportingObserver.takeRecords()
Returns the current list of reports contained in the observer's report queue, and empties the queue.

Events

This interface has no events that fire on it.

范例

In our deprecation_report.html example, we create a simple reporting observer to observe usage of deprecated features on our web page:

let options = {
  types: ['deprecation'],
  buffered: true
}
let observer = new ReportingObserver(function(reports, observer) {
  reportBtn.onclick = () => displayReports(reports);
}, options);
					

We then tell it to start observing reports using ReportingObserver.observe() ; this tells the observer to start collecting reports in its report queue, and runs the callback function specified inside the constructor:

observer.observe();
					

Later on in the example we deliberately use the deprecated version of MediaDevices.getUserMedia() :

if(navigator.mozGetUserMedia) {
  navigator.mozGetUserMedia(
    constraints,
    success,
    failure);
} else {
  navigator.getUserMedia(
    constraints,
    success,
    failure);
}
					

This causes a deprecation report to be generated; because of the event handler we set up inside the ReportingObserver() constructor, we can now click the button to display the report details.

image of a jolly bearded man with various stats displayed below it about a deprecated feature

注意 : If you look at the complete source code , you'll notice that we actually call the deprecated getUserMedia() method twice. After the first time we call ReportingObserver.takeRecords() , which returns the first generated report and empties the queue. Because of this, when the button is pressed only the second report is listed.

规范

规范 状态 Comment
Reporting API
The definition of 'ReportingObserver' in that specification.
编者草案 初始定义。

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
ReportingObserver
Chrome 69 Edge 79 Firefox No IE No Opera 56 Safari No WebView Android 69 Chrome Android 69 Firefox Android No Opera Android 48 Safari iOS No Samsung Internet Android 10.0
ReportingObserver() 构造函数
Chrome 69 Edge 79 Firefox No IE No Opera 56 Safari No WebView Android 69 Chrome Android 69 Firefox Android No Opera Android 48 Safari iOS No Samsung Internet Android 10.0
disconnect
Chrome 69 Edge 79 Firefox No IE No Opera 56 Safari No WebView Android 69 Chrome Android 69 Firefox Android No Opera Android 48 Safari iOS No Samsung Internet Android 10.0
observe
Chrome 69 Edge 79 Firefox No IE No Opera 56 Safari No WebView Android 69 Chrome Android 69 Firefox Android No Opera Android 48 Safari iOS No Samsung Internet Android 10.0
takeRecords
Chrome 69 Edge 79 Firefox No IE No Opera 56 Safari No WebView Android 69 Chrome Android 69 Firefox Android No Opera Android 48 Safari iOS No Samsung Internet Android 10.0
Available in workers
Chrome 84 Edge 84 Firefox No IE No Opera 70 Safari No WebView Android 84 Chrome Android 84 Firefox Android No Opera Android No Safari iOS No Samsung Internet Android No

图例

完整支持
完整支持
不支持
不支持
实验。期望将来行为有所改变。
实验。期望将来行为有所改变。

另请参阅