Migrating Manifest V2 to V3
This blog lists changes being implemented as a part of Manifest V3, the next version of the Chrome Extensions platform. There are a lot of recent features and functional changes for extensions using Manifest V3.
As chrome documentation says:-
Extensions using MV3 will enjoy enhancements in security, privacy, and performance; they’ll also use contemporary Open Web technologies adopted in MV3, like service workers and promises.
# Updating the manifest.json file
In Manifest Version 3 background script is replaced by Service Worker. Write the “service_worker” under the "background" field
Manifest V3, which does not support more than one background script, if you want to add more, you can optionally declare the service worker as an ES Module by defining "type": "module", which allows you to import further code.
Service Worker One more way from which you’ll be able to include multiple files and call them from a service-worker.Js is to use the importScripts( ) method at the beginning of the service-worker.js script like this:-
In Manifest V3, you have to specify host permissions and optional host permissions separately from other permissions.
In an earlier version, actions were divided into “browser_action” and “page_action” keys. In Manifest V3, they’re unified into a single field named Action. This can be often done to scale back confusion between those two fields as they ultimately serve a common purpose.
Here’s the V3 manifest: -
It is a layer of security for the application. It helps you define all the origins from which you're loading any styles, scripts, or data. For every style, script, font, or connection, you have to specify the domains which you are loading in your application.
In MV2, we define “content_security_policy” as a string.
In Manifest V3 it’s an object with members representing alternative CSP contexts.
extension_pages: This covers pages in extension, including HTML files and service workers.
Sandbox: This covers any sandboxed extension pages that the extension uses.
Additionally, MV3 has put out of action certain Content Script Policy modifications that were allowed in MV2-
The “script-src”, “object-src” and “worker-src” may have only one of the resulting values:
To declare the packed resources that require to be accessed online, the web_accessible_resources will now not need an inventory of files. Instead, you’ll now provide a list of objects, each of which maps to a collection of resources like a group of URLs or extension IDs:
In Manifest V2 the object should be like:-
In Manifest V3 the object should be like:-
#API’S DEPRECIATED IN MV3
chrome.declarativeWebRequest
chrome.extension.getExtensionTabs()
chrome.extension.lastError
chrome.extension.sendRequest()
chrome.tabs.getAllInWindow()
chrome.tabs.getSelected()
chrome.tabs.onActiveChanged
chrome.tabs.onHighlightChanged
chrome.tabs.onSelectionChanged
chrome.tabs.sendRequest()
chrome.tabs.Tab.selected()
chrome.extension.connect()
chrome.extension.onConnect
chrome.extension.onMessage
chrome.extension.sendMessage()
chrome.extension.getURL()
chrome.extension.onRequest
chrome.extension.onRequestExternal
There are many changes between Manifest Version 2 and Manifest Version 3, and a lot of new features and functions are also added in Manifest Version 3 and many APIs which were supported in Manifest Version 2 are depreciated in Manifest Version 3.
One of the major changes is that the background script is being replaced by a service worker.
A new declarativeNetRequest API is introduced to handle network request modification.
Promise support has been added in Manifest Version 3.
In manifest version 3, an extension can only execute the javascript file which is included in the package; i.e. we cannot remotely host code.