Invoking JavaScript Method via COM Interface - javascript

I am working on a project to interface Matlab and Google Earth Plugin.
My idea is to use COM interface, in which the MATLAB as COM-Client and Google Earth Plugin in Internet Explorer 9 as COM-Sever.
But until now, I still don't have idea how can I invoke a JavaScript method from MATLAB so that I can update the view of Google Earth in Internet Explorer.
Is it possible to invoke a JavaScript method via COM?
My second idea is to build a custom webbrowser based on InternetExplorer and then embedded custom COM methods and properties, so that it can function with Google Earth regarding the invoking from external application.
Any help would be highly appreciated.
Regards,
Wan

Looks like the only supported Google Earth API is the JavaScript API. So your approach of embedding Internet Explorer for this sounds reasonable.
I'm not so familiar with the MATLAB end of things, but assuming you can embed the web browser control you should then be able to start invoking scripts.
At the lowest level, the IE web browser implements the IWebBrowser2 interface. This interface exposes a Document property, which returns an IHTMLDocument2 interface. Call IHTMLDocument2::parentWindow to get an IHTMLWindow2 interface.
Once you have the IHTMLWindow2, supposedly from my reading you have a couple options. Your script can call IHTMLWindow2::execScript. Alternatively, your top-level JavaScript functions should become available as methods on the IHTMLWindow2 interface via the inherited IDispatch: especially useful if you need the return value.
Some Delphi code that uses execScript: http://www.delphidabbler.com/articles?article=21
Some .NET Framework code that uses IDispatch directly (see the commented-out more complicated code example): http://www.west-wind.com/weblog/posts/2008/Sep/27/Calling-JavaScript-functions-in-the-Web-Browser-Control
To make a long story short, essentially you need to do this:
myWebBrowser.Document.parentWindow.MyJavaScriptFunction()
or this:
myWebBrowser.Document.parentWindow.execScript("MyJavaScriptFunction();", "JavaScript")

Related

Accessing native c functions from javascript

