google-chrome-extensioncontent-script

How to run content scripts after user is authorized in a google extension with react?


I'm trying to build a chrome extension with React.js. All the logic regarding authentication is done through react using a back-end service (in spring java). The authentication is done using api calls to the servers as in a single page application.

I am using the Manifest V3 I want to run a content script if the authorization is done with success in the extension. For that I sent a message to the service worker and the service will store in the local storage a flag stating that the user is login. Why i did that? Because when the user refresh the web page I want the content script to run if the login is already done.

The problem is that when token/cookie expire at the extension level the content script will still run because the state of the service worker is not updated until the extension popup is opened.

Is there any way to update the service worker state when the token/cookie expire in the popup? There is any better solution how to run a content script only if a user is authenticated ( without doing api calls at the service worker level)?

I've tried to do chrome api call to the react part but all the api provided are for manifest v2.


Solution

  • Update: Because no better solution was proposed, I fixed this by calling the backend service with the cookie generated by the authentication process from the service worker. ( On the service worker you have access to the cookies or the storage).

    I do not like this particular solution but does the job.