browser-extension icon indicating copy to clipboard operation
browser-extension copied to clipboard

Migrate extension to Manifest V3

Open lyzadanger opened this issue 2 years ago • 8 comments

At some point, the (Chrome) extension will need to be migrated from Manifest V2 to Manifest V3. More details can be found here

lyzadanger avatar Aug 03 '21 17:08 lyzadanger

Still working through switching from localStorage to chrome.local.storage https://github.com/hypothesis/browser-extension/compare/master...rjbergerud:migrate-extension-manifest-v3?expand=1

Any thoughts about using chrome.sync.storage?

rjbergerud avatar Sep 15 '21 07:09 rjbergerud

Thanks for taking a look at what is involved in the Manifest V3 migration. I think the way we will go about shipping a migration to MV3 is to first make incremental changes which are cross-compatible between MV2 and MV3, and then make the breaking MV2 => MV3 changes.

Any thoughts about using chrome.sync.storage?

The data that the browser extension stores in local storage is a mapping from tab IDs to current Hypothesis extension state. My expectation is that this won't be valid across different browser instances, so it wouldn't make sense to store in sync storage.

robertknight avatar Sep 15 '21 08:09 robertknight

Thanks for the quick turn-around on commenting @robertknight. I'm trying to think about what changes count as cross-compatible between MV2 and MV3. Does the following split seem right to you?

MV3/Chrome 88+

  • chrome.scripting
  • chrome.actions https://developer.chrome.com/docs/extensions/reference/action/

MV2 compatible

  • using chrome.runtime.getURL (as opposed to chrome.extension.getURL)
  • chrome.storage API
  • changes to export EXTENSION_CONFIG to relevant parts of code instead of attaching to window object (service workers don't have access to window object)

rjbergerud avatar Sep 15 '21 08:09 rjbergerud

Yes, that looks right to me.

chrome.storage API

To expand on this: The background script is becoming a Service Worker in MV3, so localStorage is not accessible and we'd have to switch to either IndexedDB or chrome.local.storage. chrome.local.storage seems easier to work with.

robertknight avatar Sep 15 '21 09:09 robertknight

I have a Manifest V3 version of the extension working locally. The remaining work is:

  • Adapt executeScript calls to use the Manifest V3 or Manifest V2 APIs depending on what the browser supports. https://github.com/hypothesis/browser-extension/pull/706 covers most of this.
  • Migrate uses of localStorage to some other storage mechanism since MV3 doesn't support localStorage in the background page
  • Adapt to chrome.browserAction => chrome.action namespace change
  • Adapt the manifest format to the Manifest V3 format when building for Chrome
  • Retest all extension features with an MV3 version of the extension and look for remaining issues.

robertknight avatar Nov 05 '21 09:11 robertknight

I have code locally for these steps:

  • Adapt to chrome.browserAction => chrome.action namespace change
  • Adapt the manifest format to the Manifest V3 format when building for Chrome

The next item to tackle is localStorage usage. Local storage is used to persist the state of whether Hypothesis is active in a particular tab and cache annotation counts.

I wonder whether we could stop persisting this information entirely and instead forget all Hypothesis-related state when the extension/browser is unloaded and then lazily re-created it when a tab is activated by executing a script in the page to determine whether the client is active.

This would have the benefit that the extension's view of the Hypothesis state for a tab couldn't get out of sync with the actual state as easily.

robertknight avatar Dec 03 '21 15:12 robertknight

Did anyone succeed?

aqui1a avatar Aug 23 '22 22:08 aqui1a

I have got a Manifest V3 version of the extension working locally, and most of the required changes have already landed in the repository. I don't know exactly when we'll ship it, but it will be before the Jan 2023 deadline.

robertknight avatar Aug 24 '22 07:08 robertknight

The Chrome extension now uses Manifest V3 when the manifestV3 key is set to true in the config file used to build the extension. See chrome-prod.json for an example.

We're currently just waiting on Chrome Web Store review to switch the production extension to MV3.

robertknight avatar Apr 17 '23 10:04 robertknight

The first Manifest V3 build of the extension has been released to users (1.1252.0.7), so we can close this. There is some future cleanup to do to remove Manifest V2 support when we're sure we don't need it any more. I filed https://github.com/hypothesis/browser-extension/issues/1210 for this.

robertknight avatar Apr 19 '23 05:04 robertknight