I have a minor problem understanding jQuery deferred and promise which I would like to use for a Chrome extension I am developing for the first time. I want to be able to detect within the popup wether the current tab is Facebook.com or not. I know there might be simpler ways but I would like to learn deferred and promises along the way. It is just a test project. Sorry for my bad English and every comment is much appreciated! Thanks in advance. Below you will find my code...
function getCurrentTabUrl(callback) {
// https://developer.chrome.com/extensions/tabs#method-query
alert("hmm");
var dfd = $.Deferred();
var queryInfo = {
active: true,
currentWindow: true
};
chrome.tabs.query(queryInfo, function(tabs) {
alert(tabs[0].url);
if(callback(tabs[0].url)) {
dfd.resolve();
} else {
dfd.reject();
}
});
return dfd.promise();
}
document.addEventListener('DOMContentLoaded', function() {
var callback = function getCurrentTab(tab){
alert("ök");
if(tab == "https://www.facebook.com/") { return true; }
else { return false; }
}
getCurrentTabUrl(callback).done(function () {
alert('on facebook');
$('.section').hide();
$('.navitem').on('click', function () {
var value = $(this).html().toLocaleLowerCase();
$('.section').hide();
$('#'+value).show();
});
}).fail(function () {
alert('not on facebook');
});
});