Check if window/tab still exists?

2,357 views
Skip to first unread message

Warren Benedetto

unread,
Mar 12, 2011, 2:48:58 AM3/12/11
to Chromium-extensions
When a user switches to a new window, I want to show an overlay
concealing the content in the tabs they just switched away from. To do
this, I use chrome.windows.getLastFocused() to get the window on top.
I store that in a lastWindowTabs variable in the background script.

When the user switches to another window, I loop through
lastWindowTabs to show an overlay covering the content in all the tabs
they just switched away from. Then I reset lastWindowTabs to the new
window on top. In effect, lastWindowTabs always reflects the state of
the window that was focused *just before* the current one.

So far so good. That works.

Here's the problem:

If the user CLOSES the window they are on, focus switches back to the
other window. However, lastWindowTabs contains an array of tabs for
the window *which is now closed*. When I loop through that array, I
get this error:

chrome/RendererExtensionBindings:119Port error: Could not establish
connection. Receiving end does not exist.

Of course it couldn't establish the connection -- the window is
closed. The tabs don't exist anymore.

What I need to do is to check whether the tab still exists before
sending a message to it.

I tried using tabs.get(), thinking I'd get back undefined and could
use that. And I do get that, in the tabs.get() callback function. But
before that, I get "Error during tabs.get: No tab with id: 32.".

My question is: What can I do to check whether a tab exists? I want
some sort of tab.exists(tabId) that returns a boolean, but there is no
such function.

What are my other options?

Warren Benedetto

unread,
Mar 12, 2011, 4:03:52 AM3/12/11
to Chromium-extensions
Rather than check that the tab still exists, I decided to use the
chrome.tabs.onRemoved() listener to remove the closed tab(s) from the
lastWindowTabs array. That obviated the need for a tabs.exists()
function. Still would be nice to have though.


On Mar 11, 11:48 pm, Warren Benedetto <war...@transfusionmedia.com>
wrote:

Arne Roomann-Kurrik

unread,
Mar 15, 2011, 1:27:25 PM3/15/11
to Warren Benedetto, Chromium-extensions
Nice that you found a workaround - it's what I would have suggested.

I'd also say that the chrome.tabs.get approach would work as well.  An error message gets printed, but your callback still gets called with an undefined value.  You could check for this easily:

chrome.tabs.get(1234, function(tab) { if (!tab) { console.log('tab does not exist'); }});

~Arne


--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.


Reply all
Reply to author
Forward
0 new messages