Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect chrome web extension with localhost server?

I have a google chrome web extension that needs to communicate with a Qt desktop application - but how?

  • There is chrome's native messaging, but as I want to support multiple browsers/OS, this would be too much effort because it is only for chrome.

  • Then there is this post that suggests setting up a local server. This is what I did, see below.

I have set up a server with Qt with QTcpServer that uses QTcpSocket's on 127.0.0.1 (localhost). But a web extension can not listen to sockets, only chrome apps can. There are 2 possible solutions on my mind:

  • As a workaround, I could perhaps write a small chrome app. The Qt application would talk to the chrome extension via the chrome app (chrome apps support sockets). But I think this method is clumsy and not quite elegant.

  • On the other hand, I have read about socket.io. The idea is: The chrome extension talks via http requests with socket.io, and socket.io talks via sockets with my desktop app. Is this a possible solution?

What I also tried, is to directly connect to the local server with the following code. In my Qt server application, I see that there is a new connection. But I can not get a response at all (either my Qt code is wrong or it is because extensions can not listen to sockets?)

var xhr = new XMLHttpRequest();
xhr.open("GET", "http://localhost:12345", true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
  alert('This is the response from the server: '+ xhr.responseText );
}
like image 693
user2366975 Avatar asked Jan 23 '16 23:01

user2366975


People also ask

How do I run Chrome extensions locally?

Go to chrome://extensions/. At the top right, turn on Developer mode. Click Load unpacked. Find and select the app or extension folder.

How do I enable localhost in Chrome?

1) Open Google Chrome browser. 2) Type chrome://flags/#allow-insecure-localhost in address bar. 3) Click on Enable. 4) Select "Relaunch Now" option displaying at the bottom after making the changes OR Re-open the chrome browser.

Can Chrome extension run server?

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).

How do I connect to a localhost port?

In the IP address and port textfield on the right, enter the IP address or hostname on which your site is running on your development machine's web server, followed by the port number. For example, if your site is running on localhost:7331 you would enter localhost:7331 . Click Done.


1 Answers

as you already know extensions can not create direct connections:

Google Chrome Socket API in extensions

possible solution

maybe your QT application could serve a websocket and you should be able to communicate with that from Javascript:

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

if you are unable to serve websockets from inside the QT application, another approach could be create a "bridge" a little script that could serve a websocket to your JavaScript and pass the messages from/to the QT application

you will find plenty of examples on websockets, the easy way to get into this could be creating a little server using node.js to play with it stackabuse.com/node-js-websocket-examples-with-socket-io/

oh! and do a search for "websocket same origin policy"

Example of an extension using websockets (that will be useful for debugging): chrome.google.com/webstore/detail/simple-websocket-client/pfdhoblngboilpfeibdedpjgfnlcodoo?hl=en

hope this helps

like image 152
vmp999 Avatar answered Nov 04 '22 01:11

vmp999