How to Embed a Http Server in a Google Chrome Extension

Is it possible to embed a HTTP server in a Google Chrome extension?

Here is info on making a web-server in a Chrome app:
https://developers.google.com/live/shows/7320022-5001

embedded http server in c++ for chrome extension native client

Short answer: not possible.

If you want to open a port on a local machine to allow connections, then that is not allowed by the web security model. NaCl runs with the same privileges as JavaScript, no extra holes. However, you may specify extra flags to chrome on start to get more permissions from NaCl, such as open debug port, or get access to raw network sockets.

If you want to 'emulate' an HTTP server to make your extension keep using it regardless of being offline, then it is easier to use the PostMessage API.

I need a way to include a http-server

This error is happening 'cause you are opening the html document directly from the browser. To fix this, you'll need to serve your code into a web-server and access it on Localhost.

So you can either install a web-server in your local PC or upload the model somewhere else and change the URL to http://example.com/path/to/model

Embedding images into a Chrome extension

You could use chrome.runtime.getURL() (docs) to get the internal extension folder URL. Pass it the image relative path and you'll get the full URL for it.

For example, if you have a folder named "images", and an image named "profile.jpg" in it, both in the extension folder, you could inject it into the body of the page by doing:

var image = document.createElement("img");
image.src = chrome.runtime.getURL("images/profile.jpg");
document.getElementsByTagName("body")[0].appendChild(image);

Also, check out the web_accessible_resources manifest property documented here - you might need to declare your resources for them to available in this method.

How to use the ChromeApp to connect to a node.js server?

You cannot launch external URLs with chrome.app.window.create. In fact if you check the chrome.runtime.lastError property you will see the following error:

The URL used for window creation must be local for security reasons.

I suggest you look into using the <webview> tag as it is much more appropriate for your use-case.



Related Topics



Leave a reply



Submit