0

I am working on a chrome extension, and I need information about the active tab (when I say "active", I mean the tab that I am looking at in the current window that is focused).

Using the chrome.tabs api, I should be able to do something like the following to get what I want:

function getActiveTab() {
  var activeTabInfo = {"currentWindow": true, "active" : true};
  return chrome.tabs.query(activeTabInfo,function (tabs) {
    return tabs[0];
  });
}

However, when I log the length of tabs within the callback, I'm getting a length of 0. I modeled this snippet after How to fetch URL of current Tab in my chrome extension using javascript, but can't seem to get it to work.

Any thoughts?

steev
  • 1
  • 1
  • 1
    https://developer.chrome.com/extensions/overview#sync – Daniel Herr Jun 04 '17 at 06:04
  • 3
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Daniel Herr Jun 04 '17 at 06:05
  • Thanks @DanielHerr, those responses definitely helped. Now as a follow-up, what if I _want_ to get a hold of the the return value of an async call in the scope that I presented? Would I have to create some sort of wrapper around the callback in order to be able to do something like the code snippet above? – steev Jun 04 '17 at 16:35
  • Async means it is executed *after* the current scope code runs, as well as its parent callee and so on (i.e. the entire js engine event). – wOxxOm Jun 05 '17 at 20:38

0 Answers0