当前位置: 首页 > article >正文

Chromium 中chrome.contextMenus扩展接口定义c++

一、chrome.contextMenus

使用 chrome.contextMenus API 向 Google Chrome 的上下文菜单中添加项。您可以选择从右键菜单中添加的对象类型,例如图片、超链接和页面。

权限

contextMenus

您必须在扩展程序的清单中声明 "contextMenus" 权限,才能使用该 API。此外, 您应指定一个 16 x 16 像素的图标,显示在菜单项旁边。例如:

{
  "name": "My extension",
  ...
  "permissions": [
    "contextMenus"
  ],
  "icons": {
    "16": "icon-bitty.png",
    "48": "icon-small.png",
    "128": "icon-large.png"
  },
  ...
}

概念和用法

上下文菜单项可以出现在任何文档(或文档中的框架)中,甚至是那些带有 file:// 的菜单项 或 chrome:// 网址。要控制您的内容可以显示在哪些文档中,请指定 documentUrlPatterns 字段。create()update()

您可以根据需要创建任意数量的上下文菜单项,但如果扩展程序中的多个菜单项 则 Google Chrome 会自动将它们收起为一个父级菜单。

示例

若要试用此 API,请从 chrome-extension-samples 安装 contextMenus API 示例 存储库

类型

ContextType

Chrome 44 及更高版本

菜单可显示的不同上下文。指定“all”等同于除“launcher”以外的所有其他上下文的组合。“启动器”context 仅受应用支持,用于将菜单项添加到在启动器/任务栏/dock 等位置点击应用图标时出现的上下文菜单。不同的平台可能会限制启动器上下文菜单中实际支持的内容。

chrome.contextMenus  |  API  |  Chrome for Developers

二、chrome.contextMenus api接口 c++:

1、context_menus.json

chrome\common\extensions\api\context_menus.json

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

