chrome插件API协议(Extensions Context Menu API Proposal)

简介: Extensions Context Menu API Proposal NOTE: This document is now out of date. The API evolved some while behind the experimental flag, and is becoming fully supported in chrome 6.

Extensions Context Menu API Proposal


NOTE: This document is now out of date. The API evolved some while behind the experimental flag, and is becoming fully supported in chrome 6. For details on the current implementation you can see 




Status
Being implemented behind the --enable-experimental-extension-apis flag so we can collect developer feedback. Should be landing in the tree relatively soon.

Overview
Provide a way for extensions to add items to the context (aka "right click") menu for web pages. 

Use cases
Many features that extensions want to expose are only applicable to certain kinds of content. Some examples include saving selected text or link urls to a extension-local or web based notepad, removing particular images from a page, filling in form fields, and looking up words in an online dictionary.

Could this API be part of the web platform?
Probably not, since many actions people would like to add are logically "browser-level" functionality.

Do you expect this API to be fairly stable?
Yes, once we get some experience with an experimental version.

What UI does this API expose?
New menu items & submenus in context menus.

How could this API be abused?
Someone could create menu items with innocuous-sounding or misleading names (or even duplicates of built-in menu items) to trick users to click on them.

How would you implement your desired features if this API didn't exist?
You could (and perhaps some extensions do?) inject script into all pages to intercept onmousedown and create your own custom html menu implementation. But that is a terrible user experience since it prevents the browser-native menu from appearing.

Draft API spec

Initially these functions will only be callable from an extension's background page. This will likely not be sufficient, since a frequent use case for context menus is to operate on the specific link/image/etc. that the menu was activated on. See the "Future Improvements" section below for ideas about this.

Note: because this will be initially available as an experiment, the API methods will at first be chrome.experimental.contextMenu.methodname

chrome.contextMenu.create (object properties, function onCreatedCallback);

Creates a new context menu item. The onCreatedCallback function should have a signature like function(id) {}, and will be passed the integer id of the newly created menu item.

The properties parameter can contain:
title  (optional string) - Text for this menu item. This is required for all types except 'SEPARATOR'. The special string %s in a title will be replaced with the currently selected text, if any.
type  (optional string) - One of the strings 'NORMAL', 'CHECKBOX', 'RADIO', or 'SEPARATOR'. Defaults to 'NORMAL' if not specified.
checked (optional boolean) - For items of type CHECKBOX or RADIO, should this be selected (RADIO) or checked (CHECKBOX)? Only one RADIO item can be selected at a time in a given group of RADIO items, with the last one to have checked == true winning.
contexts  (string) - Which contexts should this menu item appear for? An array of one or more of the following strings: 
'ALL', 'PAGE', 'SELECTION', 'LINK', 'EDITABLE', 'IMAGE', 'VIDEO', 'AUDIO'. 
Defaults to PAGE, which means "none of the rest" are selected (no link, no image, etc.). If 'ALL' is in the array, the item will appear in all contexts regardless of the rest of the items in the array.
enabledContexts  (string) - An array of strings similar to the contexts property. This serves to limit the contexts where this item will be enabled (ie not greyed out). If omitted, it defaults to the same set as the contexts property.
parentId  (integer) - If this should be a child item of another item, this is the id
onclick  (function(object info, Tab tab)) - A function that will be called when this menu item is clicked on. The info parameter is an object containing the following properties:
menuItemId  (integer) - The id of the menu item that was clicked.
parentMenuItemId  (optional integer) - The parent id, if any, for the item clicked.
mediaType  (optional string) - One of 'IMAGE', 'AUDIO', or 'VIDEO' if the context item was brought up on one of these types of elements.
linkUrl  (optional string) - If the element is a link, the url it points to.
srcUrl  (optional string) - Will be present for elements with a 'src' url.
pageUrl  (optional string) - The url of the page where the context menu was clicked.
frameUrl  (optional string) - The url of the frame of the element where the context menu was clicked.
selectionText  (optional string) - The text for the context selection, if any.
editable  (boolean) - A flag indicating whether the element is editable (text input, textarea, etc.)

