How To Migrate Chrome Extensions From Manifest V2 To V3 Version?


Extensions have always been a field for innovation and enhancing the browser’s usability. If you are a web developer, you will certainly be aware of the word “manifest,” which plays an important role with extensions. Google has updated its extension platform to reflect the future of browser extensions.The Manifest V3 version has many changes, but moving from persistent background scripts to service workers is the biggest change. In this blog you will learn all the steps to migrate chrome extensions from manifest V2 to V3 version.

What Has Changed In Manifest Version V3?

There are so many changes in the Manifest V3 version. Some of the major changes are:

  • Moving from a persistent background script to a service worker 
  • There is no longer use of <iframe> elements or other DOM APIs from the service worker.
  • All APIs have become Promise-based.

Step By Step Guide To Migrate From Manifest V2 To V3 Version

This process of migrating from Manifest version 2 to version 3 is essential to make extensions better tools and to have a stronghold of privacy and security. Here is a step-by-step guide to help you carry on with the migration process.

Manifest .JSON File Updation

To access the new features in Manifest V3, you must update your manifest file. However, there are additional changes such as host permissions, service worker, content security policy and action declarations that will also require manifest updates.

Changing The Manifest Version 

To update your extension, change the value of the “manifest version” element. This decides whether you’re using the Manifest V2 or Manifest V3 feature set.

Background Pages ( Also Known As Service Worker In V3 Version) 

In Manifest V3, background pages are now service workers. You have to register the service worker under the “background” field, which will use the “service_worker” key which specifies a single JavaScript file. Even though Manifest V3 does not support multiple background scripts, the service worker can alternatively be declared as an ES Module by specifying “type”: “module,” which enables you to import further code.

Specifying Host Permissions

You must declare host permissions and optional host permissions independently from other permissions in Manifest V3. Content scripts are unaffected by the relocation of the match patterns to “host_permissions”. Content script match patterns remain under “content_scripts.matches”.

Extension’s CSP (Content security policy)

The content security policy (CSP) for an extension was defined as a string in Manifest V2, but in Manifest V3 it is an object with members denoting various CSP contexts like “extension_pages” which covers pages in your extension, including html files and service workers and “sandbox” which covers any sandboxed extension pages used by your extension. 

API Unification

Actions could be implemented using either the “browser_action” or the “page_action” APIs in Manifest V2. When these APIs were first introduced, they each had a specific purpose, but over time they grew redundant. Due to this, in Manifest V3, they are combining them into a single “action” API.

Providing Array of Objects 

This modification limits access to extension resources to particular websites or extensions. An array of objects, each of which can map a collection of resources to a set of URLs or extension IDs, is now provided instead of a list of files. Previously, all websites and extensions were included in the list of web resources. This made it possible for resource access without authorization and fingerprinting. Thanks to the revised API, a tighter control over which websites or extensions can access extension resources is now possible.

Execution of Code 

Through a combination of platform modifications and policy limitations, Manifest V3 imposes new restrictions that restrict an extension’s ability to execute unreviewed JavaScript.This modification does not affect many extensions. However, you will need to adjust your code execution techniques when moving to Manifest V3 if your Manifest V2 extension runs remotely hosted scripts, inserts code into pages, or evaluates strings at runtime.

Restriction On Remotely Hosted Codes 

Any code that is a loadable resource but is not part of an extension’s package is referred to as remotely hosted code, like the library hosted on a CDN and JavaScript files pulled from the developer’s server. All of your extension’s logic must be present in the extension for Manifest V3 to work. It is no longer possible to load and run a file hosted remotely. Depending on your use case and the reason for remote hosting, a number of alternative approaches like

configuration-driven features and logic, externalize logic with a remote service, and bundle third-party libraries are available. 

How To Execute Arbitrary String

Using “tabs.executeScript()” and the code attribute on the options object, it was possible to run any string of code in Manifest V2 at any time. Manifest V3 restricts the execution of arbitrary code. Developers of extensions can use the “scripting.executeScript()” function to inject a static file or a function. You must include the “scripting” permission in your manifest file in order to use the Scripting API, and this API does not  trigger a permission warning.

How To Inject Static File 

Moving from manifest V2 to V3, Insertion of static files by “scripting.executeScript()” is essentially the same as how it worked in the Tabs API. The new method now accepts an array of files, whereas the old method only accepted a single file.

You Can Also Check Out This :- Color Picker

How To Inject Function 

If you require more dynamic functionality, you can inject a function as a content script using the new “func” property and pass variables using the “args” property. The function’s source is sent to the target tab, where it is executed, instead of being run as if it were in the content script.

What Is Service Workers 

In Manifest V3, the background pages of Manifest V2 are replaced by service workers. This foundational modification affects most of the extensions. When you can use a persistent page and have access to the DOM in Manifest V2, you don’t have the access to DOM and the page gets terminated when not in use in Manifest V3.

How To Modify Network Request

Once you move Chrome extension from manifest V2 to V3 version, the blocking version of the Web Request API must be replaced with the new Declarative Net Request API for extensions that modify network requests. This new API was created to optimize an extension’s ability to block network requests without requiring the extension to have permissions and to integrate well with the event-based execution model of service workers.

Do Not Forget To Download This Amazing Extension:- Best Free HD Screen Recorder

Give your Comments

  • No comments yet.
  • Add a comment

    Related Articles