How to connect chromed web extension using localhost server?

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

  • There is a chrome native messaging, but since I want to support multiple browsers / OS, it will be too much, because it is only for chrome.

  • Then this appears, prompting you to configure the local server. This is what I did, see below.

I installed a server with Qt with QTcpServer , which uses QTcpSocket on 127.0.0.1 (localhost). But the web extension cannot listen to sockets, only chrome applications can . I have two possible solutions:

  • As a workaround, I could write a small chrome application. The Qt application will talk to the chrome extension through the chrome application (Chrome application support sockets). But I think this method is awkward and not very elegant.

  • On the other hand, I read about socket.io . The idea is this: extending chrome via http requests with socket.io and socket.io through sockets with my desktop application. Is this possible?

What I also tried was a direct connection to the local server with the following code. In my Qt server application, I see that there is a new connection. But I can’t get an answer at all (either my Qt code is wrong, or because extensions cannot listen on 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 ); } 
+7
google-chrome-extension sockets
source share
1 answer

as you already know, extensions may not create direct connections:

Google Chrome Socket Extensions API

Possible Solution

maybe your QT application can serve as a website and you can communicate with it from Javascript:

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

if you can’t serve websites from a QT application, another approach could be to create a “bridge” a little script that can serve as a website for your JavaScript and send messages from a QT application

you will find many examples on websites, an easy way to break into this is to create a small server using node.js to play with it. stackabuse.com/node-js-websocket-examples-with -socket th /

about! and search for the same website origin policy as

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

hope this helps

+2
source share

All Articles