Using CDP's Runtime.Evaluate to execute scripts in iframes

765 views
Skip to first unread message

Lior Aviram

unread,
Mar 17, 2022, 3:21:13 PM3/17/22
to Chromium Extensions
Our extension needs to execute dynamic code in all of a tab's frames. We're trying to get the frames` context IDs via the `Runtime.executionContextCreated` so we can use `Runtime.Evaluate` but some contexts get destroyed ("swapped"). I'm guessing this is due to site isolation (out of process iframes) but it renders us with no context ID.
I've also tried using the CDP's "Target" domain (for example Target.getBrowserContexts) but these don't run against a certain target as chrome.debugger.sendCommand does (If I try to send them I get a "Not Allowed" error).
So how are we go about getting these context IDs (or uniqueContextIds) for a page's frames? Is there a way to run "Target" domain commands or receive "Target" events from an extension?

Thanks

wOxxOm

unread,
Mar 18, 2022, 10:21:15 AM3/18/22
to Chromium Extensions, li...@testim.io
See what the other extensions do. Maybe Target.setAutoAttach with waitForDebuggerOnStart?

Lior Aviram

unread,
Mar 27, 2022, 1:00:01 PM3/27/22
to Chromium Extensions, wOxxOm
I'm not sure how to even start looking for other extensions doing this. With that said, your comment aboutTarget.setAutoAttach did lead me down the right road (Thanks for that!).
The only problem left is to map the frame IDs received by chrome.webNavigation events (accepted by chrome.scripting.executeScript) to target IDs or frame ID's received with Runtime.executionContextCreated events so I can use them with Runtime.evaluate. Any idea how to do that?
I can send a chrome.scripting.executeScript script that set's a property on each frame with it's frame ID then read it with Runtime.evaluate and build a map but I rather do it in a more straightforward way, if one exists.
Thanks.

wOxxOm

unread,
Mar 28, 2022, 11:29:57 AM3/28/22
to Chromium Extensions, li...@testim.io, wOxxOm
Judging by the source code these ids are independent, so there should be no way to infer one from another.
Reply all
Reply to author
Forward
0 new messages