📜 ⬆️ ⬇️

Node Webkit without webkit

We once discussed desktop applications nw.js. Everything is good, but the need to distribute the entire browser engine (which over time and the addition of new features does not become less and compressed 30MB now weighs) is depressing.
And what if you make a module for node.js, which can show UI in the system browser?
Under the cut I will tell about the attempt - how it is, whether it is worth the effort, whether it can be used, and what happened as a result.

Dicslaimer


Basically, the project was made more like a proof-of-concept: play around, see, check how it is. I made 3-4 orders on it in the style “I want a small application with a built-in webview” or “I want a wrapper for the site in the desktop application window”. Maybe someone my experience will be useful, so laid out in public. The code on the githaba . I will not describe the nuances of the code in detail, the goal is not to describe the code, but rather to think about the suitability of the result.

How it works


An application is statically assembled with node.js with its entry point. When launched, a native module with the window display logic is added to the node, and control is passed to _thirdPartyMain.js (there is such a possibility in the node). From there, the user main.js is loaded, where the ui module is connected. The module starts the http-server, opens a window with the specified config and sends a webview to the address of the embedded server that delivers user-generated content. The window generates events (move, minimize, ...) and is able to execute methods (for example, to close).

Webview


Under Windows, the ActiveX WebBrowser is inserted (this is the same MSIE, but without Tublars, address lines, etc.). By default, it shows a lot of unnecessary dialogs (which are turned off in a strange way), but in general it works as you want. IE, it doesn’t matter , but it was necessary to support XP, so for older versions of Windows you can download a webcam (chrome embedded framework). If necessary, if the user does not mind, the application downloads the library, unpacks them and works already with the webcam.
Under poppy, everything is more transparent, WebKit WebView compared to IE in the integration is much easier and more understandable; under Linux is webkit-gtk.
')

Packaging application


It was necessary that the applications were packaged in one EXE. ZIP turned out to be the most suitable format: it is read from the end, allowing you to add anything to the executable file and unpack it from yourself; on this principle, and work SFX. To use from node.js, you can hang a hook on calls to fs and redirect requests to files that are in the archive to the virtual file system instead of reading from the disk.

Browser interaction


IE and WebKit offer ways to interact from C ++ to JS, and vice versa. In IE, a javascript object, including a function, is an IDispatch object. If you check the typeof of such an object, the javascript engine will return unknown, these are the so-called host objects (the use of such results of the typeof operator in the ES6 standard is no longer recommended , but earlier nothing was said about non-standard types). You can call something from C ++ by accessing the window.external object, provided that the host has implemented this method.
Similarly to a webcast, you can add a window property as an object with methods available for invoking.

Interacting with node.js


In the node, the plug-in creation process is well documented . Inline add-ons are created in the same way, but they are registered by another macro. After creating the addon class, it inherits from EventEmitter and is able to generate events by getting a reference to the emit method.
Node.js intentionally lives in a separate thread, so long-running operations do not block the UI, so the UV library is used to send messages to the node and synchronize.

What worked out well


Application size


It is 2-3 MB (unpacked 7). In principle, you can make the assembly smaller, if you only need to open a page in the application, without node.js (in general, for the sake of size, everything was done).

Start time


Since the application does not use additional frameworks, it starts quickly in an average of 1 second (about the time of the first launch, see below); By adding profiling, you can make sure that most of the time is spent on browser initialization.

Problems


First start


Windows optimizes the launch of applications and components that you use more often. If IE is rarely used, the time of the first launch of the application is about 3..10 seconds, depending on the operating system and computer.

Browser Versions


Very often, IE is used as a tool for downloading the browser and is never updated at all (although the situation improves with the latest versions of Windows). IE cannot be forced to be updated (show the dialog, update IE from the application is tantamount to suicide: updating IE is difficult, long, often requires a reboot - it’s impossible to mock the user, he will simply find another application).
Browser versions are tied to OSes: for Windows XP, there is no IE10, for OS X Lion there is no Safari 8.

Javascript contexts


Objects node.js cannot be used in the browser and vice versa, the interaction occurs either in the classic client-server application (xhr), or in the manner provided by the program (postMessage in the window or node) —that is, as in a web worker, only simple data can be transferred. It was possible to make the context bridge, but firstly it’s difficult and cumbersome, secondly it’s short-sighted, because ES6 is almost there, and it’s not known what to do, for example, with the proxy object in IE10.

What is


There is an assembly under win / mac / linux, which in principle can be used, customized, and so on. I use it to create customized “browser applications”, when it happens to someone suddenly. It is relatively stable, but I did not bring it to mind and release quality, because ...

findings


Applications are rarely downloaded, the Internet is now quite fast (and in the case of installing applications, EDGE can really be neglected, because the application is not a site, it will not be installed on the road), unsolvable problems in using the system browser are present. In most cases, now the game is not worth the candle and use the webkit seems more correct. If the goal is to create a small application mainly for new OSes - or maybe it can be so.

Links


Source: https://habr.com/ru/post/260789/


All Articles