chrome.browserAction.setBadgeText(object details)
is used to set the badge text for a chrome extension. However, if the tabId doesn't exist, Chrome produces the following error using console.error()
:
Unchecked runtime.lastError while running browserAction.setBadgeText: No tab with id: ####.
This becomes a problem when the badge text is set during a page load. If the tab originally exists but is closed by the user, setBadgeText
ends up being called using a non-existent tabId
.
Normally, this can just be prevented by checking chrome.runtime.lastError
in the callback argument of the problematic function as was answered in another question. However, since browserAction.setBadgeText()
has no callback parameter, there seems to be no way to prevent the error.
When tabId
is an integer not representing any tab, the following code still produces the error even though it attempts to...
- catch the error with
try...catch
(which doesn't work sinceconsole.error()
is not thrown). - check
lastError
after callingsetBadgeText
(which doesn't work since the function is asynchronous). - use various
setTimeout
intervals to checklastError
after callingsetBadgeText
(which doesn't work and probably wouldn't be reliable). - add a callback argument to
setBadgeText
(which has been commented out since chrome produces another error when using it since there is no second argument).
var tabId = 5000;
function clearError () {
console.log(chrome.runtime.lastError);
}
try {
chrome.browserAction.setBadgeText({
text: 'text',
tabId: tabId
}/*, clearError*/);
clearError();
setTimeout(clearError, 0);
setTimeout(clearError, 50);
setTimeout(clearError, 500);
}
catch (e) {
console.log('Caught', e);
clearError();
}
Would there be any way to properly check chrome.runtime.lastError
to prevent this error from occurring?