How do I write background script code in MV3 (thus non-persistent) in order to always show the page action button without matches
, tabs
and host permissions?
My usecase is I want to access URL of the current active tab in order to feed into its popup for my extension.
Currently my extension is just a browser action popup with no permissions which just allows pasting a URL and analyze it. Of course the next obvious thing is automatically analyze the current URL in the bar from the click of a button. I could have made it so browser action popup would read the current URL on click and show it instead, but pageAction
is obviously more semantically correct place for this.
However I have no idea what kind of perms I need for it work like this:
- extension still needs no required perms (well except for
storage
because it’s required for storing settings and cannot be optional) baseline. - the page action has to be specifically enabled (with all perms it entails) in order to appear in the URL bar through options.
- the background script then checks for availability of the setting and then shows the page action button if it’s enabled with
pageAction.show()
.
I assumed the minimal perms required are storage
and activeTab
, since I don’t want to resort to <all_urls>
(even optional) just to read a single URL string. Neither I want tabs
for this.
I’ve implemented all the perm and option checking/editing logic so it’s just comes down to checking a setting in the background script, however I have no idea what kind of event I have to listen in order to detect a current tab change:
-
runtime.onStartup
doesn’t fit since the current tab can change during the browser session. -
runtime.onInstalled
doesn’t fit since the current tab can change during the lifetime of the extension. -
runtime.onMessage
doesn’t fit since it requires a pre-existing page action/browser action interaction and the hole purpose of this script is to cause page action to show up. -
runtime.onConnect
doesn’t fit since it has the same requirements asruntime.onMessage
but for persistent interactions with other parts.