How do Chrome extensions update toolbar notifications when extension is closed?

248 Views Asked by At

How do extensions like Lastpass and Traitsniper update the extension notification count when the extension is closed? Are there any extension processes that are running even when the extension is closed? I am currently updating this with events in background.js with manifest v3, which does not run when the extension is not open. extensions notifications example

Manifest.json:

{
   "short_name": "test",
   "name": "test",
   "version": "1.3",
   "manifest_version": 3,
   "action": {
      "default_popup": "index.html",
      "default_title": "Open the popup"
   },
   "permissions": ["contextMenus", "notifications", "storage", "tabs"],
   "icons": {
      "512": "logo512.png"
   },
   "background": {
      "service_worker": "./static/js/background.js"
   },
   "content_scripts": [
      {
         "matches": ["<all_urls>"],
         "js": ["./static/js/content.js"]
      }
   ],
   "devtools_page": "index.html"
}

Snippet from background.js which currently is updating the notification count in the extension toolbar:

switch (unreadCount) {
      case 0:
         chrome.action.setBadgeBackgroundColor({
            color: [110, 140, 180, 255],
         })
         chrome.action.setTitle({ title: 'No unread messages' })
         chrome.action.setBadgeText({ text: '' })
         break
      case 1:
         chrome.action.setBadgeBackgroundColor({
            color: '#F00',
         })
         chrome.action.setTitle({
            title: unreadCount + ' unread message',
         })
         chrome.action.setBadgeText({ text: unreadCount.toString() })
         break
      default:
         chrome.action.setBadgeBackgroundColor({
            color: '#F00',
         })
         chrome.action.setTitle({
            title: unreadCount + ' unread messages',
         })
         chrome.action.setBadgeText({ text: unreadCount.toString() })
         break
   }
1

There are 1 best solutions below

0
On

Just wanted to close this out - its actually an issue with Metamasks's extension provider to get access to window.ethereum. https://github.com/MetaMask/extension-provider/

Everything works fine with alarms and badge text updates, even with the extension closed, until I declare the Metamask provider:

let provider = createMetaMaskProvider()

Then, alarms only work when the app is open/visible. Its a bit wild to me why this is the case, I cannot see any errors when debugging.