Unable to check runtime.lastError during browserAction.setBadgeText

前端 未结 3 434
眼角桃花
眼角桃花 2020-12-11 22:45

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 e

3条回答
  •  误落风尘
    2020-12-11 23:25

    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!

提交回复
热议问题