[
  {
    "namespace": "contextMenus",
    "description": "Use the <code>chrome.contextMenus</code> API to add items to Google Chrome's context menu. You can choose what types of objects your context menu additions apply to, such as images, hyperlinks, and pages.",
    "properties": {
      "ACTION_MENU_TOP_LEVEL_LIMIT": {
        "value": 6,
        "description": "The maximum number of top level extension items that can be added to an extension action context menu. Any items beyond this limit will be ignored."
      }
    },
    "types": [
      {
        "id": "ContextType",
        "type": "string",
        "enum": ["all", "page", "frame", "selection", "link", "editable", "image", "video", "audio", "launcher", "browser_action", "page_action", "action"],
        "description": "The different contexts a menu can appear in. Specifying 'all' is equivalent to the combination of all other contexts except for 'launcher'. The 'launcher' context is only supported by apps and is used to add menu items to the context menu that appears when clicking the app icon in the launcher/taskbar/dock/etc. Different platforms might put limitations on what is actually supported in a launcher context menu."
      },
      {
        "id": "ItemType",
        "type": "string",
        "enum": ["normal", "checkbox", "radio", "separator"],
        "description": "The type of menu item."
      },
      {
        "id": "OnClickData",
        "type": "object",
        "description": "Information sent when a context menu item is clicked.",
        "properties": {
          "menuItemId": {
            "choices": [
              { "type": "integer" },
              { "type": "string" }
            ],
            "description": "The ID of the menu item that was clicked."
          },
          "parentMenuItemId": {
            "choices": [
              { "type": "integer" },
              { "type": "string" }
            ],
            "optional": true,
            "description": "The parent ID, if any, for the item clicked."
          },
          "mediaType": {
            "type": "string",
            "optional": true,
            "description": "One of 'image', 'video', or 'audio' if the context menu was activated on one of these types of elements."
          },
          "linkUrl": {
            "type": "string",
            "optional": true,
            "description": "If the element is a link, the URL it points to."
          },
          "srcUrl": {
            "type": "string",
            "optional": true,
            "description": "Will be present for elements with a 'src' URL."
          },
          "pageUrl": {
            "type": "string",
            "optional": true,
            "description": "The URL of the page where the menu item was clicked. This property is not set if the click occured in a context where there is no current page, such as in a launcher context menu."
          },
          "frameUrl": {
            "type": "string",
            "optional": true,
            "description": " The URL of the frame of the element where the context menu was clicked, if it was in a frame."
          },
          "frameId": {
            "type": "integer",
            "optional": true,
            "description": " The <a href='webNavigation#frame_ids'>ID of the frame</a> of the element where the context menu was clicked, if it was in a frame."
          },
          "selectionText": {
            "type": "string",
            "optional": true,
            "description": "The text for the context selection, if any."
          },
          "editable": {
            "type": "boolean",
            "description": "A flag indicating whether the element is editable (text input, textarea, etc.)."
          },
          "wasChecked": {
            "type": "boolean",
            "optional": true,
            "description": "A flag indicating the state of a checkbox or radio item before it was clicked."
          },
          "checked": {
            "type": "boolean",
            "optional": true,
            "description": "A flag indicating the state of a checkbox or radio item after it is clicked."
          }
        }
      }
    ],
    "functions": [
      {
        "name": "create",
        "type": "function",
        "description": "Creates a new context menu item. If an error occurs during creation, it may not be detected until the creation callback fires; details will be in $(ref:runtime.lastError).",
        "returns": {
          "choices": [
            { "type": "integer" },
            { "type": "string" }
          ],
          "description": "The ID of the newly created item."
        },
        "parameters": [
          {
            "type": "object",
            "name": "createProperties",
            "properties": {
              "type": {
                "$ref": "ItemType",
                "optional": true,
                "description": "The type of menu item. Defaults to <code>normal</code>."
              },
              "id": {
                "type": "string",
                "optional": true,
                "description": "The unique ID to assign to this item. Mandatory for event pages. Cannot be the same as another ID for this extension."
              },
              "title": {
                "type": "string",
                "optional": true,
                "description": "The text to display in the item; this is <em>required</em> unless <code>type</code> is <code>separator</code>. When the context is <code>selection</code>, use <code>%s</code> within the string to show the selected text. For example, if this parameter's value is \"Translate '%s' to Pig Latin\" and the user selects the word \"cool\", the context menu item for the selection is \"Translate 'cool' to Pig Latin\"."
              },
              "checked": {
                "type": "boolean",
                "optional": true,
                "description": "The initial state of a checkbox or radio button: <code>true</code> for selected, <code>false</code> for unselected. Only one radio button can be selected at a time in a given group."
              },
              "contexts": {
                "type": "array",
                "items": {
                  "$ref": "ContextType"
                },
                "minItems": 1,
                "optional": true,
                "description": "List of contexts this menu item will appear in. Defaults to <code>['page']</code>."
              },
              "visible": {
                "type": "boolean",
                "optional": true,
                "description": "Whether the item is visible in the menu."
              },
              "onclick": {
                "type": "function",
                "optional": true,
                "description": "A function that is called back when the menu item is clicked. This is not available inside of a service worker; instead, they should register a listener for $(ref:contextMenus.onClicked).",
                "parameters": [
                  {
                    "name": "info",
                    "$ref": "OnClickData",
                    "description": "Information about the item clicked and the context where the click happened."
                  },
                  {
                    "name": "tab",
                    "$ref": "tabs.Tab",
                    "description": "The details of the tab where the click took place. This parameter is not present for platform apps."
                  }
                ]
              },
              "parentId": {
                "choices": [
                  { "type": "integer" },
                  { "type": "string" }
                ],
                "optional": true,
                "description": "The ID of a parent menu item; this makes the item a child of a previously added item."
              },
              "documentUrlPatterns": {
                "type": "array",
                "items": {"type": "string"},
                "optional": true,
                "description": "Restricts the item to apply only to documents or frames whose URL matches one of the given patterns. For details on pattern formats, see <a href='match_patterns'>Match Patterns</a>."
              },
              "targetUrlPatterns": {
                "type": "array",
                "items": {"type": "string"},
                "optional": true,
                "description": "Similar to <code>documentUrlPatterns</code>, filters based on the <code>src</code> attribute of <code>img</code>, <code>audio</code>, and <code>video</code> tags and the <code>href</code> attribute of <code>a</code> tags."
              },
              "enabled": {
                "type": "boolean",
                "optional": true,
                "description": "Whether this context menu item is enabled or disabled. Defaults to <code>true</code>."
              }
            }
          },
          {
            "type": "function",
            "name": "callback",
            "optional": true,
            "description": "Called when the item has been created in the browser. If an error occurs during creation, details will be available in $(ref:runtime.lastError).",
            "parameters": []
          }
        ]
      },
      {
        "name": "update",
        "type": "function",
        "description": "Updates a previously created context menu item.",
        "parameters": [
          {
            "choices": [
              { "type": "integer" },
              { "type": "string" }
            ],
            "name": "id",
            "description": "The ID of the item to update."
          },
          {
            "type": "object",
            "name": "updateProperties",
            "description": "The properties to update. Accepts the same values as the $(ref:contextMenus.create) function.",
            // We need to preserve null because we use it as an indication to clear the callback.
            "preserveNull": true,
            "properties": {
              "type": {
                "$ref": "ItemType",
                "optional": true
              },
              "title": {
                "type": "string",
                "optional": true
              },
              "checked": {
                "type": "boolean",
                "optional": true
              },
              "contexts": {
                "type": "array",
                "items": {
                  "$ref": "ContextType"
                },
                "minItems": 1,
                "optional": true
              },
              "visible": {
                "type": "boolean",
                "optional": true,
                "description": "Whether the item is visible in the menu."
              },
              "onclick": {
                "type": "function",
                "optional": true,
                "parameters": [
                  {
                    "name": "info",
                    "$ref": "OnClickData"
                  },
                  {
                    "name": "tab",
                    "$ref": "tabs.Tab",
                    "description": "The details of the tab where the click took place. This parameter is not present for platform apps."
                  }
                ]
              },
              "parentId": {
                "choices": [
                  { "type": "integer" },
                  { "type": "string" }
                ],
                "optional": true,
                "description": "The ID of the item to be made this item's parent. Note: You cannot set an item to become a child of its own descendant."
              },
              "documentUrlPatterns": {
                "type": "array",
                "items": {"type": "string"},
                "optional": true
              },
              "targetUrlPatterns": {
                "type": "array",
                "items": {"type": "string"},
                "optional": true
              },
              "enabled": {
                "type": "boolean",
                "optional": true
              }
            }
          },
          {
            "type": "function",
            "name": "callback",
            "optional": true,
            "parameters": [],
            "description": "Called when the context menu has been updated."
          }
        ]
      },
      {
        "name": "remove",
        "type": "function",
        "description": "Removes a context menu item.",
        "parameters": [
          {
            "choices": [
              { "type": "integer" },
              { "type": "string" }
            ],
            "name": "menuItemId",
            "description": "The ID of the context menu item to remove."
          },
          {
            "type": "function",
            "name": "callback",
            "optional": true,
            "parameters": [],
            "description": "Called when the context menu has been removed."
          }
        ]
      },
      {
        "name": "removeAll",
        "type": "function",
        "description": "Removes all context menu items added by this extension.",
        "parameters": [
          {
            "type": "function",
            "name": "callback",
            "optional": true,
            "parameters": [],
            "description": "Called when removal is complete."
          }
        ]
      }
    ],
    "events": [
      {
        "name": "onClicked",
        "type": "function",
        "description": "Fired when a context menu item is clicked.",
        "parameters": [
          {
            "name": "info",
            "$ref": "OnClickData",
            "description": "Information about the item clicked and the context where the click happened."
          },
          {
            "name": "tab",
            "$ref": "tabs.Tab",
            "description": "The details of the tab where the click took place. If the click did not take place in a tab, this parameter will be missing.",
            "optional": true
          }
        ]
      }
    ]
  }
]

