Microsoft Edge extension manifest v2 migration to v3

Summary

Microsoft Edge extensions are required to migrate from Manifest V2 to V3 by June 2023 due to new security and privacy enhancements. Key changes involve updating the manifest_version to 3, separating permissions into distinct `permissions` and `host_permissions` arrays, and transitioning background scripts to `service_worker`. Developers must also replace `page_action` with `action` in the manifest. Furthermore, within service workers, `XMLHttpRequest` is no longer supported and should be replaced with `fetch()`, while `window.open()` for new tabs is superseded by `browser.tabs.create()`. It is recommended to consult Microsoft's official checklist and debug locally to address any further migration complexities.

In June 2023, Microsoft Edge would not allow Edge extensions with manifest version before v3 to be listed on Microsoft web stores as v3 extensions add more security privacy related restrictions on some of the functions. Hence all extensions with manifest v2 must migrate to v3. In this post, we would show how we did the migration for one of our extensions and the changes made.

There is a checklist provided by Microsoft Edge team on what needs to be updated so that the extension can still work in v3. It can be checked before your migration starts.

The first change needs to be made is to update the manifest_version from manifest.json. It needs to be updated from 2 to 3.

// v2
"manifest_version": 2,

// v3
"manifest_version": 3,

Next, the permissions section may need to be updated to separate normal permissions with host_permissions where some hosts are specified so that they can be accessed from the extension.

// v2
"permissions": [
  "contextMenus", 
  "declarativeContent",
  "notifications",
  "storage",
  "tabs",
  "*://www.pxlet.com/*"
],

// v3
"permissions": [
  "contextMenus", 
  "declarativeContent",
  "notifications",
  "storage",
  "tabs"
],
"host_permissions": [
  "*://www.pxlet.com/*"
],

Another big change is the background script now is changed to service_worker for more security restrictions.

// v2
"background": {
  "scripts": ["background.js"],
  "persistent": false
},

// v3
"background": {
  "service_worker": "background.js"
},

page_action is now replaced with action.

// v2
"page_action": {
  "default_popup": "popup.html",
  "default_icon": {
    "16": "image/logo.png",
    "32": "image/logo.png",
    "64": "image/logo.png",
    "128": "image/logo.png"
  }
}

// v3
"action": {
  "default_popup": "popup.html",
  "default_icon": {
    "16": "image/logo.png",
    "32": "image/logo.png",
    "64": "image/logo.png",
    "128": "image/logo.png"
  }
}

Post manifest.json update, there might be other changes needed in background script since some components are not available in service_worker.

XMLHttpRequest is not defined

In service_worker, the previous supported XMLHttpRequest is not defined now, to make async request, fetch() should be used.

// v2
let xhr = new XMLHttpRequest();
xhr.open("POST", "https://www.pxlet.com/lib/api/post.php", true);
xhr.onreadystatechange = function() {
    if (xhr.readyState === 4 && xhr.status == 200) {
        // ...
    } else {
        console.log(xhr.readyState);
        console.log(xhr.status);
    }
};
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
let data = "somedata";
xhr.send(data);

// v3
let data = "somedata";

fetch("https://www.pxlet.com/lib/api/post.php", {
    method: "POST",
    headers: {
        "Content-type": "application/x-www-form-urlencoded"
    },
    mode: 'cors',
    credentials : 'include',
    body: data,
}).then((response) => {
    response.text().then((data) => {
        // ...
    });
}).catch((error) => {
    console.log(error);
})

window object is not defined

When opening anew tab, can use window.open() previously, with v3, this will not work anymore, instead one should use browser.tabs.create().

// v2
window.open(link, '_blank');

// v3
browser.tabs.create({ url: link });

With all changes made, can load the unpacked package in Microsoft Edge and it should be loaded properly, otherwise there would be errors shown and can debug from there.

So far these are all the changes we have made based on the extension we have. There might be other changes needed for more complicated extensions. For those, can further check the official checklist for v3 migration or checking the errors when loading the updated extension in local environment.

EXTENSION MANIFEST V3 MICROSOFT EDGE

  RELATED

  COMMENTS

0

No comment for this article.