Building a Windows app w/ embedded browser & JavaS

2019-05-03 05:45发布

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

5条回答
Anthone
2楼-- · 2019-05-03 06:14

maybe Qt will be good for your case, also you have QtScript and can inject javascript with evaluateJavaScript

查看更多
ゆ 、 Hurt°
3楼-- · 2019-05-03 06:19

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.

查看更多
Evening l夕情丶
4楼-- · 2019-05-03 06:23

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.

查看更多
爷、活的狠高调
5楼-- · 2019-05-03 06:28

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.

查看更多
我想做一个坏孩纸
6楼-- · 2019-05-03 06:29

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.

查看更多
登录 后发表回答