DEV Community

Ericawanja
Ericawanja

Posted on

Google chrome extension manifest v3 for beginners

Manifest file uses JSON format to describe the important information about the extension. It contains fields that are required, recommended while other are optional depending on the extension you are building.

  • name refers to the name of the extension and should be up to 45 characters.
  • version digits separated by dots showing the extension version. For example; 1.0.0
  • manifest_version version of the manifest. The latest is 3
  • host_permissions It is a group of URLs which the extension needs extra privileges. you must specify the host permissions in manifest v3 and higher.
{
     "name": "My YT Bookmarks",
    "version": "0.1.0",
    "description": "Saving timestamps in YT videos",
    "permissions": ["storage", "tabs"],
    "host_permissions": ["https://*.youtube.com/*"],
    "manifest_version": 3
}
Enter fullscreen mode Exit fullscreen mode

The recommended fields are;

  • description- brief details on the functionality of the extension
  • action action key allows us to use the chrome.action API to control the toolbar for your extension in chromes UI. It also allows us to specify the objects of default icons, the default title and the default popup html.

 "description": "Saving timestamps in YT videos",
"action": {
      "default_icon": {
        "16": "assets/ext-icon.png",
        "24": "assets/ext-icon.png",
        "32": "assets/ext-icon.png"
      },
      "default_title": "My YT Bookmarks",
      "default_popup": "popup.html"
    },
Enter fullscreen mode Exit fullscreen mode

The optional fields are;

  • author it is a string of the authors name
{
...
 "author": "Erica",
...
}
Enter fullscreen mode Exit fullscreen mode
  • background background scripts interact with the events through the Chrome API. These scripts are registered as an object using the service_worker key.
{
...
 "background": {
      "service_worker": "background.js"
    },
...
}
Enter fullscreen mode Exit fullscreen mode
  • permissions – extensions may require various permissions such s to use chrome storage to function properly. The permissions required are passed as an array of strings.
{
...
 "permissions": ["storage", "tabs"],
...
}
Enter fullscreen mode Exit fullscreen mode
  • web_accessible_resources it is an array of objects that declare the various resource such as images or any other asset needed by the extension
{
...
 "web_accessible_resources": [
      {
        "resources": [
          "assets/bookmark.png",
          "assets/play.png",
          "assets/delete.png",
          "assets/save.png"
        ],
        "matches": ["https://*.youtube.com/*"]
      }
    ],
...
}
Enter fullscreen mode Exit fullscreen mode

content_scripts are scripts that can manipulate the page’s DOM and run in context of a particular page.

{
...
 "content_scripts": [
      {
        "matches": ["https://*.youtube.com/*"],
        "js": ["contentScript.js"]
      }
    ],
...
}
Enter fullscreen mode Exit fullscreen mode

If the script is using a JavaScript library, then it should also be injected.

{
...
 "content_scripts": [
      {
        "matches": ["https://*.youtube.com/*"],
        "js": [jquery, "contentScript.js"]
      }
    ],
...
}
Enter fullscreen mode Exit fullscreen mode

Final remarks

There is much more to do with chrome extensions, but this is a great starting point. We will be looking on all other files in details soon

Top comments (0)