0

I just started building a Chrome extension, but I'm not sure how everything works. I want to get a string of data in one method (getTabs) and then return it to the method that called it. This code calls getTabs which creates a string and tries to return it.

function emailTabs() {  
chrome.tabs.getAllInWindow(null, getTabs);
console.log(data); //this is never able to access the string
}

function getTabs(tabs) {
  var data='';
  //build up data... 
  console.log(data); //this works when there's no anonymous function
  return data;
}

How do I get that string back in the emailTabs?

1 Answer 1

2

Assuming that getTabs return something, you can create an anonymous function in getAllInWindow to gain more control over it, then catch the value with something like this :

function emailTabs() {
    chrome.tabs.getAllInWindow(null, function(tabs){
        var str = getTabs(tabs);
        // some code using the string
    });
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks. But when I try accessing data, it just shows up as "Object object", so it's still not getting the value.
I updated my answer, basically you need the function "getTabs" to return something first, then you will be able to catch and assign the value in the getAllInWindow callback (second parameter)
Thanks. It still doesn't work exactly since when I change it to an anonymous function getTabs ends up not getting the tabs that it needs from getAllInWindow. I'll figure something out though.
you need to give getTabs all the parameters it expects, then it will work exactly as if you called "getTabs" directly, I hope this solves your issue
Oh, I see, I need to pass in the parameter into the anonymous function and then into getTabs. Now it works. Thanks!
|

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.