How to get all frameId's in a tab?

4.5k views Asked by At

Is there a way to use chrome.tabs.executeScript to get all frameId's in a tab?

I tried executeScript() then have the code send a message to background which would send it back to the content with the frameId, however it's async. And, executeScript() needs sync return.

1

There are 1 answers

1
Makyen On BEST ANSWER

Yes, you can get a list of frameId's for a tab using chrome.tabs.executeScript() (MDN).As it sounds like you have determined, it is a bit convoluted to do so. You could use chrome.tabs.executeScript() with the option allFrames:true to inject a content script into all frames in the tab. Your content script would then need to send a message back to the background context using chrome.runtime.sendMessage() (MDN). The frameId will then be available for each frame as a property of the Object passed as the second argument, the sender (MDN), to your chrome.runtime.onMessage (MDN) listener. You will need to accumulate the list yourself.

A much better way: use chrome.webNavigation.getAllFrames() (MDN)

As wOxxOm mentioned in a comment, It would be much easier to get it from chrome.webNavigation.getAllFrames() (MDN).

There is no way to obtain a list of frameId's for a tab using a synchronous interface. As wOxxOm also mentioned, the only way to do it is to obtain the information prior to you needing it. If you need it for a call to chrome.tabs.executeScript(), then you can make the call to chrome.tabs.executeScript() from the callback for chrome.webNavigation.getAllFrames().