2、context_menus.json自动生成c++文件:

// GENERATED FROM THE API DEFINITION IN

//   chrome/common/extensions/api/context_menus.json

// by tools/json_schema_compiler.

// DO NOT EDIT.

out\Debug\gen\chrome\common\extensions\api\context_menus.h

out\Debug\gen\chrome\common\extensions\api\context_menus.cc

3、chrome.contextMenus api定义c++:

chrome\browser\extensions\api\context_menus\context_menus_api.h

chrome\browser\extensions\api\context_menus\context_menus_api.cc

namespace extensions {

class ContextMenusCreateFunction : public ExtensionFunction {
 public:
  DECLARE_EXTENSION_FUNCTION("contextMenus.create", CONTEXTMENUS_CREATE)

 protected:
  ~ContextMenusCreateFunction() override {}

  // ExtensionFunction:
  ResponseAction Run() override;
};

class ContextMenusUpdateFunction : public ExtensionFunction {
 public:
  DECLARE_EXTENSION_FUNCTION("contextMenus.update", CONTEXTMENUS_UPDATE)

 protected:
  ~ContextMenusUpdateFunction() override {}

  // ExtensionFunction:
  ResponseAction Run() override;
};

class ContextMenusRemoveFunction : public ExtensionFunction {
 public:
  DECLARE_EXTENSION_FUNCTION("contextMenus.remove", CONTEXTMENUS_REMOVE)

