2

Yes, there is a similar question to this already has already been answered (Chrome Extension Socket io node js); however, it only works for background scripts (from what I've seen). So, how would I do this in the content script?

Here is what I have so far:

// manifest.json
// And yes, I've also tried to import "socket.io.js" as a background script

"content_scripts":
[
  {
    "matches": ["<all_urls>"],
    "js": ["socket.io.js", "index.js"]
  }
]
// index.js

const socket = io('http://localhost:8080');

socket.on('some-event', (data) => {
  ...
};

//  index.js:1 Uncaught ReferenceError: io is not defined
//    at index.js:1

What am I doing wrong?

Thanks.

Plot
  • 31
  • 7
  • 2
    You can't. Modern Chrome doesn't allow cross-origin requests in the content script. See [How to stop CORB from blocking requests to data resources that respond with CORS headers?](https://stackoverflow.com/a/55292071) – wOxxOm Sep 23 '20 at 11:48
  • A workaround is to add an extension frame in the current tab. That frame will be able to do everything the background script can. Look for examples of using extension iframes with web_accessible_resources. – wOxxOm Sep 23 '20 at 11:49

0 Answers0