Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The best way to check if Tab with exact ID exists in Chrome

Tags:

Sometimes there is tab Id stored in a variable and you need to check if tab still exists before doing something with it (because users can close tabs at any time). I've found this solution:

chrome.tabs.get(1234567, function(tab) {   if (typeof tab == 'undefined') {     console.log('Tab does not exist!');   } }); 

It works but it has quite serious disadvantage. It writes error message into console like this:

Error during tabs.get: No tab with id: 1234567.

And this is not an exception. So try/catch can't help. It's just a message in console.

Any ideas?

UPDATE: This error now looks like "Unchecked runtime.lastError while running tabs.get: No tab with id: 1234567."

like image 710
Konstantin Smolyanin Avatar asked May 15 '13 17:05

Konstantin Smolyanin


1 Answers

function callback() {     if (chrome.runtime.lastError) {         console.log(chrome.runtime.lastError.message);     } else {         // Tab exists     } } chrome.tabs.get(1234,callback); 

source Chrome Extension error: "Unchecked runtime.lastError while running browserAction.setIcon: No tab with id"

Edit:

Chrome checks if the value of chrome.runtime.lastError was checked in a callback, and outputs a console message for this "unhandled async exception". If you do check it, it won't pollute the console.

From the comment by @Xan

like image 152
anglinb Avatar answered Oct 20 '22 19:10

anglinb