The tab parameter is the tab where the click happened, of the same form as that returned by chrome.tabs.get.

 

 


chrome.contextMenu.remove (id);

Removes an extension menu item with the given id.



Examples

Define the currently selected word(s)

​The following code would live in your background page, and you would need to have "tabs" specified in the permissions section of your manifest.

​function getDefinition(info, tab) {
 if (!info.selectionText || info.selectionText.length == 0) {
   return;
 }
 var maxLength = 1024;
 var searchText = (info.selectionText.length <= maxLength) ?
                  info.selectionText : info.selectionText.substr(0, maxLength);
 var url = "http://www.google.com/search?q=define:" + escape(searchText);
 chrome.tabs.create({"url": url});
}

chrome.contextMenu.create({"title": "Define '%s'", "onclick": getDefinition,
                          "contexts":["SELECTION"]});



Remove an image from a page

The following code would live in your background page, and you would need to have an entry in the permissions section of your manifest which allowed you to run chrome.tabs.executeScript on the page where the image had its context menu activated. This example highlights one of the limitations of the initial proposal, which is that you don't have any way to determine the actual unique node that was clicked on, so it removes any instances of the image from the page.

chrome.contextMenu.create({"title": "Remove This Image", "contexts": ["IMAGE"],
                           "onclick": function(info, tab) {
  var frameUrl = info.frameUrl ? info.frameUrl : "";
  var code = 
      "var imgs = document.getElementsByTagName('img');" +
      "var srcUrl = unescape('" + escape(info.srcUrl) + "');" +
      "for (var i in imgs) {" + 
      "  var img = imgs[i];" +
      "  if (img.src == srcUrl) {" +
      "    img.parentNode.removeChild(img);" +
      "  }" +
      "}";
  chrome.tabs.executeScript(tab.id, {"allFrames": true, "code": code});
}});

UI Design
-Extension items appear towards the bottom of the context menu, above "Inspect element", sorted by extension name.
-We will show at most one top-level menu item per extension. If an extension adds more than 1 item, we automatically push the items into a submenu, with the extension name as the top-level item.
-The extension's icon will appear to the left of the top-level item to help the user understand which extension added what items, and help to mitigate the spoofing concerns raised above.



Future Improvements

- Provide a mechanism for getting a handle to the precise node in the DOM where the menu was activated. This could mean one of the following ideas:
  • Allow registration of context menus from content scripts, with a callback function that receives a handle to the node.
  • Pass the id of the node in the callback to the background page, and if the node didn't have an id we synthesize one.
  • Provide something like chrome.tabs.executeScript, where you can pass a file or string of code to execute but also specify a function that will be called with the node
- Add an update(int id, object properties) method so you can change your menu items in place.

- Add a removeAll() method so you don't have to keep track of id's if you don't want to.

-Add a way to limit your menu items to specific tabs or frame urls (by a given match pattern, or perhaps automatically limit to only sites you have content script permissions for)

-Some people have asked for a onBeforeShow() event to fire before the menu items are displayed, but this may be infeasible given chrome's multiprocess architecture and desire to keep the UI very responsive.
 
