1

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

Jamie
  • 11
  • 3
  • Use `chrome.tabs.get(tabId, callback)` – wOxxOm Jan 10 '17 at 20:01
  • @wOxxOm Thanks for the answer... I'll try putting the tabs.get in background.js and deleting the whole part in content_script... But I still have two doubts: first, is the loop with while correct in this case? And second: how to differentiate between the tab existing or not inside the callback? – Jamie Jan 10 '17 at 20:10
  • Are you trying to detect that the tab has actually closed, or that it is no longer displaying the page on which your content script is running? – Makyen Jan 10 '17 at 20:22
  • @Makyen that it has actually closed, like if I clicked on the close button. I've just seen the answer here: http://stackoverflow.com/questions/16571393/the-best-way-to-check-if-tab-with-exact-id-exists-in-chrome, now it works but I have another doubt about it: if I put a variable assignment instead of `console.log(chrome.runtime.lastError.message);` am I assured that the assignment will be fired if and only if the tab with the specified id is no longer existing? – Jamie Jan 10 '17 at 20:29
  • You appear to need to read [Message Passing](https://developer.chrome.com/extensions/messaging). [`chrome.runtime.sendMessage()`](https://developer.chrome.com/extensions/runtime#method-sendMessage) will not send messages to a content script, only to scripts that are running in the background context. You will need to use [`chrome.tabs.sendMessage()`](https://developer.chrome.com/extensions/tabs#method-sendMessage) to send a one time message to a content script. – Makyen Jan 10 '17 at 20:29
  • @Makyen Ahhhhh my fault, I have copied it from somewhere else and did not correct that part. Anyway now it nearly works with tabs.get as specified just above – Jamie Jan 10 '17 at 20:31
  • Do you want to be notified that the tab has closed (have an event listener called upon closure), or detect that a tab is no longer open (e.g. would be used to poll for the tab's existence)? – Makyen Jan 10 '17 at 20:32
  • @Makyen the second one, to detect that it's no longer open. What I want to do is handle the global _running_ variable, that should be changed to false as the tab closes (and consequentially the script stops running). Then _running_ will be used later in backpage. – Jamie Jan 10 '17 at 20:42
  • That sounds more like the job for receiving an event rather than polling. – Makyen Jan 10 '17 at 20:51
  • @Makyen Maybe yes. Now with the answer I posted above it seems to work, but I'd like to be sure if it'll always work as I want – Jamie Jan 10 '17 at 20:56

0 Answers0