配置文件manifest.json详解

445 阅读1分钟

目录:

1、配置文件:manifest.json

基础配置

{
    "manifest_version": 3,
    "name": "test extension",
    "author": "yong cai",
    "developer": {
        "name": "yong cai",
        "url": "https://nanjingcaiyong.github.io/"
    },
    "version": "1.0",
    "description": "my first chrome extesion",
    "permissions": [
        "tab",
    ],
    "background": {},
    "action": {
        "default_popup": "popup/popup.html",
        "default_title": "AB Extension"
    },
    "content_scripts": [{}],
    "web_accessible_resources": [],
    "icons": {},
    "commands": {
        "my-command": {
            "suggested_key": {
                "default": "Ctrl+Shift+A",
                "mac": "Ctrl+Shift+A"
            },
            "description": "My Command Description"
        }
    },
    "options_page": "options.html",
    "options_ui": {
        "page": "options.html",
        "open_in_tab": false
     },
}

所有配置字段参考MDN

action

定义用户点击扩展程序的操作图标时发生的情况。操作可以使用 Action API 调用扩展程序功能,也可以打开一个弹出式窗口。或者提示告诉用户操作的用途。

commands

{
     "commands": {
        "my-command": {
            "suggested_key": {
                "default": "Ctrl+Shift+A",
                "mac": "Ctrl+Shift+A"
            },
            "description": "My Command Description"
        }
    }
}

在 background.js 监听

chrome.commands.onCommand.addListener(function(command) {
  if (command === "my-command") {
      // 执行命令触发时的操作
      console.log('=====>')
  }
});

options_page 选项配置页

image.png

options_ui 选项配置页

image.png