Accessing native c functions from javascript - 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.

Related

When javascript is disabled, React event does not work

I am referring to the following example for Node and React: https://github.com/yldio/react-example
I am able to add a book to the array when javascript is enabled in Chrome. When I disable javascript in Chrome, I am not able to add a book to the array.
Is there any way to resolve this?
Is there any way to resolve this?
Not in the way that you're hoping.
React is a JavaScript framework for the front-end. Which means it executes in the browser. If you disable JavaScript in the browser, React doesn't work anymore. Just in the same way if you delete Photoshop from your computer, you can't open .psd files anymore.
If you really must support browsers without JavaScript (which most people don't), you need to build your app to work with plain old HTML based navigation (think links, forms etc).
It's possible to use React (for people with JS enabled) and a fallback for those without, this approach is called Progressive Enhancement. Might be useful to you: https://softwareengineering.stackexchange.com/questions/25969/should-i-bother-to-develop-for-javascript-disabled

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

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