Reporting API
The ReportingObserver() constructor of the Reporting API creates a new ReportingObserver object instance, which can be used to collect and access reports.
Syntax
new ReportingObserver(callback)
new ReportingObserver(callback, options)
Parameters
-
callback- : A callback function that runs when the observer starts to collect reports (i.e., via ReportingObserver.observe()).
The callback function is given two parameters:
-
reports-
: A sequence of objects representing the reports collected in the observer's report queue.
Report objects are expected to have the following properties:
body- : An object representing the body of the report.
The structure of the report (in particular of its body), depends on its
type.
- : An object representing the body of the report.
The structure of the report (in particular of its body), depends on its
type- : A string indicating the type of the report.
For information on report types see
options.typesbelow.
- : A string indicating the type of the report.
For information on report types see
url- : A string representing the URL of the document that generated the report.
-
-
observer- : A reference to the same
ReportingObserverobject, allowing for recursive report collection, and so on.
- : A reference to the same
-
- : A callback function that runs when the observer starts to collect reports (i.e., via ReportingObserver.observe()).
The callback function is given two parameters:
-
options- : An object allowing you to set the options for creating the object.
The available options are:
-
types-
: An array of strings representing the types of report to be collected by this observer. Available types include:
coep- : Violations of the site's Cross-Origin-Embedder-Policy (COEP). Reports are COEPViolationReport instances.
crash- : Browser crash reports.
Reports are CrashReport instances. Note that crash reports aren't retrievable via a
ReportingObserverbut can be sent to a server.
- : Browser crash reports.
Reports are CrashReport instances. Note that crash reports aren't retrievable via a
csp-violation- : Violations of the site's CSP policy. Reports are CSPViolationReport instances.
deprecation- : Deprecated features used by the site. Reports are DeprecationReport instances.
integrity-violation- : Violations of the page's integrity policy. Reports are IntegrityViolationReport instances.
intervention- : Features blocked by the user agent, for example, if an ad significantly impacts page performance. Reports are InterventionReport instances.
permissions-policy-violation- : Violations of the site's Permissions-Policy. Reports are PermissionsPolicyViolationReport instances.
If this option is omitted, all supported types are collected.
-
-
buffered- : A boolean that defines whether the reports that were generated before the observer was able to be created should be observable (
true) or not (false).
- : A boolean that defines whether the reports that were generated before the observer was able to be created should be observable (
-
- : An object allowing you to set the options for creating the object.
The available options are:
Examples
Display specific report types
This code shows how to create a ReportingObserver that could be used to observe deprecation and integrity-violation reports.
const options = {
types: ["deprecation", "integrity-violation"],
buffered: true,
};
const observer = new ReportingObserver((reports, observer) => {
reportBtn.onclick = () => displayReports(reports);
}, options);
Specifications
Browser compatibility
See also
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
