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
{{ message }}
Repository navigation
Add API to get WebFrame for all frame/iframes #5115
Is there any way to get an instance of WebFrame representing a particular iframe (or actual frame) inside a window’s main frame? It doesn’t look like there is (at least, it’s non-obvious to me if it is).
I’m not sure there are a lot of use cases for this kind of functionality, but there is some discussion over on the Nightmare repository (https://github.com/segmentio/nightmare/pull/496) about scripting content in iframes. As a browser automation/testing tool, this seems like a great thing for Nightmare to be able to have. However, one of the caveats with current approaches is the need to turn off a lot of security features to script across frames from different domains. I think the only meaningful way around this would be to obtain a webFrame instance for a given frame, then call methods (e.g. executeJavaScript) on that.
It seems like this should be possible (blink::WebLocalFrame, which Electron’s WebFrame wraps, has a static fromFrameOwnerElement() method. blink::WebFrame has a series of traversal methods as well.) Maybe it would be an API on WebFrame instances, that way security issues are probably avoided (you can only call it if you have node support). Nightmare could probably benefit, but I don’t know how valuable this would be in general.
changed the title [-]Is it possible to get a WebFrame instance for a frame/iframe inside the current page?[/-][+]Add API to get WebFrame for all frame/iframes[/+]on Aug 9, 2016
We are trying to include an extension which injects script in all frames. Unfortunately all_frames:true doesn't work with Electron. It would be really great to have some API to executeScript with frames without disabling webSecurity. For now we are using this but I would really like to have a secure way to do it.
Unfortunately all_frames:true doesn't work with Electron.
Are you referring to the first argument to WebContents::SendIPCMessage or something else? I’m not sure I exactly get what you’re going for here, though I think I get the general idea.
Is there somehow I could help?
Well, you could write a patch that does what this issue is suggesting ;) Otherwise, I certainly don’t think I’m going to be able to get around to this myself anytime soon. I certainly won’t claim any monopoly on working on this issue, though. Anyone can take this if they want!
Hmmm, I don't think I had thought too deeply on exactly what the API should be, but my general idea was to expose a method on WebFrame that takes an element and returns a WebFrame instance for it. e.g. WebFrame#getFrameForElement().
WebLocalFrame has a static fromFrameOwnerElement() method that returns the WebLocalFrame instance corresponding to an element. So you’d need to call that and then create a new atom::api::WebFrame to wrap the result. So you might have a method sorta-kinda like:
v8::Local<v8::Value> WebFrame::GetFrameForElement(v8::Local<v8::Value> value
mate::Arguments* args) {
mate::Handle<WebElement> element;
// Note you'll probably need to implement the converter for thisif (mate::ConvertFromV8(isolate(), value, &element)) {
blink::WebLocalFrame* element_frame = WebLocalFrame::fromFrameOwnerElement(element);
if (element_frame != 0) {
// You'll need to add a way to construct a `WebFrame` with a given `WebLocalFrame`// (it always grabs the current context's frame right now)
mate::Handle<WebFrame> frame_handle = mate::CreateHandle(isolate, newWebFrame(isolate, element_frame));
return frame_handle.ToV8();
}
}
args->ThrowError("Must pass a frame element instance");
returnv8::Null(isolate());
}
ScriptWrappable.h includes ScriptWrappableVisitor.h which includes RuntimeEnabledFeatures.h which is missing.
blink::Element is declared in vendor/download/libchromiumcontent/src/third_party/WebKit/Source/core/dom/Element.h which is missing.
in order to construct a blink::WebElement from blink::Element, you need vendor/download/libchromiumcontent/src/third_party/WebKit/Source/core/exported/WebElement.cpp which is missing, and BLINK_IMPLEMENTATION=1
I can try
commenting out the RuntimeEnabledFeatures.h include
grabbing vendor/download/libchromiumcontent/src/third_party/WebKit/Source/core from chromium (how do you determine which commit to take?)
setting BLINK_IMPLEMENTATION=1
Am I on the right track here, or is there a better way to do this?
I’m sorry to say that you are much farther along on this than I ever had time to get to. @zcbenz would almost certainly know more about this than I — I foolishly didn’t expect this part would be that complicated!
A different approach that wouldn’t require dealing with WebElements might be to expose the traversal methods on WebFrame (Parent(), FirstChild(), NextSibling(), TraverseNext()) so users can simply walk the frame tree to find the WebFrame they want.
It’s not nearly as pretty or nice as exchanging an <iframe> element for a WebFrame instance, but it still solves a lot of the use cases. (It might be hard or impossible to determine which frame you want in a page with several frames all pointing to the same URL, but it lets you traverse deeply through the hierarchy easier and lets you get at frames inside shadow trees, which might be pretty hard with API I’d proposed above.)
A different approach that wouldn’t require dealing with WebElements might be to expose the traversal methods on WebFrame (Parent(), FirstChild(), NextSibling(), TraverseNext())
Those methods return blink::WebFrame*. Which I am assuming means they could be remote and can't be cast to blink::WebLocalFrame* (which atom::api::WebFrame needs)?
True, I wasn’t thinking about that. You could always see if what it returns are actually WebLocalFrame instances most of the time, and if so, then just do that check at runtime and cast if you get an instance or throw an error if not. Obviously if that’s not usually the case, then you don’t have a viable approach there.
Another ugly approach might be to take a string selector or ID instead of an element reference. Then you could get the element by:
WebElement* element = localFrame->GetDocument()->QuerySelector(frame_selector);
WebLocalFrame* element_frame = WebLocalFrame::fromFrameOwnerElement(element);
Activity
zcbenz commentedon Apr 12, 2016
Yeah it is possible with the API you mentioned. Nightmare's usage looks reasonable to me so I'm good adding this.
[-]Is it possible to get a WebFrame instance for a frame/iframe inside the current page?[/-][+]Add API to get WebFrame for all frame/iframes[/+]kalehrishi commentedon Aug 10, 2016
Is anyone working on this feature?
Mr0grog commentedon Aug 10, 2016
I have unfortunately not had time to work on this, so as far as I’m aware, no. Do you have another particular need/use case for it?
kalehrishi commentedon Aug 11, 2016
We are trying to include an extension which injects script in all frames. Unfortunately
all_frames:truedoesn't work with Electron. It would be really great to have some API to executeScript with frames without disablingwebSecurity. For now we are using this but I would really like to have a secure way to do it.Is there somehow I could help?
Mr0grog commentedon Aug 11, 2016
Are you referring to the first argument to WebContents::SendIPCMessage or something else? I’m not sure I exactly get what you’re going for here, though I think I get the general idea.
Well, you could write a patch that does what this issue is suggesting ;) Otherwise, I certainly don’t think I’m going to be able to get around to this myself anytime soon. I certainly won’t claim any monopoly on working on this issue, though. Anyone can take this if they want!
kalehrishi commentedon Aug 11, 2016
I am talking about
all_framesflag used in chrome extension to inject content script in iframes. https://developer.chrome.com/extensions/content_scriptsI am trying to implement same in my Electron app. I have 3 options:
Right now we are using option 3. But it's not right way to do it. So I was thinking if we could get this API developed then we can start using it.
Would you be able to give me some pointers about how we can implement it? I would like to help on this one.
Mr0grog commentedon Aug 11, 2016
Hmmm, I don't think I had thought too deeply on exactly what the API should be, but my general idea was to expose a method on
WebFramethat takes an element and returns aWebFrameinstance for it. e.g.WebFrame#getFrameForElement().Under the hood, Electron’s
atom::api::WebFramewraps Blink’sblink::WebLocalFrame(it’s stored in theweb_frame_instance variable).WebLocalFramehas a staticfromFrameOwnerElement()method that returns theWebLocalFrameinstance corresponding to an element. So you’d need to call that and then create a newatom::api::WebFrameto wrap the result. So you might have a method sorta-kinda like:Hope that helps!
bughit commentedon Jan 5, 2018
@Mr0grog
Could you please give some guidance for getting a blink::WebElement from a v8::Value?
bughit commentedon Jan 6, 2018
according to V8Element.h the following should work
however it has these problems:
blink::Elementis declared invendor/download/libchromiumcontent/src/third_party/WebKit/Source/core/dom/Element.hwhich is missing.blink::WebElementfromblink::Element, you needvendor/download/libchromiumcontent/src/third_party/WebKit/Source/core/exported/WebElement.cppwhich is missing, and BLINK_IMPLEMENTATION=1I can try
vendor/download/libchromiumcontent/src/third_party/WebKit/Source/corefrom chromium (how do you determine which commit to take?)Am I on the right track here, or is there a better way to do this?
Mr0grog commentedon Jan 6, 2018
I’m sorry to say that you are much farther along on this than I ever had time to get to. @zcbenz would almost certainly know more about this than I — I foolishly didn’t expect this part would be that complicated!
A different approach that wouldn’t require dealing with WebElements might be to expose the traversal methods on WebFrame (
Parent(),FirstChild(),NextSibling(),TraverseNext()) so users can simply walk the frame tree to find theWebFramethey want.It’s not nearly as pretty or nice as exchanging an
<iframe>element for aWebFrameinstance, but it still solves a lot of the use cases. (It might be hard or impossible to determine which frame you want in a page with several frames all pointing to the same URL, but it lets you traverse deeply through the hierarchy easier and lets you get at frames inside shadow trees, which might be pretty hard with API I’d proposed above.)bughit commentedon Jan 8, 2018
Those methods return
blink::WebFrame*. Which I am assuming means they could be remote and can't be cast toblink::WebLocalFrame*(whichatom::api::WebFrameneeds)?Mr0grog commentedon Jan 9, 2018
Isn’t
WebLocalFramea subclass ofWebFrame, though? https://chromium.googlesource.com/chromium/src/+/master/third_party/WebKit/public/web/WebLocalFrame.h#71bughit commentedon Jan 9, 2018
Yes and if
atom::api::WebFrameneeded aWebFrameand the methods returned aWebLocalFrame, it would have been fine, but it's the other way around.Mr0grog commentedon Jan 9, 2018
True, I wasn’t thinking about that. You could always see if what it returns are actually
WebLocalFrameinstances most of the time, and if so, then just do that check at runtime and cast if you get an instance or throw an error if not. Obviously if that’s not usually the case, then you don’t have a viable approach there.Another ugly approach might be to take a string selector or ID instead of an element reference. Then you could get the element by:
WebElement* element = localFrame->GetDocument()->QuerySelector(frame_selector); WebLocalFrame* element_frame = WebLocalFrame::fromFrameOwnerElement(element);bughit commentedon Jan 9, 2018
RTTI is disabled by default, however empirically a blind cast does seem to work. The following works and the frame can execute javascript:
Is there a guarantee that these will always be local? Then what's the remote frame for?
QuerySelectoralso works, the string conversion is pretty cumbersome here, is there a better way?bughit commentedon Jan 9, 2018
#11607
It probably won't be merged as I don't time to do tests or documentation, but it's worth to have the code reviewed
bughit commentedon Mar 1, 2018
#11607 has been merged, didn't make 2.0. This can probably be closed.
Mr0grog commentedon Mar 2, 2018
Oh, nice! Thanks for taking this over and pushing it through that slow review process, @bughit.