In an AngularJS app (a Chrome extension) I need to get the title and URL of the current tab, before I can do anything useful. This function takes a callback, which receives the tab in question. But instead of logging the tab object, I want to pass it to a module / service as soon as it's available. How can I do this?
chrome.tabs.query({currentWindow: true, active: true}, function(tabs) {
console.log(tabs[0]);
});
chrome.tabs.query() apparently has no return value.
Edit
I'm stuck. I tried to get your example to work:
After failing, I put in a very simple test-service to show that the rest of the app works. Maybe someone can fix my service in the commented-out block?
You can either use a lazy initialization of AngularJs or create a service that returns a promise to asynchronously resolve the tab info.
Lazy initialization:
chrome.tabs.query({currentWindow: true, active: true}, function(tabs) {
angular.bootstrap(document);
});
Async service with promise:
module.service('Chrome', function($q) {
return function() {
this.getInfo = function() {
var deferred = $q.defer();
chrome.tabs.query({currentWindow: true, active: true}, function(tabs) {
deferred.resolve(tabs[0]);
});
return deferred.promise;
};
};
});
The best approach depends on how much your initialization process depends on the tab vs how much getting this info is important to address an specific part of your extension.