DeprecationReportBody
注意:此功能在 Web Workers 中可用。
Reporting API 中的 DeprecationReportBody 接口代表弃用报告的正文。
当被 ReportingObserver 观察的文档中使用了已弃用的功能(例如已弃用的 API 方法)时,会生成弃用报告。除了支持此 API 外,接收有用的弃用警告还依赖于浏览器供应商为已弃用的功能添加这些警告。
构造函数
当 Report.Type 为 deprecation 时,DeprecationReportBody 的实例将作为 Report.body 的值返回。该接口没有构造函数。
实例属性
此接口还继承了 ReportBody 的属性。
DeprecationReportBody.idExperimental-
一个字符串,表示已弃用的功能或 API,例如
NavigatorGetUserMedia。这可用于按已弃用的功能对报告进行分组。 DeprecationReportBody.anticipatedRemovalExperimental-
一个
Date对象(显示为字符串),表示该功能预计从当前浏览器中移除的日期。如果日期未知,此属性将返回null。 DeprecationReportBody.messageExperimental-
一个字符串,包含对弃用的可读描述,包括信息(例如,是否有较新的功能已取代它)。这通常与浏览器在其开发者工具控制台中显示已弃用功能使用时的消息相匹配(如果可用)。
DeprecationReportBody.sourceFileExperimental-
一个字符串,包含使用已弃用功能所在的源文件的路径(如果已知),否则为
null。 DeprecationReportBody.lineNumberExperimental-
一个数字,表示使用已弃用功能所在的源文件的行号(如果已知),否则为
null。 DeprecationReportBody.columnNumberExperimental-
一个数字,表示使用已弃用功能所在的源文件的列号(如果已知),否则为
null。
实例方法
此接口还继承了 ReportBody 的方法。
DeprecationReportBody.toJSON()Experimental-
一个序列化器,它返回
InterventionReportBody对象的 JSON 表示形式。
示例
在我们 deprecation_report.html 示例中,我们创建了一个简单的报告观察者来观察我们网页上已弃用功能的使用情况。
const options = {
types: ["deprecation"],
buffered: true,
};
const observer = new ReportingObserver((reports, observer) => {
reportBtn.onclick = () => displayReports(reports);
}, options);
然后,我们告诉它使用 ReportingObserver.observe() 开始观察报告;这告诉观察者开始在其报告队列中收集报告,并运行构造函数中指定的函数。
observer.observe();
由于我们在 ReportingObserver() 构造函数中设置了事件处理程序,现在我们可以单击按钮来显示报告详细信息。

报告详细信息通过 displayReports() 函数显示,该函数将观察者回调的 reports 参数作为其参数。
function displayReports(reports) {
const outputElem = document.querySelector(".output");
const list = document.createElement("ul");
outputElem.appendChild(list);
reports.forEach((report, i) => {
const listItem = document.createElement("li");
const textNode = document.createTextNode(
`Report ${i + 1}, type: ${report.type}`,
);
listItem.appendChild(textNode);
const innerList = document.createElement("ul");
listItem.appendChild(innerList);
list.appendChild(listItem);
for (const [key, value] of Object.entries(report.body)) {
const innerListItem = document.createElement("li");
innerListItem.textContent = `${key}: ${value}`;
innerList.appendChild(innerListItem);
}
});
}
reports 参数包含观察者报告队列中所有报告的数组。我们使用基本的 for 循环遍历每个报告,然后使用 for...in 结构遍历报告正文(DeprecationReportBody 实例)中的每个条目,在列表项中显示每个键/值对。
规范
| 规范 |
|---|
| 弃用报告 # deprecationreportbody |
浏览器兼容性
加载中…