0

I have 2 background scripts one is background.js and other one is background.ts when I build everything inside build file it looks something like this: /build |-background.js |-helper |--background.ts

What I want to achieve is that I want to send chrome message from .js to .ts background - is that communication possible at all? Both of background are working and everything is great except lack of communication between them.

I have tried to send message from background.js to content script and then to background.ts and it seems that is working but content script is depending on tabs, and I would like to have direct communication channel between background scripts.

11
  • Chrome extension can have only one background script Commented Mar 20, 2024 at 10:37
  • Not true: developer.chrome.com/docs/extensions/mv2/… Commented Mar 20, 2024 at 10:39
  • 1
    Sorry, I wasn't clear. I meant there is always only one background context, even if you put multiple script files there, they are all executed in same context. Same as you can put multiple <script> tags on page and they will be all executed in same context. So you don't need any communications API (like chrome.runtime.sendMessage) to exchange data between scripts. How exactly you exchange data between scripts will depend on your project or/and goal, but it's really more like two building bricks of one background script rather than two separate entities Commented Mar 20, 2024 at 10:44
  • Oh thanks for explanation. Well if I receive socket message in backgroun.js with some data, how to access to that data inside other background.ts? I mean if we don't need some communications between them as you said they are in same context - how to access to data from one in another? Commented Mar 20, 2024 at 10:46
  • That depend on what exactly you would like to do with it. If you want to run some function from TS file when you get WS message, you could just import than function into JS file and call in WS message handler. If you need to save message somewhere to later be processed by function from TS file when another event happens, you might need to store it in variable in separate file and import it in both scripts. Commented Mar 20, 2024 at 10:51

1 Answer 1

0

There is always only one background context, even if you put multiple script files there, they are all executed in same context. Same as you can put multiple tags on page and they will be all executed in same context. So you don't need any communications API (like chrome.runtime.sendMessage) to exchange data between scripts. How exactly you exchange data between scripts will depend on your project or/and goal, but it's really more like two building bricks of one background script rather than two separate entities

If you want to run some function from background.ts file when you get WS message, you could just import than function into background.js file and call in WS message handler.

To do this you'll need to add export keyword to function definition in background.ts:

export const funcName = () => {...}
// Or
export function funcName() {...}

And then import it in background.js and use as normal function:

import {funcName} from './path/to/background.ts'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.