DeprecationReportBody

可用性有限

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

注意:此功能在 Web Workers 中可用。

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

Reporting API 中的 DeprecationReportBody 介面代表棄用報告的正文。

當被 ReportingObserver 觀察的文件中使用了已棄用的功能(例如已棄用的 API 方法)時,會生成棄用報告。除了支援此 API 外,接收有用的棄用警告還依賴於瀏覽器供應商為已棄用的功能新增這些警告。

建構函式

Report.Typedeprecation 時,DeprecationReportBody 的例項將作為 Report.body 的值返回。該介面沒有建構函式。

例項屬性

此介面還繼承了 ReportBody 的屬性。

DeprecationReportBody.id Experimental

一個字串,表示已棄用的功能或 API,例如 NavigatorGetUserMedia。這可用於按已棄用的功能對報告進行分組。

DeprecationReportBody.anticipatedRemoval Experimental

一個 Date 物件(顯示為字串),表示該功能預計從當前瀏覽器中移除的日期。如果日期未知,此屬性將返回 null

DeprecationReportBody.message Experimental

一個字串,包含對棄用的可讀描述,包括資訊(例如,是否有較新的功能已取代它)。這通常與瀏覽器在其開發者工具控制檯中顯示已棄用功能使用時的訊息相匹配(如果可用)。

DeprecationReportBody.sourceFile Experimental

一個字串,包含使用已棄用功能所在的原始檔的路徑(如果已知),否則為 null

DeprecationReportBody.lineNumber Experimental

一個數字,表示使用已棄用功能所在的原始檔的行號(如果已知),否則為 null

DeprecationReportBody.columnNumber Experimental

一個數字,表示使用已棄用功能所在的原始檔的列號(如果已知),否則為 null

例項方法

此介面還繼承了 ReportBody 的方法。

DeprecationReportBody.toJSON() Experimental

一個序列化器,它返回 InterventionReportBody 物件的 JSON 表示形式。

示例

在我們 deprecation_report.html 示例中,我們建立了一個簡單的報告觀察者來觀察我們網頁上已棄用功能的使用情況。

js
const options = {
  types: ["deprecation"],
  buffered: true,
};

const observer = new ReportingObserver((reports, observer) => {
  reportBtn.onclick = () => displayReports(reports);
}, options);

然後,我們告訴它使用 ReportingObserver.observe() 開始觀察報告;這告訴觀察者開始在其報告佇列中收集報告,並執行建構函式中指定的函式。

js
observer.observe();

由於我們在 ReportingObserver() 建構函式中設定了事件處理程式,現在我們可以單擊按鈕來顯示報告詳細資訊。

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

報告詳細資訊透過 displayReports() 函式顯示,該函式將觀察者回調的 reports 引數作為其引數。

js
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

瀏覽器相容性

另見