0

popup.js:

chrome.runtime.getBackgroundPage((bg) => {    
     bg.createTabObj();                 
     Volume.init();                                             
     });

background.js:

createTabObj() {
    chrome.tabCapture.capture({
            audio: true,
            video: false
        },
        (stream) => {                                                     
            //do something                                         
        })

As it is, the popup continues to the next second line before the background method finished its process. What's the simplest way to make sure that the background finishes processing the createTabObj methods before the popup continues to Volume.init()? I prefer doing all the calls from the popup instead of a background callback, if possible.

Niko
  • 369
  • 1
  • 3
  • 14
  • I can't find any documentation on createTabObj() for a the Javascript window that is returned from the getBackgroundPage function. Do you have any references for this? – kaladin_storm Apr 04 '21 at 01:37
  • @kaladin_storm Sorry for the confusion, I've updated the question for better clarity. – Niko Apr 04 '21 at 01:42

1 Answers1

1

The second part of the chrome.tabCapture.capture is a callback function. Just call the Volume.init() inside that callback function.

chrome.runtime.getBackgroundPage((bg) => {    
   bg.createTabObj(Volume.init);                                             
});

createTabObj(id, yourcallback) {
    chrome.tabCapture.capture({
            audio: true,
            video: false
        },
        (stream) => {      
            yourcallback();                                               
            //do something                                         
        })

I'm not sure where your id for createTabObj is being passed in though, but I hope I got the general idea across.

kaladin_storm
  • 352
  • 1
  • 7
  • id was just a parameter I've forgot to remove. I'm trying to avoid this back and forth of activating methods from the popup and background, and only use the popup. Does it make sense? or is the callback the only way to do this? – Niko Apr 04 '21 at 02:06
  • the callback is a function that will run after it finishes completing what the function is suppose to do. so if that function performs something asynchronously then the only way to guarantee your code runs after it is done is to call your function in the callback function. – kaladin_storm Apr 04 '21 at 02:11