8

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 );
}
Community
  • 1
  • 1
user2366975
  • 4,350
  • 9
  • 47
  • 87
  • If you want to support multiple browsers/OS, why you still use google chrome extensions? It is only for chrome. – Haibara Ai Jan 25 '16 at 08:38
  • because it was the easiest to start with and the core (js, html, css) should be quite universal... – user2366975 Jan 25 '16 at 20:11
  • This is similar to a question I want to ask but in my case the local web-server would only need to respond to requests from the browser rather than push updates. Is that something I can do directly from JS without extensions/apps? Figured I'd ask here before raising a new question in case it's trivial. – Mr. Boy May 28 '19 at 11:48

1 Answers1

5

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

Community
  • 1
  • 1
vmp999
  • 66
  • 1
  • 4
  • 1
    This is also what I figured out in the meantime. I had to upgrade vom Qt5.2 to Qt5.3 because of the added support for web sockets though. I am using HTML5 chrome-side and Qt, no node.js needed. – user2366975 Mar 17 '16 at 11:50