You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've needed to conveniently propagate event handlers to all iframes of the page automatically so I modified nodriver in my fork: xn435/nodriver.
I wanted to open a pull request or create a new discussion in the nodriver repository but only collaborators can do that, so I suppose this is the correct place to do that.
With my changes you can propagate event handlers by simply calling page.propagate_handlers() after adding handlers to the page. If you make changes to the event handlers on the page you will have to call page.propagate_handlers() again if you want those changes to apply to the iframes as well. You can also remove all handlers from the iframes by calling it with an empty dictionary like so: page.propagate_handlers({}).
Usage example
Note: modify the url to something valid which uses iframes.
Figuring out if a target is an iframe child of another target
After reading through chrome devtools protocol for days and trying out different methods to determine if a target is a child target of the page, I finally found a solution. You need to recurse the nodes of the page and check if a node with frame_id is equal to the target_id of the iframe. It works, but it seems strange to me that there is no obvious way to check this in the protocol directly. Maybe someone knows a better way to check if a target is a child target of your target?
On Event Handlers
From a design standpoint I think event handlers of the page should always be applied to all child iframes, as that makes the most sense to me. A function such as propagate_handlers ideally shouldn't be needed at all. In the event handler itself you could check if it's an iframe or not if you want to execute code specific to only the iframes or non-iframes.
However as this is probably a breaking change I implemented it in a way that wouldn't break existing code bases.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I've needed to conveniently propagate event handlers to all iframes of the page automatically so I modified nodriver in my fork: xn435/nodriver.
I wanted to open a pull request or create a new discussion in the nodriver repository but only collaborators can do that, so I suppose this is the correct place to do that.
With my changes you can propagate event handlers by simply calling
page.propagate_handlers()
after adding handlers to the page. If you make changes to the event handlers on the page you will have to callpage.propagate_handlers()
again if you want those changes to apply to the iframes as well. You can also remove all handlers from the iframes by calling it with an empty dictionary like so:page.propagate_handlers({})
.Usage example
Note: modify the url to something valid which uses iframes.
Implementation Details and Design Discussion
Figuring out if a target is an iframe child of another target
After reading through chrome devtools protocol for days and trying out different methods to determine if a target is a child target of the page, I finally found a solution. You need to recurse the nodes of the page and check if a node with
frame_id
is equal to thetarget_id
of the iframe. It works, but it seems strange to me that there is no obvious way to check this in the protocol directly. Maybe someone knows a better way to check if a target is a child target of your target?On Event Handlers
From a design standpoint I think event handlers of the page should always be applied to all child iframes, as that makes the most sense to me. A function such as
propagate_handlers
ideally shouldn't be needed at all. In the event handler itself you could check if it's an iframe or not if you want to execute code specific to only the iframes or non-iframes.However as this is probably a breaking change I implemented it in a way that wouldn't break existing code bases.
Beta Was this translation helpful? Give feedback.
All reactions