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 |
瀏覽器相容性
載入中…