Querying the DOM

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Querying the DOM

Postby emerick » Fri Dec 09, 2016 10:10 am

I often have a need to query the DOM and return some data. While CEF makes this doable, it's a lot of work. I was wondering if a convenience method like the following is technically feasible:

Code: Select all
 
///
// Query the DOM document.
///
/*--cef()--*/
virtual void QueryDOM(CefRefPtr<CefDOMVisitor> domVisitor, CefRefPtr<CefStringVisitor> strVisitor) =0;


This would run from the browser process. Implementation would look similar to GetText, where the visitors are wrapped in CefResponseManager::Handler and then the command is sent to the render process. The renderer process would call domVisitor->VisitDOM(strVisitor) and the implementation of VisitDOM would call strVisitor->Visit() with the contents to return.

Does that seem reasonable or have I missed something that would prevent this? If it's feasible, I may take a run at it if we think it's something that would be generally useful/desirable.
emerick
Expert
 
Posts: 154
Joined: Sun Feb 21, 2010 7:57 pm
Location: Belmont, MA

Re: Querying the DOM

Postby magreenblatt » Fri Dec 09, 2016 10:21 am

You can implement something like this in your client application. Keep in mind that the result may be out of date by the time it arrives back in the browser process.
magreenblatt
Site Admin
 
Posts: 12407
Joined: Fri May 29, 2009 6:57 pm

Re: Querying the DOM

Postby emerick » Fri Dec 09, 2016 1:47 pm

Right, good point about it potentially being out of date by the time it arrives back. Well, good to know that this is at least technically feasible - thanks Marshall!
emerick
Expert
 
Posts: 154
Joined: Sun Feb 21, 2010 7:57 pm
Location: Belmont, MA

Re: Querying the DOM

Postby tgraupmann » Thu Feb 16, 2017 5:37 pm

A code sample would be awesome...

Code: Select all
private sealed class SourceVisitor : CefStringVisitor
        {
            private readonly Action<string> _callback;

            public SourceVisitor(Action<string> callback)
            {
                _callback = callback;
            }

            protected override void Visit(string value)
            {
                _callback(value);
            }
        }


This works...

Code: Select all
browser.LoadingStateChanged += (s, e) =>
                {
                    CefBrowser b = ((Xilium.CefGlue.Demo.Browser.WebBrowser)s).CefBrowser;
                    string[] names = b.GetFrameNames();
                    foreach (string name in names)
                    {
                        CefFrame frame = b.GetFrame(name);
                        if (null != frame)
                        {
                            CefStringVisitor visitor = new SourceVisitor((text) =>
                            {
                                if (!string.IsNullOrEmpty(text))
                                {

                                }
                            });
                            frame.GetSource(visitor);
                        }
                    }
tgraupmann
Techie
 
Posts: 16
Joined: Tue Feb 14, 2017 12:51 pm


Return to Support Forum

Who is online

Users browsing this forum: Google [Bot] and 35 guests

cron