目录
打赏
0
0
0
0
20
分享
相关文章
深入解密 :Postman、Apipost和Apifox API 协议与工具选择
作为全栈开发者,每天与API打交道是常态。本文总结了多年经验,深入解析常见API协议(HTTP(s)、SSE、gRPC、WebSocket、Socket.IO)及其适用场景,并对比三款主流调试工具(Postman、Apipost、ApiFox)。从基础特性到高级应用,帮助开发者根据需求选择最优方案,提升效率,让开发更顺畅!
【2025.3.08更新】Linkreate wordpressAI智能插件|自动生成SEO文章/图片/视频+长尾词优化 内置DeepSeek多模型支持与API扩展
Linkreate WordPress AI插件提供强大的自动化文章生成、SEO优化、关键词管理和内容采集功能。它能根据关键词自动生成高质量文章,支持多语言和批量生成,内置长尾关键词生成工具,并可定时自动发布文章。插件还集成了多种AI服务,支持前端AI客服窗口及媒体生成,帮助用户高效管理网站内容,提升SEO效果。
Linkreate wordpressAI智能插件|自动生成SEO文章/图片/视频+长尾词优化 内置DeepSeek多模型支持与API扩展
Linkreate WordPress AI插件提供强大的文章生成与优化功能,支持自动化生成高质量文章、批量生成、SEO优化及双标题定制。关键词生成管理方面,可批量生成长尾关键词并自定义参数。内容采集功能支持单篇和批量采集指定网站内容,可视化规则生成器方便使用。定时任务实现全自动文章生成,24小时稳定运行。API集成兼容多种AI服务,如DeepSeek、OpenAI等,并支持前端AI客服窗口。媒体生成功能包括自动为文章生成图片和短视频,提升内容丰富度。官网提供插件演示及下载:[https://idc.xymww.com/](https://idc.xymww.com/)
|
5月前
|
ServiceStack:不仅仅是一个高性能Web API和微服务框架,更是一站式解决方案——深入解析其多协议支持及简便开发流程,带您体验前所未有的.NET开发效率革命
【10月更文挑战第9天】ServiceStack 是一个高性能的 Web API 和微服务框架,支持 JSON、XML、CSV 等多种数据格式。它简化了 .NET 应用的开发流程,提供了直观的 RESTful 服务构建方式。ServiceStack 支持高并发请求和复杂业务逻辑,安装简单,通过 NuGet 包管理器即可快速集成。示例代码展示了如何创建一个返回当前日期的简单服务,包括定义请求和响应 DTO、实现服务逻辑、配置路由和宿主。ServiceStack 还支持 WebSocket、SignalR 等实时通信协议,具备自动验证、自动过滤器等丰富功能,适合快速搭建高性能、可扩展的服务端应用。
327 3
Chrome 插件上架发布全流程指南
浏览器插件开发完以后,要发布到 Chrome Web Store上,也是需要颇费一番周折的,本文就从注册账号开始,一直到最后发布上架的全流程进行指导,希望帮助你提供一些经验,避免踩坑,耗时耗力。
358 8
数据魔力,一触即发 —— Dataphin数据服务API,百炼插件新星降临!
本文通过一个利用百炼大模型平台和Dataphin数据服务API构建一个客户360智能应用的案例,介绍如何使用Dataphin数据服务API在百炼平台创建一个自定义插件,用于智能应用的开发,提升企业智能化应用水平。
342 4
数据魔力,一触即发 —— Dataphin数据服务API,百炼插件新星降临!
WebChat:开源的网页内容增强问答 AI 助手,基于 Chrome 扩展的最佳实践开发,支持自定义 API 和本地大模型
WebChat 是一个基于 Chrome 扩展开发的 AI 助手,能够帮助用户理解和分析当前网页的内容,支持自定义 API 和本地大模型。
492 1
vue学习:chrome 中 vuetools 开发插件 的下载、安装
这篇文章介绍了如何在Chrome浏览器中下载、安装并测试Vue.js开发插件——vue-devtools。
941 0
vue学习:chrome 中 vuetools 开发插件 的下载、安装
API协议 的十种技术特点及适用场景
本文介绍了十种常见的API协议技术,包括REST、GraphQL、gRPC、SOAP、WebSocket、AMF和XML-RPC等,每种技术都有其特点和适用场景,如REST适用于轻量级Web服务开发,gRPC适合高性能分布式系统,而WebSocket则适用于需要低延迟交互的应用。
《Chrome谷歌插件Top10》开发最好用的谷歌插件
本文介绍了多个实用的浏览器插件及其安装方法。包括CSDN浏览器助手,提供高效开发工具;FeHelper,前端必备工具,支持格式化、压缩等功能;uBlock Origin,有效屏蔽广告和弹窗;PageLiner,网页标尺工具,便于前端设计;Fatkun,批量下载图片;Smallpdf,文件转换工具;Octotree,GitHub代码树插件;Awesome Screenshot,截图与录屏工具;ColorZilla,颜色拾取器;Dark Reader,暗黑模式阅读插件。安装方式有通过Chrome商店搜索或下载crx插件本地安装。
178 11

热门文章

最新文章