0

I'm making a chrome extension that read a current tab's content and does a heavy task on backend. What if I close a tab during its process? I'd like to allow a user to close tab without waiting for a process to finish. My code is like below. It is written in React and Typescript. Thank you in advance.

import React from 'react';

export const PopupPage = () => {
  const doTask = async(event: React.MouseEvent<HTMLButtonElement>) => {
    chrome.tabs?.query({
      active: true,
      currentWindow: true
    }, async tabs => {
      const currentTab = tabs[0]
      const currentUrl = currentTab.url
      const currentTitle = currentTab.title
      if (currentUrl === undefined) {
        return
      }
      const creationOptions = {
        type: 'basic' as chrome.notifications.TemplateType,
        title: `Nice title`,
        message: `Start a heavy process`,
        iconUrl: './logo.png',
        contextMessage: 'You can close the tab now.'
      } as chrome.notifications.NotificationOptions<true>;

      chrome.notifications.create(currentUrl, creationOptions)
      
      // Send a url and title to backend and wait for it finishing.
      await executeHeavyTaskAndWaitForResponseFromBackend(currentUrl, currentTitle)

      const updateOptions = 
        {
          type: 'basic' as chrome.notifications.TemplateType,
          title: 'Nice Title',
          message: "COMPLETED",
          iconUrl: './logo.png'
        }
        // I want to be sure this notification is called even after the tab is closed
        chrome.notifications.create(currentUrl, updateOptions)
    }); 
  }
  return <button onClick={doTask}>Do a heavy task</button>
}
Watanabe.N
  • 1,549
  • 1
  • 14
  • 37

1 Answers1

1

I think what you're looking for is a background service worker, it listens on events (you can trigger it in your doTask function) and will not unload until it goes idle (finished all tasks).

You can load the service worker by calling runtime.getBackgroundPage or via sending a message (message passing).

  • Thanks. So is it impossible in an action? I've searched but couldn't find relevant resources about closing tab during a process. – Watanabe.N Dec 30 '21 at 04:42
  • Yes, you can trigger it in an action. Closing a tab won't affect a service worker. However, if you mean terminating the whole browser, you should probably look at [Does service worker runs on background even if browser is closed?](https://stackoverflow.com/questions/39201067/does-service-worker-runs-on-background-even-if-browser-is-closed) But you can simply try it out if a service worker will keep a background process running after window closing. If the worker terminates, you could also persist states by using the storage API. – krautgortna Dec 30 '21 at 13:16
  • Thank you. I confirmed when closing popup or switching between tabs, popup process gets terminated (by seeing if it changes badgetext even after closing tabs). I'll use Event Page available from Manifest V3 – Watanabe.N Dec 31 '21 at 04:08