Skip to content Skip to sidebar Skip to footer

Unable To Check Runtime.lasterror During Browseraction.setbadgetext

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 usi

Solution 1:

An option would be to call chrome.tabs.get first and if no error is called assume that the tab will exist for the next few milliseconds.

var tabId = 5000;
functioncallback(tab) {
    if (chrome.runtime.lastError) {
        console.log(chrome.runtime.lastError.message);
    } else {
        chrome.browserAction.setBadgeText({
            text: 'text',
            tabId: tabId
        });
    }
}
chrome.tabs.get(tabId, callback);

There is of course always a chance the tab could get closed between tabs.get finishing and setBadgeText getting called but it's very unlikely.

Solution 2:

In addition to chrome.tabs.get() as described above, you can also piggyback off an existing browserAction method that accepts a callback to catch for errors. Example:

var tabId = 5000;
chrome.browserAction.getTitle({tabId: tabId}, function(result) {
  if (chrome.runtime.lastError) return;
  // The coast is clear...
  chrome.browserAction.setBadgeText({
    text: 'text',
    tabId: tabId
  });
});

Good news: chrome.browserAction.setBadgeText() should also support a callback function like chrome.browserAction.getTitle() starting with Chrome 67 according to one source. Fingers crossed!

Solution 3:

A bug was reported for this issue and has been marked as fixed: https://crbug.com/451320

Post a Comment for "Unable To Check Runtime.lasterror During Browseraction.setbadgetext"