Browser agnostic C++ DOM interface

2019-03-29 03:58发布

When programming in C++ against the browser's DOM each engine has a different set of interfaces, IE has the COM based [MSHTML](http://msdn.microsoft.com/en-us/library/aa752279(VS.85).aspx), Mozilla has the XPCOM based Gecko DOM etc.

Is there a common API that has adapters for major browsers (and versions)?


As a clarification, the application in question is a desktop application written in C++ which interacts with browsers, currently we have separate code bases for support of IE and Mozilla and I'm trying to reduce duplications of logic and allow adding new browsers with less effort.

A concrete example can be getting the innerHTML property of an HTML element.

// Firefox
nsAutoString html;
nsCOMPtr<nsIDOMNSHTMLElement> elem = do_QueryInterface(obj);
if (elem)
    elem->GetInnerHTML(html); 

// IE
CComBSTR html;
MSHTML::IHTMLElementPtr elem = obj;
if (elem) 
     elem->get_innerHTML(&html);

4条回答
孤傲高冷的网名
2楼-- · 2019-03-29 04:35

Your best bet seems to be to define your own interface and write different adapters. In the likeliness that your C++ code won't be employing all DOM traversing capabilities, your interface will only have to define a compact set of traversal functions.

As a bonus of defining your own interface, you can easily write a mock adapter for your unit testing.

查看更多
Evening l夕情丶
3楼-- · 2019-03-29 04:37

Moonlight is released under LGPL, they may have something usable for you, if the licensing is ok.

查看更多
Juvenile、少年°
4楼-- · 2019-03-29 04:54

Any reason why it has to be C++? Can't you use jQuery? Or something like http://webkit.org/blog/156/queryselector-and-queryselectorall/ in WebKit might do the trick..

查看更多
Emotional °昔
5楼-- · 2019-03-29 04:55

I've never seen one. For my software, I simple wrote the extension in Firefox's javascript, and then created an Internet Explorer BHO (which I haven't finished yet).

查看更多
登录 后发表回答