Derokorian;11038745 wrote:
chrome.runtime.sendMessage("checkActive", function(response) {
Can one message receive several responses? Or just one?
Then in my background page, I'm adding a listener. I can successfully check if the tab is active, however I can't seem to figure out how to include if the window is active in my check. Here's what I've got:
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
if (request == "checkActive") {
var bRet = sender.tab.active;
chrome.windows.getCurrent({}, function(oWin) {
// this refers to bRet in the enclosing scope, i.e. 2 lines above
bRet = (oWin.focused && bRet);
console.log('this is logged second');
sendResponse(bRet); // This never gets to the content page
});
/* This is still inside the closure. If you wait here long enough for the callback to
* be executed, bRet should change value depending on oWin. And the order of
* the console.logs should change accordingly.
* But if my guess about a tab only being considered active if its window is active
* then you'd never see a difference since you'd have
* true (tab) && true (window always true)
* tab (false) && whatever
* and the whole expression can be simplified to: tab.active
*/
console.log('this is logged first');
sendResponse(bRet); // this gets to the content page, but not updated with oWin.focused
}
}
);
Derokorian;11038745 wrote:
What am I missing? I assume it has something to do with my lack of understanding on scopes, callbacks, and the like 🙂
If you wish to separate the inner callback from the current closure, you may do so like this
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
"use strict";
if (request === "checkActive") {
var bRet = sender.tab.active;
/* Anonymous self executing function taking one argument, bRet
* This creates a new lexing scope, inside which bRet is local
* and not an upvalue. You could even change the parameter name
* from
* (function (bRet) {}(bRet));
* to
* (function (bRetLocal) {}(bRet));
* if you find it easier to keep track of which is which in this way.
*
* Also, this
* (function (param) {})(arg);
* is the same as this
* (function (param) {}(arg));
* Except that the latter form is required by jslint
*/
(function (bRetLocal) {
chrome.windows.getCurrent({}, function (oWin) {
bRetLocal = (oWin.focused && bRetLocal);
// since you do get both logs in the console, add the activity status there for inspection
console.log('this is logged second: ' + (bRetLocal ? 'true' : 'false'));
sendResponse(bRetLocal); // This never gets to the content page
});
}(bRet));
// since you do get both logs in the console, add the activity status there for inspection
console.log('this is logged first: ' + (bRet ? 'true' : 'false');
sendResponse(bRet); // this gets to the content page, but not updated with oWin.focused
}
}
);
If you wish to get more information about what is active and not, try
// First check
var bRet = sender.tab.active ? 'tab' : '';
// Second check, inside the callback
bRetLocal += oWin.focused ? 'window' : '';
Setting breakpoints and inspecting the code as it is executing usually helps gain a better understanding of what is happening.
Finally, would it work to use chrome.tabs.query({ active : true, currentWindow : true }) to get the currently active tab of the current window and check if that tab matches your needs?
Hopefully something helped in some way.