I'm starting out with building a Chrome extension and it looks like most of the functions there are asynchronous that use a callback function, even the ones where one would expect synchronous behavior, like chrome.tabs.getCurrent
.
So, say I want to have a popup for a browserAction, and when the user clicks on a button to transfer some object as a message to background.js. That object should contain, for the purposes of this example, the current's tab URL and a cookie value. Both chrome.tabs.getCurrent(cb)
and chrome.cookies.get(details, cb)
require a callback. How do I collect the data to pass without doing something like the following:
chrome.tabs.getCurrent(
function(currentTab){
var url = currentTab.url;
chrome.cookies.get({...},
function(cookie){
var msg = { "url" : url, "value" : cookie.value };
chrome.runtime.connect().postMessage(msg);
});
}
);