How set CefDOMNode's inner HTML?

Do not post support requests, bug reports or feature requests. Discuss CEF here. Non-CEF related discussion goes in General Discussion!

How set CefDOMNode's inner HTML?

Postby parxos » Wed Feb 08, 2012 12:20 pm

Hi, just registered to ask this question: How can I manipulate the DOM structure that the browser holds from the C++ part of my application?

In general, for the structure of my app, I don't want to load documents from external resources. Instead, I need to manipulate it based on changes inside the model of my app. Is that possible with CEF? I know it is possible with WebKit, because I have done it in Qt and on Apple WebKit. I see there is a method CefDOMNode::setValue(string) which I have tried but which does lead to any visual effects. It is used in the DOM Access test but only on text nodes, and if I add html tags to the string these are rendered as plain text not as structured text.

Ive tried the following on a <div id="struct"> inside the loaded document:

Code: Select all
        CefRefPtr<CefDOMNode> structure = document->GetElementById("struct");
        ASSERT(structure.get());
        structure->SetValue("AAA <span style=\"color:red\">BBB</span> CCC");


doesn't work (has no effect). Anything else that I miss?

How can I add DOM nodes programmatically? I've seen only getter methods so far. Is this by design or have the modifiers just not added because you think there is no need for such?

Thanks!
parxos
Newbie
 
Posts: 3
Joined: Wed Feb 08, 2012 12:09 pm

Re: How set CefDOMNode's inner HTML?

Postby magreenblatt » Wed Feb 08, 2012 5:48 pm

In most cases you should use JavaScript to manipulate the DOM. You can do this using the CefFrame::ExecuteJavaScript() method.

You can also load self-generated content using custom scheme handlers or CefFrame::LoadString().
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: How set CefDOMNode's inner HTML?

Postby parxos » Wed Feb 08, 2012 6:07 pm

Why should I use JavaScript if I want to add to the DOM structure? Do you say this because it is the only option with CEF or because you consider it best-practice in general?

I have written a long time ago about hacking into WebKit (via QtWebKit) see http://www.pareis.com/2010/03/23/accessing-the-original-webkit-api-in-qtwebkit-hybrid-apps/. In QWebElement it was easily possible to set the inner HTML. I even don't like the unnecessary indirection via Qt API but at least it was possible without JavaScript.

Does Chromium expose a similar API (allowing to set the inner HTML on a WebElement) or not? If not, then we have an explanation why it is not possible in CEF.

Thanks for further clarification.
parxos
Newbie
 
Posts: 3
Joined: Wed Feb 08, 2012 12:09 pm

Re: How set CefDOMNode's inner HTML?

Postby magreenblatt » Wed Feb 08, 2012 6:15 pm

parxos wrote:Does Chromium expose a similar API (allowing to set the inner HTML on a WebElement) or not? If not, then we have an explanation why it is not possible in CEF.

The DOM API exposed by Chromium is read-only with the exception of adding listeners. CEF exposes this same API via the CefFrame::VisitDOM method. If you want to manipulate the DOM directly via C++ it will be necessary to use WebKit interfaces directly.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: How set CefDOMNode's inner HTML?

Postby parxos » Wed Feb 08, 2012 6:30 pm

magreenblatt wrote:The DOM API exposed by Chromium is read-only with the exception of adding listeners. CEF exposes this same API via the CefFrame::VisitDOM method. If you want to manipulate the DOM directly via C++ it will be necessary to use WebKit interfaces directly.


Thanks for confirming this restriction. I wonder why there is a method CefDOMNode::SetValue() if the DOM is read-only ;) I can only guess it's because Google puts safety first.

Anyhow, would it be possible to access WebCore inside a CEF application (like in the link from my last post)? Is the WebCore API preserved and how do I get the "initial pointer" into it? I would need WebCore::WebElement.

Thanks
parxos
Newbie
 
Posts: 3
Joined: Wed Feb 08, 2012 12:09 pm

Re: How set CefDOMNode's inner HTML?

Postby magreenblatt » Wed Feb 08, 2012 6:40 pm

I believe the SetValue method can be used for input elements like text fields. Accessing WebKit objects is not supported but you can look at the existing CEF code as a guide for making your own local changes.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: How set CefDOMNode's inner HTML?

Postby emerick » Wed Feb 08, 2012 8:13 pm

parxos wrote:Anyhow, would it be possible to access WebCore inside a CEF application (like in the link from my last post)? Is the WebCore API preserved and how do I get the "initial pointer" into it? I would need WebCore::WebElement.


Not within a CEF application, but you can modify CEF to support your required functions. dom_inode_impl.h in CEF has a private WebKit::WebNode member named node_ that you can use for this purpose. If you need to cast it to a WebElement, you can do so like this: node_.to<WebKit::WebElement>().

Emerick
emerick
Expert
 
Posts: 154
Joined: Sun Feb 21, 2010 7:57 pm
Location: Belmont, MA


Return to CEF Discussion

Who is online

Users browsing this forum: No registered users and 183 guests