I am developing an extension that allows the user to set preferered languages and a user agent string for a container (contextual identity).
In MV2, I used an approach where I define a dynamic content script string which includes custom languages and a user agent string, for each container.
Question 1
With nonpersistent background pages in MV2, however, how can I register and unregister content scripts dynamically? Since variables are not saved in background pages, content script objects needed to unregister associated content scripts later, cannot be retained, I think. They are complex objects, so they do not seem to be storable in storage API.
Question 2
With MV3, dynamic content script strings do not seem to be possible. So we need to get overrides strings (languages and a user agent string) from storage, in a static content script. To get the strings, the content script needs to know the cookieStoreId (the ID of the contextual identity or container the tab resides).
The flow, I think, is the following:
- the content script loads at
document_start
. - the content script asks the background script for the cookieStoreId of the tab. (asynchronously)
- the content script gets the overrides strings from storage, using the cookieStoreId as the key. (asynchronously)
- the content script sets the preferred languages and the user agent string, for the tab.
Isn’t this too late for a web page to get languages and user agent information? I suppose that a web page determines the preferred languages and the user agent string, when the script on the page starts.
I am very confused. What is the best practice for what I am trying to achieve?
Thanks.