 protected:
  ~ContextMenusRemoveFunction() override {}

  // ExtensionFunction:
  ResponseAction Run() override;
};

class ContextMenusRemoveAllFunction : public ExtensionFunction {
 public:
  DECLARE_EXTENSION_FUNCTION("contextMenus.removeAll", CONTEXTMENUS_REMOVEALL)

 protected:
  ~ContextMenusRemoveAllFunction() override {}

  // ExtensionFunction:
  ResponseAction Run() override;
};

}  // namespace extensions

4、 创建菜单帮助类:

chrome\browser\extensions\api\context_menus\context_menus_api_helpers.h

chrome\browser\extensions\api\context_menus\context_menus_api_helpers.cc

三、看下加载效果:


http://www.kler.cn/a/389779.html

相关文章:

  • Kafka - 启用安全通信和认证机制_SSL + SASL
  • 猿创征文|Inscode桌面IDE:打造高效开发新体验
  • 【深圳大学】数据结构A+攻略(计软版)
  • 多叉树笔记
  • 算法演练----24点游戏
  • vxe-table 3.10+ 进阶高级用法(一),根据业务需求自定义实现筛选功能
  • QT模态对话框和非模态对话框区别以及常用标准对话框
  • 【QT】Qt网络
  • 【CSS】什么是BFC?
  • 2.5_XXE(XML外部实体注入)
  • 杨辉三角-一维数组与二维数组解法
  • 图片搜索引擎,来快速实现一个高性能的本地图片搜索引擎
  • Ansys EMC Plus:以 Touchstone 格式计算和导出 S 参数
  • Web3推动社交媒体的去中心化转型:挑战与机遇
  • FFmpeg —— 通过AES-CTR方式对视频加密解密(详细介绍通过FFmpeg指令、代码方式进行加密解码,附源码)
  • Information Server 中共享开源服务中 kafka 的__consumer_offsets目录过大清理
  • 第二周训练
  • 计算机网络系列课程《网络解释》
  • 【力扣】05最长的回文子串
  • 【C++ 算法进阶】算法提升十四
  • Python之魔术方法笔记
  • Spring Boot集成SQL Server快速入门Demo
  • jsmind 思维导出 vue 示例
  • ArcGIS从Excel表格文件导入XY数据并定义坐标系与投影的方法
  • Rancher的安装
  • JS禁用鼠标滚动条功能且滚动条不消失教程