Wait for new tab to complete loading before interacting with it

Hey there,

I’m looking to create a new tab, then interact with the page once it’s completed loading. Currently I’m doing something like:

browser.tabs.create({url: url}).then( (newTabInfo) =>
  @interactWithPage(newTabInfo)    

but I got the error message in the debugger of “Error: Could not establish connection. Receiving end does not exist.”

I thought I needed to do something like create a listener to wait for the new tab to have the status of ‘complete’, but when I added that check inline it still passed:

browser.tabs.create({url: url}).then( (newTabInfo) =>
  if newTabInfo.status == "complete"
    @interactWithPage(newTabInfo)
  else 
    console.log "page not loaded yet"

My guess is that the complete refers to the tab being successfully loaded, but I’m interested in whether or not the content of the page is loaded. Is there a way within the API to make that check, is there another technique that I can use to wait, or am I missing something really obvious? (or all of the above.)

Thanks!

1 Like

There is an open bug about this issue.

In the last comment, i described the workaround i use.

1 Like

Thanks for the response @mig!

I don’t think this is the same issue since when I check newTabInfo.status, it is returning “complete”.

You mention in your response waiting for “document_idle”, is there an api endpoint where I can check for that myself? It seems like tab status is returning complete before the page is fully loaded on the other side.

I think I have this worked out, instead of checking for newTabInfo.status == “complete” inline, I instead used browser.runtime.sendMessage to send a message to my background.js to listen to the new tab via browser.tabs.onUpdated.addListener, then trigger my code when if changeInfo.status == "complete" and tabId == request.newTabId.

That update seems to trigger ~4 times before the tab is considered complete by the background.js code, so I’m not totally sure why my initial attempt had the status as complete right away.

Thanks!