How to work with COM object using JavaScript? - 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

Related

Array.filter function in serverside Javascript

I would like to filter an array in my XPage with serverside Javascript. Unfortunately I get the following error:
Error calling method 'filter(Function)' on an object of type 'Array [JavaScript Object]'
I have an Array of Strings like ["elem1","elem2","elem3"]
I call the function like this:
list.filter(function(){
});
Is there any reason why this error happens? Does this function even exist in ssjs?
This question is not duplicate since it is not clear that Xpages/Lotus Notes runs Rhino in background.
It sounds like whatever server-side JavaScript environment you're using doesn't support ES5 features (that's features from the 5th edition specification from December 2009).
You can use a polyfill for that and other things that were added to Array, see MDN, but beware: If ES5 features aren't supported, it's impossible to add things to Array.prototype without making them enumerable, meaning any code (mis)using for-in to loop through arrays will be affected.
There is Rhino behind XPages. And this SO topic
No Array.filter() in Rhino? says, it is out of date.
Edit:
No, it is not. Years ago I read somewhere about it. Now it seems it was not true. According to comment by Dan Sickles (quoting Philippe Riand?) here:
It runs on the server jvm and uses javascript as the application
language. For licensing reasons, IBM wrote their own jvm javascript
engine instead of using Rhino. With Rhino shipping in java 6, they
should be able to ship it in Designer 8.5 (or later). The licensing
problems may have been around the extensions like #Formulas and type
declarations. Classes, modules/namespaces and type declarations are
coming in javascript 2 and even google is helping to get that
implemented in Rhino. I'd hate to see a non-standard, javascript
engine underlying the coolest web development technology in Domino.
Speaking of Rhino, the "most important new feature that is not as
certain to be in 8.5 as XPages" uses Rhino and other jvm scripting
languages on the client. If this makes it into the product, two years
from now most new Notes applications will be written in neither
Lotusscript nor Java. I'll leave it at that.
In fact, there are few topics how to use Rhino in XPages, so with newest Rhino version your code would work. Anyway, my advice is to use Java calls.

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.

Invoking JavaScript Method via COM Interface

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

Javascript (and HTML rendering) engine without a GUI for automation?

Are there any libraries or frameworks that provide the functionality of a browser, but do not need to actually render physically onto the screen?
I want to automate navigation on web pages (Mechanize does this, for example), but I want the full browser experience, including Javascript. Thus, I'd like to have a virtual browser of some sort, that I can use to "click on links" programmatically, have DOM elements and JS scripts render within it, and manipulate these elements.
Solution preferably in Python, but I can manage others.
PhantomJS and PyPhantomJS are what I use for tasks like these.
What it is, is a headless WebKit based browser which is fully controllable via JavaScript. There's a C++ implementation (PhantomJS) and a Python one (PyPhantomJS). I prefer the Python one though, because it has a plugin system which allows you to add functionality to the core without actually modifying any code, unlike the C++ one. :)
There is an absolute ton of free software technology now available: take your pick at http://wiki.python.org/moin/WebBrowserProgramming but if you have specific questions join pyjamas-dev on google groups and i'll be happy to give further details, there. brief answer: you can run pywebkitgtk "headless", or you can use xulrunner (via python-hulahop) again using pygtk without actually doing "browserwidget.show()", and there's also pykhtml. also you could use python COM to connect to MSHTML.DLL.
these are all "cheat" methods: using python bindings to a graphical web browser engine without actually firing up the graphical bit. if you really wanted to put some serious hard-core programming in, you could create a "port" of webkit which was not connected to a GUI toolkit: as an experienced webkit programmer i'd put it as around... 2 weeks of full-time effort to make such a "headless" version of webkit.
l.
Looks like http://watin.sourceforge.net/ might be a good way to go.
If you don't have to go pure Python, you could do IronPython since it's a C# project.
take a look at this little doosy on ajaxian
http://ajaxian.com/archives/server-side-rendering-with-yui-on-node-js
It also talks about Aptana Jaxer which I think runs on a headless firefox so is basically the Mozilla browser engine in all it's glory.
There is Kapow. Its pure Java and costs money:
http://kapowtech.com/
And there is Lixto: Its Eclipse based and uses Mozilla Gecko as rendering engine (unless they already changed it to WebKit, as they said they'll do years ago). Its very nice and also costs money:
http://www.lixto.com/?page_id=50
They are both graphical tools where you define the site navigation and what should be extracted by point and click. But you can also write xpath and regular expressions and even JavaScript that runs in the sites context.
I used them both in the lectures web data extraction and applied web data extraction at the technical university Vienna (Lixto is written by the Professor who held the lecture).
HTMLUnit in Java is very good. I think it's only the Java implementations of headless browsers that manage to provide Javascript support.
MaxQ, I read about here, sounds like it might be interesting: "written in Java, generates Jython scripts"
Try HtmlUnit !!!

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