Update context-menu-demo (#272)

* Update context-menu-demo

* Some cleanup, and note from cross-browser compat
This commit is contained in:
wbamberg
2017-09-08 16:07:36 -07:00
committed by GitHub
parent d7bf874833
commit 179836d794
10 changed files with 54 additions and 37 deletions

View File

@@ -103,8 +103,8 @@
"runtime.lastError", "runtime.lastError",
"tabs.executeScript" "tabs.executeScript"
], ],
"name": "context-menu-demo", "name": "menu-demo",
"description": "Demonstrates adding and manipulating context menu items using the contextMenus API." "description": "Demonstrates adding and manipulating menu items using the menus API."
}, },
{ {
"javascript_apis": [ "javascript_apis": [

View File

@@ -1,9 +1,11 @@
# context-menu-demo # menu-demo
A demo of the [contextMenus API](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/contextMenus/). A demo of the [menus API](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/menus/).
**This add-on injects JavaScript into web pages. The `addons.mozilla.org` domain disallows this operation, so this add-on will not work properly when it's run on pages in the `addons.mozilla.org` domain.** **This add-on injects JavaScript into web pages. The `addons.mozilla.org` domain disallows this operation, so this add-on will not work properly when it's run on pages in the `addons.mozilla.org` domain.**
**This add-on uses the `menus` namespace to access the functions it needs to create menu items. Note that Chrome, Edge, and Opera all use the `contextMenus` namespace for this, so this extension will not work in these browsers. For compatibility with these browsers, Firefox also offers the `contextMenus` namespace, so to make this extension work with other browsers, use `contextMenus`.**
## What it does ## What it does
This add-on adds several items to the browser's context menu: This add-on adds several items to the browser's context menu:
@@ -20,9 +22,11 @@ like about:debugging.
item is clicked. item is clicked.
* one item that uses the "commands" property to open the add-on's sidebar. * one item that uses the "commands" property to open the add-on's sidebar.
It also adds one item to the browser's "Tools" menu.
## What it shows ## What it shows
* How to create various types of context menu item: * How to create various types of menu item:
* normal * normal
* radio * radio
* separator * separator

View File

@@ -1,47 +1,52 @@
{ {
"extensionName": { "extensionName": {
"message": "Context menu demo", "message": "Menu demo",
"description": "Name of the extension." "description": "Name of the extension."
}, },
"extensionDescription": { "extensionDescription": {
"message": "Demonstrates the contextMenus API.", "message": "Demonstrates the menus API.",
"description": "Description of the add-on." "description": "Description of the add-on."
}, },
"contextMenuItemSelectionLogger": { "menuItemSelectionLogger": {
"message": "Log '%s' to the browser console", "message": "Log '%s' to the browser console",
"description": "Title of context menu item that logs the selected text when clicked." "description": "Title of context menu item that logs the selected text when clicked."
}, },
"contextMenuItemRemoveMe": { "menuItemRemoveMe": {
"message": "Remove me!", "message": "Remove me!",
"description": "Title of context menu item that removes itself when clicked." "description": "Title of context menu item that removes itself when clicked."
}, },
"contextMenuItemGreenify": { "menuItemGreenify": {
"message": "Greenify", "message": "Greenify",
"description": "Title of context menu item that adds a green border when clicked." "description": "Title of context menu item that adds a green border when clicked."
}, },
"contextMenuItemBluify": { "menuItemBluify": {
"message": "Bluify", "message": "Bluify",
"description": "Title of context menu item that adds a green border when clicked." "description": "Title of context menu item that adds a green border when clicked."
}, },
"contextMenuItemCheckMe": { "menuItemCheckMe": {
"message": "Check me", "message": "Check me",
"description": "Title of context menu item when the item is checked." "description": "Title of context menu item when the item is checked."
}, },
"contextMenuItemUncheckMe": { "menuItemUncheckMe": {
"message": "Uncheck me", "message": "Uncheck me",
"description": "Title of context menu item when the item is unchecked." "description": "Title of context menu item when the item is unchecked."
}, },
"contextMenuItemOpenSidebar": { "menuItemOpenSidebar": {
"message": "Open sidebar", "message": "Open sidebar",
"description": "Title of context menu item that opens a sidebar." "description": "Title of context menu item that opens a sidebar."
},
"menuItemToolsMenu": {
"message": "Click me!",
"description": "Title of tools menu item."
} }
} }

View File

@@ -29,41 +29,41 @@ function onError(error) {
/* /*
Create all the context menu items. Create all the context menu items.
*/ */
browser.contextMenus.create({ browser.menus.create({
id: "log-selection", id: "log-selection",
title: browser.i18n.getMessage("contextMenuItemSelectionLogger"), title: browser.i18n.getMessage("menuItemSelectionLogger"),
contexts: ["selection"] contexts: ["selection"]
}, onCreated); }, onCreated);
browser.contextMenus.create({ browser.menus.create({
id: "remove-me", id: "remove-me",
title: browser.i18n.getMessage("contextMenuItemRemoveMe"), title: browser.i18n.getMessage("menuItemRemoveMe"),
contexts: ["all"] contexts: ["all"]
}, onCreated); }, onCreated);
browser.contextMenus.create({ browser.menus.create({
id: "separator-1", id: "separator-1",
type: "separator", type: "separator",
contexts: ["all"] contexts: ["all"]
}, onCreated); }, onCreated);
browser.contextMenus.create({ browser.menus.create({
id: "greenify", id: "greenify",
type: "radio", type: "radio",
title: browser.i18n.getMessage("contextMenuItemGreenify"), title: browser.i18n.getMessage("menuItemGreenify"),
contexts: ["all"], contexts: ["all"],
checked: true checked: true
}, onCreated); }, onCreated);
browser.contextMenus.create({ browser.menus.create({
id: "bluify", id: "bluify",
type: "radio", type: "radio",
title: browser.i18n.getMessage("contextMenuItemBluify"), title: browser.i18n.getMessage("menuItemBluify"),
contexts: ["all"], contexts: ["all"],
checked: false checked: false
}, onCreated); }, onCreated);
browser.contextMenus.create({ browser.menus.create({
id: "separator-2", id: "separator-2",
type: "separator", type: "separator",
contexts: ["all"] contexts: ["all"]
@@ -71,22 +71,27 @@ browser.contextMenus.create({
var checkedState = true; var checkedState = true;
browser.contextMenus.create({ browser.menus.create({
id: "check-uncheck", id: "check-uncheck",
type: "checkbox", type: "checkbox",
title: browser.i18n.getMessage("contextMenuItemUncheckMe"), title: browser.i18n.getMessage("menuItemUncheckMe"),
contexts: ["all"], contexts: ["all"],
checked: checkedState checked: checkedState
}, onCreated); }, onCreated);
browser.menus.create({
browser.contextMenus.create({
id: "open-sidebar", id: "open-sidebar",
title: browser.i18n.getMessage("contextMenuItemOpenSidebar"), title: browser.i18n.getMessage("menuItemOpenSidebar"),
contexts: ["all"], contexts: ["all"],
command: "_execute_sidebar_action" command: "_execute_sidebar_action"
}, onCreated); }, onCreated);
browser.menus.create({
id: "tools-menu",
title: browser.i18n.getMessage("menuItemToolsMenu"),
contexts: ["tools_menu"],
}, onCreated);
/* /*
Set a colored border on the document in the given tab. Set a colored border on the document in the given tab.
@@ -113,12 +118,12 @@ property into the event listener.
function updateCheckUncheck() { function updateCheckUncheck() {
checkedState = !checkedState; checkedState = !checkedState;
if (checkedState) { if (checkedState) {
browser.contextMenus.update("check-uncheck", { browser.menus.update("check-uncheck", {
title: browser.i18n.getMessage("contextMenuItemUncheckMe"), title: browser.i18n.getMessage("menuItemUncheckMe"),
}); });
} else { } else {
browser.contextMenus.update("check-uncheck", { browser.menus.update("check-uncheck", {
title: browser.i18n.getMessage("contextMenuItemCheckMe"), title: browser.i18n.getMessage("menuItemCheckMe"),
}); });
} }
} }
@@ -127,13 +132,13 @@ function updateCheckUncheck() {
The click event listener, where we perform the appropriate action given the The click event listener, where we perform the appropriate action given the
ID of the menu item that was clicked. ID of the menu item that was clicked.
*/ */
browser.contextMenus.onClicked.addListener((info, tab) => { browser.menus.onClicked.addListener((info, tab) => {
switch (info.menuItemId) { switch (info.menuItemId) {
case "log-selection": case "log-selection":
console.log(info.selectionText); console.log(info.selectionText);
break; break;
case "remove-me": case "remove-me":
var removing = browser.contextMenus.remove(info.menuItemId); var removing = browser.menus.remove(info.menuItemId);
removing.then(onRemoved, onError); removing.then(onRemoved, onError);
break; break;
case "bluify": case "bluify":
@@ -148,5 +153,8 @@ browser.contextMenus.onClicked.addListener((info, tab) => {
case "open-sidebar": case "open-sidebar":
console.log("Opening my sidebar"); console.log("Opening my sidebar");
break; break;
case "tools-menu":
console.log("Clicked the tools menu item");
break;
} }
}); });

View File

Before

Width:  |  Height:  |  Size: 251 B

After

Width:  |  Height:  |  Size: 251 B

View File

Before

Width:  |  Height:  |  Size: 344 B

After

Width:  |  Height:  |  Size: 344 B

View File

Before

Width:  |  Height:  |  Size: 310 B

After

Width:  |  Height:  |  Size: 310 B

View File

@@ -7,7 +7,7 @@
"default_locale": "en", "default_locale": "en",
"applications": { "applications": {
"gecko": { "gecko": {
"strict_min_version": "55.0a1" "strict_min_version": "56.0a1"
} }
}, },
@@ -16,7 +16,7 @@
}, },
"permissions": [ "permissions": [
"contextMenus", "menus",
"activeTab" "activeTab"
], ],