I have a javascript application which will be running on an embedded platform. It has to call some c APIs which are already implemented in the platform. This is a proprietary application and is meant to run only on selected devices.
I am running this application on webkit. There is no JVM available.
I am thinking of some options and got a few. I would like to know whether any of them are applicable or a better option. I am new to javascript so my ideas may not be right.
Something similar to JNI ( if this exists at all)
Develop a plugin for webkit which exposes some Javascript APIs to the application and then calls native c functions
Modify webkit
Your 2nd suggestion is pretty close to what you need to do. When you compile the browser (you don't need to compile webkit yourself) you can register functions and objects.
If you are using Qt, you should probably start here.
If you are using GTK, the registration is different, here is a nice example.
One approach you might want to explore is script alert handler. I know webkit gtk supports it http://webkitgtk.org/reference/webkitgtk/stable/webkitgtk-webkitwebview.html#WebKitWebView-script-alert
You can have simple javascript alert on your webview and parse parameter on other side and then call the required function. Of course, if you have complex parameters (e.g. objects) which can not be translated to strings easily, this approach will be difficult to implement.
Only problem is I don't know if this functionality is supported on your platform since you did not mention the platform, there is no way for me to check.

Building a Windows app w/ embedded browser & JavaScript 'hooks'

I built a PHP / JavaScript website for a customer. Then they asked me to replicate it except as a standalone Mac application. I did this with an app that combined an embedded web server, PHP, and 'WebView' - a Cocoa-ish version of the WebKit web browser that I can embed in a standard app window. This all worked great - I got to reuse 10,000+ lines of PHP/JS code, which saved months off of re-implementing it all again in 'native' code.
Now they want a Windows equivalent. I'm reasonably confident I can get PHP and the web server to work. And I know embedding basic IE functionality is pretty easy.
However...in my Mac setup, WebView (via the windowScriptObject stuff) gave me the ability to call JavaScript methods from C++. For instance, I might call a JavaScript method from C++ to update the screen. Likewise I could set things up so that a JavaScript call in the browser could trigger a C++ method - I used this, for instance, to let a user click 'BROWSE' and pick a file path using a real, standard file browser.
So my question is, is there a Windows-based embedded browser setup that would let me interact with JavaScript in this same way?
(the JavaScript <--> WebKit interface is described in much better detail here: http://lipidity.com/apple/javascript-cocoa-webkit/)
Maybe try using something like Appcelerator Titanium so you'll be ready when your client says they want it to work on Linux, or iPhone, or Android.
Quoting Wikipedia: "Appcelerator Titanium is a platform
for developing mobile and desktop
applications using web technologies.
[...] Support for standards-based web
technologies: HTML, CSS and Javascript
on all platforms along with PHP,
Python and Ruby for desktop platforms.
Integrated support for popular
JavaScript and AJAX Frameworks
including jQuery, YUI, MooTools,
Scriptaculous and others."
Sounds like a perfect tool for the job.
When you embed the Web Browser Control (IE), your application code can simply call execScript (http://msdn.microsoft.com/en-us/library/ms536420(v=vs.85).aspx) on the window object. You can have your script call out to the application by using the window.external object from the script, and by using the ObjectForScripting (or C++ equivalent) from the application.
maybe Qt will be good for your case, also you have QtScript and can inject javascript with evaluateJavaScript
I found a great example on the web for invoking JS in my embedded browser from C...basically using COM-ish methods that let you get a DISPID from a script object, and then using the Invoke() method with that. This works great.
But it turns out I need to also call C++ funcs from my JS code. It appears this is possible, and after hours of messing around I think I almost had it - it's like the above in reverse - you create a COM object, then hook it to the browser's script object - but in the end I could not close the deal - I kept getting "library not registered" errors. Honestly I don't know COM well enough to do this right.
So then I, for the heck of it, tried building my first C# app. In about 20 minutes I had an app running with a browser where I could both invoke JS inside of it and have the browser invoke C# methods. Geesh. I'm a believer in .NET after this experience, and a confirmed non-believer in 90's Microsoft technology.
In the interest of completeness, I'd like to mention my Windows port of WebKit, which includes the various cross-layer features of WebKit on the Mac.
I posted some example code showing how to embed WebKit in a native WinAPI application, complete with JavaScript->C++ and C++->JavaScript examples.
The example is a tiny test case for a much larger application using embedded WebViews for major UI components. I can confirm that what you are doing is not only possible, but a great way to build an application.

How to work with COM object using JavaScript?

I drive into this issue:
I create COM object using C#, register it and managed to work with it using powershell.
when i trying to do the same with JavaScript it fails, but javascript keeps throwing object null errors.
Do you have any advice on how to fix this problem? or maybe you JavaScript doesn't support COM (if so, where can i read more about it)???
Thanks a lot!
Use Shanti Rao's JSDB shell. It's based on the core Spidermonkey engine (Mozilla's Javascript implementation) used in Firefox, but has a bunch of bindings for databases & ActiveX objects and such. It has a few limitations but unless you're using something complicated you should be able to make use of it.
Example:
x=new ActiveX('MSXML2.DOMDocument.6.0');
x.async = false;
// I forget how to use IXMLDOMDocument but other calls go here
I know this is a bit late, but for others who find this, yes this can be done easily. This assumes you're running on Windows since you're looking for Windows/JavaScript interoperability.
The most important question is "what JavaScript engine are you using?" as this functionality is determined by that engine. Since 1995, Windows has supported a system standard scripting model originally called OLE Automation or sometimes just COM. Windows-based scripting engines like the JavaScript and VBScript engines built into the Windows Scripting Host use this engine, in addition to IE through version 8 and I think up to 11. However, the IE container implements security restrictions that prevent some of what I'm describing from working. Open-source JavaScript engines like node.js typically do not use COM as this is Windows specific functionality and so cannot do what I am describing.
Given that, to accomplish what you want, you must:
1. Implement a scriptable COM object.
2. Register that object (typically automatic during your build process).
3. In JavaScript, create an instance of that object using new ActiveX object, as mentioned above.
You can write your object in both C# and C++. In both cases, you need to base your object on IDispatch. C# will make the whole process considerably easier. Essentially, you generate a few unique GUIDs for your interface and component using guidgen, then using a few COM-specific attributes in C# to provide these. Here's a link to a great simple example (ignore the Events stuff):
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/interop/example-com-class
The most important thing to know is that you will be limited on what data types you can take as parameters or return to the caller. Things like strings and ints are no problem. For others, you can describe them in C# and send them from C# to JavaScript, but the other way around is not going to work.
Javascript indeed does not support COM. An option is to use JScript and an ActiveX wrapper to a COM object. Also, it will only work in Internet Explorer.
Instantiating a COM class
Calling functions of a COM object in JScript
Other JScript/COM tutorials, including script callbacks

Is Javascript only available for web browsers?

I would like to know about Javascript. Is Javascript available only for web browsers? Because I used some Javascript code for Firefox Plugin development and Thunderbird.
Help me to find out more about this: where can I use JavaScript other than web browsers, and how?
There's node.js which includes a full webserver and runs on Google's V8
No! Have a look at Google's V8
Google Javascript. Click first link (Wikipedia). Click "uses outside webpages".
http://en.wikipedia.org/wiki/JavaScript#Uses_outside_web_pages
There are a number of server-side (or otherwise non-browser) implementations of Javascript, including Node.js, which currently has a lot of momentum behind it, as well as the now-defunct Jaxer, which provided a server-side DOM implementation based on Mozilla Gecko. There are also efforts to bring DOM type functionality to Node.js, primarily from Yahoo.
With Rhino, you can embed Javascript in Java applications.
No, there are definitely other implementations. For instance, JScript.Net is included with the .NET framework.
MS-Windows can execute .js files (from the command prompt).
Since you do not have the browser context, you cannot use any calls to the browser (alert, for example, does not work).
Look for "Windows Scripting Host".
WSH supports scripts written in
Microsoft Visual Basic Scripting
Edition (VBScript) or JavaScript.
There are several implementations of stand-alone Javascript
JSLibs (http://code.google.com/p/jslibs/) is general purpose
jsdb (http://www.jsdb.org/) is more database oriented
Many text editors (including the one I use, ActiveState's Komodo Edit) expose their APIs and let you write macros in Javascript.
Using Adobe AIR you can write almost platform-independent Desktop Applications using JavaScript
PDF files can also contain JavaScript code. See first entry in google on this subject and also Adobe JavaScript virus.
Javascript can be easily embedded in applications written in other programming languages.
For Java there is as already mentioned Rhino, for C++ you can use for example Flusspferd. Here are some tutorials/examples.
Qt has the QtScript module. From Qt 4.7 it uses JavaScriptCore (the Webkit JavaScript engine).
In short, No.
Actually, Javascript and other Web technologies are used these days to create native mobile and desktop applications. (see Titanium Appcelerator and PhoneGap)
If you are familiar with the KDE project you can craft Plasma widgets using JavaScript, see Creating plasmoids with JavaScript
In Qt framework there is a QtScript module for JavaScript (ECMAScript) application scripting to provide much of the application’s functionality.
Also the new technology that is soon to be released in Qt 4.7 is Qt Quick, aka the Qt UI Creation Kit, which allows application developers to declaratively define their user interfaces in QML, for more information go here and an example here
QML is an extension to JavaScript,
that provides a mechanism to
declaratively build an object tree of
QML elements. QML improves the
integration between JavaScript and
Qt's existing QObject based type
system, adds support for automatic
property bindings and provides network
transparency at the language level.
And let's not forget Javascript is used as an embedded scripting language in various applications OpenOffice.org, Google Desktop Widgets, and many others, see Wikipedia's article here
On the server-side Javascript enables back-end access to databases, file systems, etc (see Node.js, Google V8, SpiderMonkey and others here)
Some document databases, such as CouchDB and MongoDB, use JavaScript to interact with the database.

Where in Windows is the Javascript file which contains functions for executing PAC files?

Proxy Auto Config (PAC) is the traditional method by which web-browsers are automatically configured to use the appropriate proxy for any given site. PAC files consist of a single function implemented in JavaScript.
I'd like to execute this function for another puropose: I'm trying to make an application which selects a proxy to use in exactly the same way that Microsoft Internet Explorer might do. Since I cannot simply ask IE what proxy it would use for a particular site, I'm trying to emulate what IE would do when it selects which proxy to use for a given web-resource.
It's easy to call a JS function with Windows Scripting host, but in order to execute a PAC file I need a few standard functions such as shExpMatch and isPlainHostName. These are usually provided by the browser. Firefox provides this file in an easy to use JS file - unfortunately the FF implementation does not seem to be perfectly compatible with Microsoft's implementation of Javascript.
I need to find where the official microsoft implementations of these functions are so that I can include them in my scripting environment before trying to call the function.
Any ideas where these might be?
Thanks
Update 0: I found a documented microsoft function which might do what I need - thr question is how can I call this from Win32Com in Python 2.4.4?
I don't know where the actual implementation is, however Microsoft have provided code that you can call that replicates the functionality in the .NET framework, in the System.Net namespace.
The class is called WebProxyScriptHelper, but unfortunately it is set to internal visibility so you can't call it directly (you don't say what language you are using so this may not even be an option). Fortunately, Microsoft have made the source code available anyway.
Edited to add:
There is also a Windows API function, WinHttpGetProxyForUrl. Perhaps this is what you should be using instead?

Categories