alexbardas / nightmare-har-plugin

Nightmare plugin used to retrieve the network activity of a web page in HAR (HTTP Archive) format
MIT License
18 stars 3 forks source link

window.chrome.devtools.network.onRequestFinished.addListener not firing on windows #5

Open cheapsteak opened 7 years ago

cheapsteak commented 7 years ago
const REQUEST_FINISHED_URL = 'chrome://version?get-har'

let send = (channel, msg) => {
  window.chrome.devtools.inspectedWindow.eval(
    `__nightmare.ipc.send("${channel}", ${JSON.stringify(msg)})`
  )
}

let sendHAR = () => {
  window.chrome.devtools.network.getHAR((har) => {
    console.log('sendHAR');
    har.entries = har.entries.filter((entry) => entry.request.url !== REQUEST_FINISHED_URL)
    send('har-data', har)
  })
}

+setTimeout(() => {
+  window.chrome.devtools.inspectedWindow.eval(
+    `console.log('har')`
+  )
+}, 5000);

window.chrome.devtools.network.onRequestFinished.addListener((request) => {
+  window.chrome.devtools.inspectedWindow.eval(
+    `console.log('har har har', ${request.request.url});`
+  )
  if (request.request.url === REQUEST_FINISHED_URL) {

    sendHAR()
  }
})

I can see 'har' twice but never see 'har har har' on windows

cheapsteak commented 7 years ago

This fixes it:

+setTimeout(() => {
  window.chrome.devtools.network.onRequestFinished.addListener((request) => {
    if (request.request.url === REQUEST_FINISHED_URL) {
      sendHAR()
    }
  })
+}, 1000);

I guess the devtools network wasn't ready to be listened to