2

I am trying to use Socket.io in my Chrome extension, but for some reason I can't get it to work. The following is the code I have in my background.html:

<!doctype html>
<html>
<head>
    <script src='socket.io.js'></script>
    <script>
    var socket = io.connect('http://localhost:8080/');

    chrome.browserAction.onClicked.addListener(function(tab) {
        socket.send('hi');
    });
    </script>
</head>
<body>
</body>

And my manifest.json I have the following for loading the background page and some permissions:

"permissions": [
"http://*/",
"http://*/*"
],

"background": {
"page": "background.html"
},

This does not work however. When the extension is loaded, it should connect to the Socket.io server, but it doesn't. I am unable to figure out what is causing this. This works perfectly when I simply run the background.html file. Any help is greatly appreciated!

Lazze
  • 323
  • 2
  • 7
  • 17

3 Answers3

3

According to the Manifest v2 "migration guidelines" you should "remove JS code contained within tags and place it within an external JS file".

Inline JS is not executed at all, so place it in external JS files and then either import them in background.html or use the scripts attribute of the background attribute in manifest, e.g.:

"background": {
    "scripts": [
        "socket.io.js",
        "background.js"
    ]
}
gkalpak
  • 47,844
  • 8
  • 105
  • 118
0

Try Using its CDN - https://cdn.socket.io/socket.io-1.0.6.js, and in the manifest.json file add

"content_security_policy": "script-src 'self' https://cdn.socket.io; object-src 'self'"

Worked for me.

-4

socket.io.js isn't a file on the filesystem, it's supposed to be served by the server of your choosing. In your case, localhost:8080.

dsp_099
  • 5,801
  • 17
  • 72
  • 128