6

I am trying to make a node.js app using node-webkit . Is it possible to load a chrome extension in node-webkit window and make it appear as it appear in chrome browser

I have gone through this but I am only able to understand that it can load NPAPI plugin. Is there a way to convert chrome extension to NPAPI plugin?

taxilian
  • 14,229
  • 4
  • 34
  • 73
Jagdeep
  • 312
  • 2
  • 8

2 Answers2

6

Loading Chrome extensions is supported now with '--load-extension' switch from Chromium.

Previously, since node-webkit was based on Content Layer of Chromium, where there was no Extension support.

Roger Wang
  • 1,388
  • 10
  • 8
  • Thanks !! Is there a way to convert chrome extension to NPAPI plugin? According to node-webkit documentation it can supposedly load this kind of plugin. – Jagdeep Jun 15 '13 at 06:42
  • @pc1500 Do you have anything to support the claim that upstream is moving it to the content layer? I'm not inferring falsehood, but I'm curious so I can follow it. – blockloop Jul 16 '13 at 19:39
  • did you find a way to do it, and can it be done with chrome apps instead of extensions. i just want a way to access api that come with chrome extensions/app....also is there a way to do what you asked for like use extension or plugin via plugin... – Muhammad Umer Sep 07 '13 at 18:28
  • http://stackoverflow.com/questions/18647853/is-there-a-way-to-package-chrome-apps-using-nodejs-webkit – Muhammad Umer Sep 07 '13 at 18:28
  • pc1500 i think i emailed you – Muhammad Umer Sep 07 '13 at 18:28
  • @Roger - Can we install chrome extensions in latest nwjs now in 2017? – Ajey Jul 06 '17 at 14:05
4

As Roger Wang mentions in his answer, node-webkit does not currently support the Chrome Extension or the Chrome App API's. However, I initially created a Chrome App, and when it was clear that Google was focusing more on the Chrome OS platform than Mac OS and Windows, we made the switch to node-webkit.

To make it easier to port over our Chrome app to node-webkit, I created some API stubs that wrapped around some of the node-webkit, Node.js, and node module APIs that do some of the same things as the Chrome APIs.

Example stubs for chrome.alarms.* APIs:

Here's an example that Stubs out chrome.alarms.* so that it at least doesn't throw errors and silently fails:

 window.chrome = {
    alarms: {
        clear: function(name) {
            console.warn("not implemented.");
        },
        clearAll: function() {
            console.warn("not implemented.");
        },
        create: function(name, obj) {
            console.warn("not implemented.");
        },
        onAlarm: {
            addListener: function(callback) {
                console.warn("not implemented.");
                return;
                var alarm = {
                    name: ""
                };
                callback();
            }
        }
    }

Example stubs for manifest and background page access:

And here's an example of getting the package.json manifest using chrome.runtime.getManifest, as well as access to a background page in node-webkit via chrome.runtime.getBackgroundPage, assuming the background page opens the window and its parent is the background page:

    chrome.runtime: {
        getManifest: function() {
            return typeof(require) !== "undefined" ? require("../package.json") : {};
        },
        getBackgroundPage: function(callback) {
            var backgroundPage = {
                postMessage: function(message, origin) {
                    if(window.opener != null)
                        window.opener.postMessage(message, origin);
                    else
                        window.postMessage(message, origin);
                }
            };
            callback(backgroundPage);
        },

With this solution, we more or less just added a package.json file to the app, added in the API stubs, and had a running app on day 1.

(Disclaimer: This is my open source contribution to node-webkit)

You can find the "node-webkit-chrome-api-stubs" in my GitHub repository.

The code in the GitHub repository actually runs as both a Chrome App as well as a node-webkit app, for demonstration purposes.

jamesmortensen
  • 33,636
  • 11
  • 99
  • 120
  • Does the document share, screen share extensions works with your solution using latest node-webkit? –  Feb 09 '15 at 07:48
  • 1
    @yumyum we do screen share and file share in our node-webkit app. It is possible, but we don't use the stubs. We just used the existing apis and abandoned Chrome apps. – jamesmortensen Feb 09 '15 at 08:06
  • What about USB and Serial? – Pier Mar 09 '15 at 15:23
  • @Pier - If there's a native way to use USB and Serial through node-webkit, then you can wrap it in the stubs. At this time, I have not written stubs for those components, so feel free to use the other stubs as an example. Hope this helps! – jamesmortensen Mar 09 '15 at 15:45
  • It seems there are some C++ Node modules to do that, but to use them in NWJS they need to be recomplied. – Pier Mar 09 '15 at 16:22