11

Do you think it would be possible to embed a HTTP server inside a Google Chrome extension?

I've seen other Google Chrome extensions execute native code and apparrently they do it by using NPAPI, but I have no idea what that is. And it's really freaking me out, as I'm no C++ expert or anything like that. So I feel a little stuck now and that's why I'm asking for help.

Specifically, I want to create an extension for Chrome with features similar to Opera Unite. For those who don't know what I'm talking about: Opera Unite is basically a zero-conf web server bundled with the browser. I don't want to use it for the same things Opera does, but I like the concept.

I was thinking about using something like node.js inside the browser to create a simple web interface to control some stuff in the browser. Think of it as a remote control for the browser. If node.js isn't possible I'd like to use lighthttpd or something similar. The technology really doesn't matter as long as it allows me to receive commands over HTTP.

But how do I take an existing web server and make a NPAPI plugin out of it?

Clarification: I'd like any browser to be able to communicate with my extension. Especially I want mobile devices like Android or the iPhone to be able to remote control the browser. Is there any other way to accomplish that except with a HTTP server?

Another update: I think the easiest way to do this would be to use a relay server on the web like Pusher or some self-created service. But I don't like this approach because it requires constant internet access and because it's a paid service.

Thank you all!

Jannes
  • 1,139
  • 1
  • 8
  • 17

3 Answers3

4

Here is another web server implemented with chrome.socket: https://github.com/kzahel/web-server-chrome. The server example linked to in chrome-app-samples is actually really buggy and will lock up if you e.g. hold down Ctrl-R on a page served by it.

kzahel
  • 2,587
  • 1
  • 18
  • 26
3

If you want to do a remote control for the browser, would something like HTML5 WebSockets work for you?

http://www.html5rocks.com/tutorials/websockets/basics/

You can have an external "remote" server that your extension listens to via WebSockets. If you want to host a webserver via extensions. You would need to use NPAPI, there are many C++ libraries out there (Google search) that can do a simple webserver. But I would rather use WebSockets communicate to an external server which will provide you anything you want.

But if you insist, you would need to learn C++, NPAPI, there are many examples online regarding NPAPI.

Mohamed Mansour
  • 36,569
  • 9
  • 110
  • 87
  • Thanks for your answer. I don't know if I understand you completely. I want any browser to be able to communicate with my extension. Especially I want mobile devices like Android or the iPhone to be able to remote control the browser. So are you saying I could use WebSockets to accomplish that? It would be really cool if it was so easy but I think I'm out of luck here. I don't think WebSockets are even implemented in most browsers yet. – Jannes Feb 19 '11 at 21:00
  • You can have a WebSocket server, and your extension will listen to that WebSocket. Your WebSocket server could be nodejs. All other browsers can send messages to that WebSocket server via HTTP (normal requests) then it will broadcast its message to the extension via WebSocket. But you can use NPAPI and host a server in everyones computer, but that becomes a security risk since your exposing native code. But the benefit of hosting a server on the client is that you can communicate directly to it, but that is a security risk, hence why NPAPI is not recommended. – Mohamed Mansour Feb 19 '11 at 21:08
  • 2
    Thanks. I can see how my own poorly-written NPAPI web server would be a security risk. It's really a shame that Chrome doesn't have this kind of functionality built in. It would really enable a lot of cool new things. Anyway, I think the relay server approach is probably the best way to do this, despite its disadvantages. It seems to be the only doable way without major security risks. Although I really think browsers should finally be able to do P2P communication with each other. ;-) – Jannes Feb 19 '11 at 21:26
  • Yes, I partially agree :) Creating a public P2P API is pretty hard, taking security into consideration into it too. The relay server has disadvantages, but it will make your extension securer and people wont be questioning about what happened. For example, creating a WebSocket remoting app to control the website through a hardware device is valid (can be fast), but it would be cool to have that hardware as a NPAPI extension so it will be realtime. So there is drawbacks, but it makes the web safer until a sandboxed API (such as NaCL PEPPER) gets finalized :) – Mohamed Mansour Feb 19 '11 at 21:55
2

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

konsumer
  • 3,133
  • 1
  • 25
  • 29
  • 3
    Note that this is a Chrome [app](https://developer.chrome.com/apps/about_apps), not a Chrome extension. Standalone chrome extensions cannot launch a server, though it is possible to start a server from an extension via external messaging (between extension and chrome app) or native messaging (between extension and some pre-installed application on the OS). – Rob W Aug 01 '14 at 11:23