0

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
   }
  • The background script is not related to the UI of the extension. It runs separately in a hidden background context. Add your code and manifest.json to the question ([MCVE](/help/mcve)). – wOxxOm Jun 07 '22 at 07:41
  • Thanks @wOxxOm - I have updated my original post. I incorrectly thought the background worker was able to run when the extension was closed. Thats the main issue i'm facing, how to run some code while the extension is not open. – cryptoKevin Jun 07 '22 at 15:54
  • The background script is event-based, so you need to place this code inside a listener for such an event. See the [documentation](https://developer.chrome.com/docs/extensions/mv3/migrating_to_service_workers/) for more info. – wOxxOm Jun 07 '22 at 17:09
  • thanks @wOxxOm - I have setup events in the background script, timer events work fine, but the events do not fire when the extension is closed, and simple counters are reset to 0 each time the extension opens/closes. My main issue is having events, or some sort of code running at all times, even when the extension is not open. I'm just asking here if some core part of Chrome extensions can run 100% of the time (even when extension is not active). – cryptoKevin Jun 07 '22 at 17:22
  • Timers aren't such events. You can use chrome.alarms API. See also [Persistent Service Worker in Chrome Extension](https://stackoverflow.com/a/66618269). – wOxxOm Jun 07 '22 at 19:11
  • @wOxxOm - I found the issue with why alarms were dying, just haven't solved it. I am using Metamask's provider to get access to window.ethereum in my extension. Badge text updates work fine on alarm even with the extension closed, but when I declare my MM provider, alarms break when the app is closed https://github.com/MetaMask/extension-provider/ – cryptoKevin Jul 07 '22 at 20:39

1 Answers1

0

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.