Browser agnostic C++ DOM interface - c++

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

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.

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

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

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

Related

C++: Alternative to the CHtmlVIew to show PDF/HTML files in MFC based application

Are the any alternatives how to display PDF/HTML file in the MFC based application without using ActiveX elements like CHtmlView?
Thanks in advance.
The alternative would be WebView2. It uses the Microsoft Edge (Chromium) browser engine, unlike CHtmlView that's based on IE.
If the reason you are looking for an alternative is, that you need a more up-to-date browser implementation, then WebView2 delivers. If, on the other hand, you are looking for an alternative, that's easier to use, then not a whole lot changes. Either one expects very intimate familiarity with COM and C++.

Firefox extension and nsIMutationObserver

I am a Firefox C++ extension newbie. I need to get access to DOM mutation events in my extension during page load. Firefox by default doesn't send DOM mutation events during page load to improve page load performance.
I understand the reason, but understanding the consequences I still need access to the DOM mutation events. I read somewhere that nsIMutationObserver still gets invoked during page load (and is bit more efficient then DOM mutation events as don't have to walk up the DOM tree looking for listeners), however it's only available to native code.
So I have following questions :
Is nsIMutationObserver and nsIMutationObserver2 available to Firefox extensions?
If yes, how can I write a simple Firefox extension in C++ to get access to it and expose it to Javascript?
If folks can point me to a existing extension that does this forwarding from C++ land to JS, that will be highly appreciated.
Or can I use JS-CTypes to get access to that functionality from my Javascript based extension?
BTW, I asked this question in Firefox's extension forum, but no replies there.
Thanks in advance
Is nsIMutationObserver and nsIMutationObserver2 available to Firefox extensions?
Yes, binary Firefox extensions can use it. Of course, the drawback is that your binary XPCOM component will only work with one Firefox release - it will have to be recompiled for each new release.
If yes, how can I write a simple Firefox extension in C++ to get access to it and expose it to Javascript?
You create an XPCOM component (see example code) and implement nsIMutationObserver interface. You then attach this mutation observer to documents like this:
NS_IMETHODIMP
MyMutationObserver::AttachToDocument(nsIDOMDocument* document)
{
nsCOMPtr<nsINode> node(do_QueryInterface(document));
node->AddMutationObserver(this);
}
For reference: nsINode interface
If folks can point me to a existing extension that does this forwarding from C++ land to JS, that will be highly appreciated.
Sorry, don't know any. But your XPCOM component can expose an additional interface that your JavaScript code will use - e.g. to register a callback. You have to consider that it might not be safe to run JavaScript when the mutation observer gets called. Important methods here: nsContentUtils::IsSafeToRunScript() and nsContentUtils::AddScriptRunner() (see nsContentUtils.h).
Or can I use JS-CTypes to get access to that functionality from my Javascript based extension?
No, you cannot. These are Gecko internals, they aren't exposed to js-ctypes.
A binary extension can use nsIMutationObserver, but unless it's very very careful about what it does when it's notified (see the big WARNING above the interface declaration) it'll cause crashes and various other broken behavior and is likely to introduce security bugs. Like any other internal API, this is a footgun; probably a fatal one if not used extremely carefully. Things that are fine to do in a DOM mutation listener are NOT OK in an nsIMutationObserver.

Creating gui binding for c++ and YUI

I am planning to use yui or jquery ui as front end for native c++ applications. I found no bindings present for this purpose. Will it be feasible/possible to do this ? If it is so, then how shall i proceed? please suggest advice.thanks
#rwik - I can see your point and desire to do what you intend and I would presume it to be a wise move to be making at this point in time considering the vast facilities browsers bring to the table as well as other aspects... I know we are in September now and I have come across this pretty late, but this is also for others who have a similar problem...
As far as I know, descending from JavaScript in to C++ is pretty murky, mostly due to the heterogenous nature of Javascript engines [Rhino, Tracemonkey, Spidermonkey, V8, Caracan,Charka, etc] employed by different browsers. But there are indeed ways to do this - depending on how dirty you want to get your hands and, indeed, on your abilities...
If you were to go with Google as a browser, you should take a look at projects such as cproxyv8 - http://code.google.com/p/cproxyv8/ or v8-juice - http://code.google.com/p/v8-juice/ which both offer interesting facilities...
My preference however is with Mozilla - simply because of XML User-Interface Language (XUL) which, if you do not know, allows you to speedily create your own user interfaces by giving direct access to its layout engine. I.e., you can even do away with the browser look and have your own independent layout while still having all browser facilities at your disposal.
With respect to C++/Javascript interaction - projects should exist for all alternatives - it pays to check.
I wish you good luck if you haven't already solved this problem.
I'd recommend QT instead.
However since native C++ has nothing to do with GUI I'm going to assume you are making a library in C++. However Yui is basically just JavaScript as is jQuery. So if you're asking can a web page or script be a C++ application the answer is not really.
What you could do is create a web service which uses your C++ library and exposes an interface for your web based application to call it indirectly.
One way to accomplish this is to embed WebKit into your application and provide your custom C++ functionality via a plugin. Both the WebKit framework on OSX and the Chromium Embedded Framework on Windows allow this style of application development. This allows you to write a UI using local Javascript/HTML files without the need for a webserver.
There are no direct bindings in JQuery and YUI because these libraries run in a web browser. Thus unless you’re planning to write your own browser plugins, the integration with the C++ will happen at the webserver that serves the data to the browser.
You could feed the data from your C++ code directly into JQuery/YUI by building web services directly in C++. But you might want to build a web application that will be serving your JQuery/YUI pages and that act as user interface controller (like in the MVC pattern). Web application languages like Java, PHP, etc. all have ways to call native code. Java has JNDI and in PHP you build extensions that link to C++ code.

Embedded ActiveX analog using Django

This might be a naive question, but when using a python framework, what approaches are available to provide the kind of functionality that can be exposed using an ActiveX control as an embedded object. The specific example I have in mind is a third party ocx we are using in some ASP.NET pages to provide TWAIN interaction on the client side. I'd like to avoid browser and platform specific approaches, if possible.
Your question is a little confused. If you're talking client-side, it makes no difference whether you are using Python or ASP.NET on the server - you can still embed the object in the HTML and the client will run it, assuming they are using Internet Explorer. However, this already is a browser- and platform-specific approach.
The best alternatives are probably Flash and Silverlight - although the latter is from Microsoft, it is pretty well cross-platform now since all the main browsers and OSs have implementations of it. Flash is still more widespread for now, though.

What embedded browser for C++ project?

Is there any browser I could embedd in C++ application on Windows?
I need all features typical browser has (HTTP client, cookies support, DOM style HTML parser, JavaScript engine) except rendering.
Because I don't need rendering capability (and that's rather big part of a browser) I would prefer a browser with non monolithic design so I wouldn't have to include rendering stuff into my project.
It would be nice if it had C++ rather than C API.
I need this embedded browser mainly because I have much trouble finding C++ HTML parser which could handle broken HTML like browsers do.
If you know any, please answer Library Recommendation: C++ HTML Parser SO question or at least vote on it to increase a chance someone will give a good answer.
Sounds like all you need is something like libcurl which is an HTTP library and will let you do GET/POST/etc.
When I think browser I generally think rendering/JavaScript and not HTTP library.
Edit
In that case I'd look at WebKit (which I think has a C++ API) and hope you don't have to pull too much in.
Edit Again
On second thought (since rendering is such a big part of what browsers do), you might be better off using a stand-alone JS engine like SpiderMonkey and a stand-alone XML parser like Xerces-C (plus maybe tidy to make your HTML into XML).
I'm a bit confused by your question regarding embedding a web browser for which you don't need rendering capabilities. A web browser is rendering web pages by definition, unless you just need HTTP and XML with JavaScript capabilities which is a subset of a browser functionalities?
If you need a web browser to embed in your C++ application, I would suggest to consider Qt that comes with the WebKit plugin. It is C++, LGPL and has a very nice IDE (Qt Creator). I tried Qt with Qt Creator on unix (Ubuntu) and it was very impressive. The debugger is a bit light but it is just the first version. The adapter of Qt into visual c++ 2008 is now free.
You might also want to check out Awesomium-- it's free for non-commercial use and has all of the features you're looking for (if you don't need rendering, simply don't use it).
There is a project called CEF = The Chromium Embedded Framework - it is:
a simple framework for
embedding Chromium-based browsers in other applications. It is a
BSD-licensed open source project founded by Marshall Greenblatt in
2008 and based on the Google Chromium project. Unlike the Chromium
project itself, which focuses mainly on Google Chrome application
development, CEF focuses on facilitating embedded browser use cases in
third-party applications.
and yes:
The base CEF framework includes support for the C and C++ programming
languages.
Including javascript support and html parsing makes this non-trivial task - you have to use one of the available browsers.
IE is usable through its COM model - you can create instance of it in your window be it invisible or not and call its javascript/html capabilities.
It has been designed to be used like that since the beginning and certainly it is working fine.
The other options are:
Gecko/Mozilla - a couple of years ago it wasn't usable like this, currently I think it is.
WebKit/V8 - no public API has been released for chrome yet, you could use webkit itself, but it doesn't have javascript engine. Another option is to take a look at the Chrome codebase and see if you could get out of it what you need.
I would probably go for IE, since it is maybe the easiest option and I have already used it. The other options seem to me more like building a browser instead of just using it.
How about Gecko ? You may not need the entire engine but you may find some its components useful like SpiderMonkey which is a JavaScript engine written in C.
I'd recommend picking up Qt for C++ programming. It has a built-in library that embeds Webkit with all the bells'n'whistles, and Qt is a great C++ library in general.
Old question, I know, but take a look on
http://msdn.microsoft.com/en-us/library/ky29ffxd%28v=vs.94%29.aspx
IActiveScript and family COM interfaces allows script execution (not only JS, any language that registers as script interpeter, for that matter) in-memory.