Is it possible to load chrome extension in node-webkit window

会有一股神秘感。 提交于 2019-12-06 03:10:55

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/17116509/is-it-possible-to-load-chrome-extension-in-node-webkit-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!