44

I can't get my head around how to upgrade my chrome extension manifest v2 to v3

I checked https://developer.chrome.com/extensions/migrating_to_manifest_v3 but it doesn't talk about the manifest.json

any idea what it needs to be changed in my manifest here :-

    "name": "My Extension",
    "version": "1.0.0",
    "short_name": "Ex",
    "author": "User",
    "description": "cool chrome ex",
    "browser_action": {
        "default_title": "ex",
        "default_icon": "img/logo.png"
    },
    "options_page": "options.html",
    "options_ui": {
        "page": "options.html",
        "open_in_tab": true
    },
    "background": {
        "scripts": [
            "js/background.js"
        ]
    },
    "permissions": [
        "tabs",
        "background",
        "storage"
    ],
    "icons": {
        "128": "img/logo128.png"
    },
    "content_scripts": [
        {
            "run_at": "document_end",
            "matches": [
                "https://conferfly.com/*",
                "https://meet.google.com/*",
                "https://teams.microsoft.com/*",
                "https://*.zoom.us/*"
            ],
            "js": [
                "js/main.js",
                "js/injected.js"
            ],
            "css": [
                "css/main.css"
            ]
        }
    ],
    "web_accessible_resources": [
        "js/options.js",
        "js/main.js",
        "js/injected.js"
    ],
    "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
} 

thank you in advance


This is for conferfly extension

  • 1
    It says "Replace background.page or background.scripts with background.service_worker in manifest.json. Note that the service_worker field takes a string, not an array of strings." And of course change 2 to 3 in manifest_version. – wOxxOm Aug 08 '20 at 14:47

2 Answers2

102

I was looking for the same thing. Thanks to the help of the user wOxxOm I've figured out what to change inside the manifest file. Here is an example of how to migrate manifest.json from v2 to v3.

First thing is to change the manifest_version key from 2 to 3

//Manifest v2
"manifest_version": 2

//Manifest v3
"manifest_version": 3

As written into the manifest migration guidelines, pageAction and browserAction API will be unified under the action API. This means that you need to change browser_action and page_action keys into action

//Manifest v2
"browser_action": {...}
"page_action": {...}

//Manifest v3
"action": {...}

Background pages and background scripts are deprecated into manifest v3. They are replaced from service workers. This means that the background section of the manifest file needs to be modified in this way

//Manifest v2
"background": {
 "scripts": ["js/background.js"]
}

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

To declare the packed resources that needs to be accessed from the web, the web_accessible_resources needs to be changed in this way

//Manifest v2
"web_accessible_resources": [
        "js/options.js",
        "js/main.js",
        "js/injected.js"
]

//Manifest v3
"web_accessible_resources": [{
 "resources": ["js/options.js","js/main.js","js/injected.js"],
 "matches": [],
 "extension_ids": []
}]

Content security policy in v3 is an object

//Manifest v2
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'" 

//Manifest v3
"content_security_policy": {
 "extension_pages": "script-src 'self' 'unsafe-eval'; object-src 'self'",
 "sandbox": "..."
}

To have more information about you can check this link or this one.

newbiedev
  • 2,607
  • 3
  • 17
  • 65
  • 9
    Great answer! Just for reference, [Manifest v3 no longer supports 'unsafe-eval' in CSP for `extension_pages`](https://developer.chrome.com/docs/extensions/mv3/intro/mv3-migration/#:~:text=MV3%20disallows%20certain%20CSP%20modifications). – Thomas Orlita Jan 13 '21 at 14:19
  • 13
    Wow man. You saved my life. To let anybody else know. Even though the `matches` and `extension_ids` are not yet used, they MUST be present. – Alex.Gunning Jan 24 '21 at 16:23
  • 1
    also background.js file must be moved to the root folder so need to change "js/background.js" to "background.js" – Armand Apr 03 '21 at 12:18
  • 6
    Wow these migration guides are terrible. – andrsnn Apr 26 '21 at 01:20
1

Here you can find a very good explained guide: https://blog.shahednasser.com/chrome-extension-tutorial-migrating-to-manifest-v3-from-v2/

pabloRN
  • 866
  • 10
  • 19