The background.js has a separate “background page” console. You also need to declare it as the background page in the manifest (reference). Go to chrome://extensions/ and enable “Developer mode” then you will see “Inspect views” under your extension. Find and click “background page” and a separate console will appear. You will probably get Uncaught ReferenceError: $ is not defined because you’re not loading jQuery in the background so make sure to include a copy of jQuery with your extension and declare it in the manifest. I suggest this StackOverflow answer.
The only way to access the resulting data is within the done callback. Inside of the if statement or not will not matter. You can have an else statement and display "Off" instead.
.done(function(data) {
if(data._total > 0) {
chrome.browserAction.setBadgeText( { text: "On" } );
}
else {
chrome.browserAction.setBadgeText( { text: "Off" } );
}
});
.fail(function(jqXHR, textStatus, errorThrown) {
chrome.browserAction.setBadgeText( { text: "Err" } );
console.log('Ajax error', { jqXHR, textStatus, errorThrown });
});