如何从HAR对象计算Chrome DevTools“网络”标签中显示的“完成”时间?

时间:2019-03-07 23:07:55

标签: google-chrome browser google-chrome-extension google-chrome-devtools har

我正在创建Chrome扩展程序。我从DevTools面板检索了HAR对象。我可以从HAR对象获取“加载”和“ DOMContentLoaded”时间,

pages: [
  {
    "startedDateTime": "2019-03-07T22:16:02.333Z",
    "id": "page_2",
    "title": "https://xxxx",
    "pageTimings": {
      "onContentLoad": 1635.4740000006132,
      "onLoad": 2318.5020000000804
    }
  }
]

现在,如何从HAR对象计算7.75 s的“完成”时间,如下所示?

enter image description here

1 个答案:

答案 0 :(得分:0)

所以,我已经弄清楚了,

FinishTime =(lastRequestStartedTime + lastRequestDuration )-(firstRequestStartedTime)

const requests=harObject.entries;

const lastRequest = requests.reduce(
    (a, b) => {
      return a.startedDateTime > b.startedDateTime ? a : b;
    }
  );

const firstRequest = requests.reduce(
    (a, b) => {
      return a.startedDateTime < b.startedDateTime ? a : b;
    }
  );

//Finish Time in Seconds
const finishTime=`((Date.parse(lastRequest.startedDateTime) + lastRequest.time
                  - Date.parse(firstRequest.startedDateTime))
                  / 1000).toFixed(2)