Skip to content
Jan Odvarko edited this page Dec 1, 2015 · 11 revisions

HAR is not defined

Since HAR object is exposed into the page content asynchronously it isn't available from the very beginning, so if you script is executing before HAR is available you might get HAR is not defined error.

Note that the HAR object is exposed to a browser tab just once - after the tab is loaded for the first time. When the tab content (i.e. the web-page) is reloaded or navigated to different location, the HAR object stays there all the time.

So, there are two solutions for this problem

  • Make sure the tab is reloaded at least once.
  • Check existence of the HAR object before using it. See the script below.
function triggerExport() {
  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);
  });
};

// If HAR isn't yet defined, wait for the `har-api-ready` event.
// Otherwise trigger the export right away.
if (typeof HAR === 'undefined') {
  addEventListener('har-api-ready', triggerExport, false);
} else {
  triggerExport();
};
Clone this wiki locally