有时你可能想在C++代码中直接操作HTML中的某个元素,比如改变某个按钮的状态(文字、颜色)等,此时可以使用CEF提供的CefDomVisitor、CefDOMDocument、CefDomNode这三个类,包含cef_dom.h即可。
我们可以用它们完成下列任务:
- 使用DOM模型访问HTML的各种节点(Element、Attribute、Text、CDATA、Comment、Document等)
- 修改某个元素的属性
- 修改某个Text节点的值
下面简要说说各个类的用法。
CefDOMDocument
CefDOMDocument对应JS里的document,不过功能少一些,类声明如下:
class CefDOMDocument : public virtual CefBase {
public:
typedef cef_dom_document_type_t Type;
///
// Returns the document type.
///
/*--cef(default_retval=DOM_DOCUMENT_TYPE_UNKNOWN)--*/
virtual Type GetType() =0;
///
// Returns the root document node.
///
/*--cef()--*/
virtual CefRefPtr<CefDOMNode> GetDocument() =0;
///
// Returns the BODY node of an HTML document.
///
/*--cef()--*/
virtual CefRefPtr<CefDOMNode> GetBody() =0;
///
// Returns the HEAD node of an HTML document.
///
/*--cef()--*/
virtual CefRefPtr<CefDOMNode> GetHead() =0;
///
// Returns the title of an HTML document.
///
/*--cef()--*/
virtual CefString GetTitle() =0;
///
// Returns the document element with the specified ID value.
///
/*--cef()--*/
virtual CefRefPtr<CefDOMNode> GetElementById(const CefString& id) =0;
///
// Returns the node that currently has keyboard focus.
///
/*--cef()--*/
virtual CefRefPtr<CefDOMNode> GetFocusedNode() =0;
///
// Returns true if a portion of the document is selected.
///
/*--cef()--*/
virtual bool HasSelection() =0;
///
// Returns the selection offset within the start node.
///
/*--cef()--*/
virtual int GetSelectionStartOffset() =0;
///
// Returns the selection offset within the end node.
///
/*--cef()--*/
virtual int GetSelectionEndOffset() =0;
///
// Returns the contents of this selection as markup.
///
/*--cef()--*/
virtual CefString GetSelectionAsMarkup() =0;
///
// Returns the contents of this selection as text.
///
/*--cef()--*/
virtual CefString GetSelectionAsText() =0;
///
// Returns the base URL for the document.
///
/*--cef()--*/
virtual CefString GetBaseURL() =0;
///
// Returns a complete URL based on the document base URL and the specified
// partial URL.
///
/*--cef()--*/
virtual CefString GetCompleteURL(const CefString& partialURL) =0;
};
如你所见,它能获取一些字符串值(URL、标题等),能根据id查找某个元素(在JS里我们最常用的方式),能返回Do