Skip to content

Add API to get WebFrame for all frame/iframes #5115

Closed

Description

@Mr0grog

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.

Activity

  1. zcbenz commented on Apr 12, 2016

    @zcbenz
    Contributor

    Yeah it is possible with the API you mentioned. Nightmare's usage looks reasonable to me so I'm good adding this.

  2. 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
  3. kalehrishi commented on Aug 10, 2016

    @kalehrishi

    Is anyone working on this feature?

  4. Mr0grog commented on Aug 10, 2016

    @Mr0grog
    ContributorAuthor

    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?

  5. kalehrishi commented on Aug 11, 2016

    @kalehrishi

    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.

    Is there somehow I could help?

  6. Mr0grog commented on Aug 11, 2016

    @Mr0grog
    ContributorAuthor

    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!

  7. kalehrishi commented on Aug 11, 2016

    @kalehrishi

    I am talking about all_frames flag used in chrome extension to inject content script in iframes. https://developer.chrome.com/extensions/content_scripts

    I am trying to implement same in my Electron app. I have 3 options:

    1. Use chrome extension which can inject script in iframes on page (This doesn't work with chrome)
    2. There should be some API which will allow to execute script in iframes present in page loaded in WebView tag/BrowserWindow
    3. Disable web-security so that executeScript could execute script in main window which could access elements within iframe

    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.

  8. Mr0grog commented on Aug 11, 2016

    @Mr0grog
    ContributorAuthor

    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().

    Under the hood, Electron’s atom::api::WebFrame wraps Blink’s blink::WebLocalFrame (it’s stored in the web_frame_ instance variable).

    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 this
      if (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, new WebFrame(isolate, element_frame));
          return frame_handle.ToV8();
        }
      }
      args->ThrowError("Must pass a frame element instance");
      return v8::Null(isolate());
    }

    Hope that helps!

  9. bughit commented on Jan 5, 2018

    @bughit
    Contributor

    @Mr0grog

    // Note you'll probably need to implement the converter for this

    Could you please give some guidance for getting a blink::WebElement from a v8::Value?

  10. bughit commented on Jan 6, 2018

    @bughit
    Contributor

    according to V8Element.h the following should work

    blink::Element* frame_element = blink::ToScriptWrappable(v8::Local<v8::Object>::Cast(value))->ToImpl<blink::Element>();

    however it has these problems:

    • 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?

  11. Mr0grog commented on Jan 6, 2018

    @Mr0grog
    ContributorAuthor

    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.)

  12. bughit commented on Jan 8, 2018

    @bughit
    Contributor

    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)?

  13. bughit commented on Jan 9, 2018

    @bughit
    Contributor

    Yes and if atom::api::WebFrame needed a WebFrame and the methods returned a WebLocalFrame, it would have been fine, but it's the other way around.

  14. Mr0grog commented on Jan 9, 2018

    @Mr0grog
    ContributorAuthor

    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);
  15. bughit commented on Jan 9, 2018

    @bughit
    Contributor

    You could always see if what it returns are actually WebLocalFrame instances most of the time

    RTTI is disabled by default, however empirically a blind cast does seem to work. The following works and the frame can execute javascript:

    v8::Local<v8::Value> WebFrame::FirstChild() const {
      blink::WebFrame* _first_child = WebFrame::web_frame_->FirstChild();
      blink::WebLocalFrame* first_child = (blink::WebLocalFrame*) _first_child;
      if (first_child)
        return mate::CreateHandle(isolate(), new WebFrame(isolate(), first_child)).ToV8();
      else
        return v8::Null(isolate());
    }

    Is there a guarantee that these will always be local? Then what's the remote frame for?

    QuerySelector also works, the string conversion is pretty cumbersome here, is there a better way?

    v8::Local<v8::Value> WebFrame::getFrameForSelector(v8::Local<v8::Value> selector_value, mate::Arguments* args) const {
      if (!selector_value->IsString()) {
        args->ThrowError("selector string required");
        return v8::Null(isolate());
      }
      v8::Local<v8::String> selector_v8_string = selector_value->ToString();
      int selector_utf8_size = selector_v8_string->Utf8Length();
      char* selector_utf8_buff = new char[selector_utf8_size];
      selector_v8_string->WriteUtf8(selector_utf8_buff, selector_utf8_size);
      blink::WebString selector_web_string = blink::WebString::FromUTF8(selector_utf8_buff, selector_utf8_size);
      delete[] selector_utf8_buff;
      blink::WebElement element = web_frame_->GetDocument().QuerySelector(selector_web_string);
      blink::WebLocalFrame* element_frame = blink::WebLocalFrame::FromFrameOwnerElement(element);
      if (element_frame)
        return mate::CreateHandle(isolate(), new WebFrame(isolate(), element_frame)).ToV8();
      else
        return v8::Null(isolate());
    }
  16. bughit commented on Jan 9, 2018

    @bughit
    Contributor

    #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

  17. bughit commented on Mar 1, 2018

    @bughit
    Contributor

    #11607 has been merged, didn't make 2.0. This can probably be closed.

  18. Mr0grog commented on Mar 2, 2018

    @Mr0grog
    ContributorAuthor

    Oh, nice! Thanks for taking this over and pushing it through that slow review process, @bughit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions