I am building a simple chrome extension that will only work on a specific domain, and I'm now facing the problem of detecting a tab's close in background page. As I have read around, there is no specific way to achieve it, but window.unonload and window.onbeforeunload can help detecting page variatons (though they also get fired if the page is reloaded). However, since the extension is programmed to work in the same tab but it has to reload the page many times, it would be a bad solution for my case. So this is what I thought:
1-at the very beginning of his execution, the content script sends a message to the backpage, which will now be able to detect the sender tab id and store it to a global variable.
2-then, the backpage should programmaticaly check if a tab with that id is currently open, for example in a while loop or something like it.
3-as the control returns false, the backpage will know that the page has been closed and the extension isn't running.
This one seems to be a simple and short way to check if a page is open, but I can't find a way for the backpage to determine if a tab with a certain id is open. I've also read chrome documentation (https://developer.chrome.com/extensions/tabs) but it sounds like there isn't a specific function to do this. So what I'm wondering about is: what might be a work-around to solve this problem? Might message passing be helpful? P.S. I'm not using jquery.
Thanks for help.
*EDIT:** I forgot to say that I have already tried this code but it's not changing the running variable background.js
var running;
while (0!=1){
chrome.runtime.sendMessage(idtab, {quest: "running"}, function(response) {
if (response.ans!="yep"){
running=false;
}
});
}
content_script
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.quest=="running"){
sendResponse({ans:"yep"});
}
});
-the idtab variable in background is defined before and it already contains the id of the tab in which the script is workin