-
Notifications
You must be signed in to change notification settings - Fork 13
Examples
Take a look at the following example scripts showing how to use HAR API.
Do you have an issue with any of the following examples? Please file a report.
All examples bellow assume that the security token in your Firefox profile is set to test
. This token needs to be set in extensions.netmonitor.har.contentAPIToken
preference.
See more details about the extension setup.
This script shows how to trigger HAR export and get JSON data back (no local file created).
var options = {
token: "test", // Value of the token in your preferences
getData: true, // True if you want to get HAR data as a string
};
HAR.triggerExport(options).then(result => {
console.log(result.data);
});
See how to get HARP file back from the collected data.
var options = {
token: "test",
getData: true,
jsonp: true // Get data as JSONP/HARP
};
HAR.triggerExport(options).then(result => {
console.log(result.data);
});
It's possible to clear collected data at any time and export only new HTTP activity.
HAR.clear({token: "test"});
This example shows how to trigger HAR export and save the log automatically in a local *.har
file. The default target directory is <firefox-profile-dir>/har/logs
. You can provide different directory path in devtools.netmonitor.har.defaultLogDir
preference.
File name can use format string to have better control over the formatting of the generated file name.
var options = {
token: "test",
fileName: "my test har file %Y, %H:%M:%S" // Name of the file
};
HAR.triggerExport(options).then(result => {
// The local file is available now, result.data is null since options.getData wasn't set.
});
HAR API are injected into a browser tab asynchronously, so when the tab is opened it might take some time to have the API available. But, they are injected just once for the entire life time of the tab, no matter how many times is its content reloaded or navigated to different URL.
In any case, there is an event fired on the content window
object when the API injection is done.
addEventListener("har-api-ready", event => {
console.log("har api ready", event);
}, false);
Experimental feature (see also issue #10)
The extension is also firing new event on the content window
object that can be used to detect that the page has been fully loaded. This event complements existing DOMContentLoaded
and load
events and is fired when the following conditions are true:
- Both
DOMContentLoaded
andload
events has been fired - There is no pending HTTP request
- No new HTTP request started in given period of time (1500ms by default, see also
devtools.netmonitor.har.pageLoadedTimeout
preference)
The same logic is used for auto-exporting HAR into a file when devtools.netmonitor.har.enableAutoExportToFile
preference is set to true
.
Example script:
addEventListener("har-page-ready", event => {
var options = {
token: "test",
getData: true,
};
HAR.triggerExport(options).then(result => {
console.log(result.data);
});
});