-
Notifications
You must be signed in to change notification settings - Fork 2.6k
menu-demo to Manifest v3 #634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,120 +1,121 @@ | ||
| /* | ||
| Called when the item has been created, or when creation failed due to an error. | ||
| We'll just log success/failure here. | ||
| Called when a menu item is created, or when creation fails due to an error. | ||
| We log the error here. | ||
| */ | ||
| function onCreated() { | ||
| if (browser.runtime.lastError) { | ||
| console.log(`Error: ${browser.runtime.lastError}`); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think that it would still be useful to log an error to show how error handling works. The The
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Rob--W done, plus some tweaks to the comments |
||
| } else { | ||
| console.log("Item created successfully"); | ||
| console.log(`Error: ${browser.runtime.lastError.message}`); | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| Called when the item has been removed. | ||
| We'll just log success here. | ||
| Called when the menu item is removed. | ||
| We log success here. | ||
| */ | ||
| function onRemoved() { | ||
| console.log("Item removed successfully"); | ||
| } | ||
|
|
||
| /* | ||
| Called when there was an error. | ||
| We'll just log the error here. | ||
| Called when there is an error in removing a menu item. | ||
| We log the error here. | ||
| */ | ||
| function onError(error) { | ||
| console.log(`Error: ${error}`); | ||
| } | ||
|
|
||
| /* | ||
| Create all the context menu items. | ||
| Creates all the context menu items. | ||
| */ | ||
| browser.menus.create({ | ||
| id: "log-selection", | ||
| title: browser.i18n.getMessage("menuItemSelectionLogger"), | ||
| contexts: ["selection"] | ||
| }, onCreated); | ||
|
|
||
| browser.menus.create({ | ||
| id: "remove-me", | ||
| title: browser.i18n.getMessage("menuItemRemoveMe"), | ||
| contexts: ["all"] | ||
| }, onCreated); | ||
|
|
||
| browser.menus.create({ | ||
| id: "separator-1", | ||
| type: "separator", | ||
| contexts: ["all"] | ||
| }, onCreated); | ||
|
|
||
| browser.menus.create({ | ||
| id: "greenify", | ||
| type: "radio", | ||
| title: browser.i18n.getMessage("menuItemGreenify"), | ||
| contexts: ["all"], | ||
| checked: true, | ||
| icons: { | ||
| "16": "icons/paint-green-16.png", | ||
| "32": "icons/paint-green-32.png" | ||
| } | ||
| }, onCreated); | ||
|
|
||
| browser.menus.create({ | ||
| id: "bluify", | ||
| type: "radio", | ||
| title: browser.i18n.getMessage("menuItemBluify"), | ||
| contexts: ["all"], | ||
| checked: false, | ||
| icons: { | ||
| "16": "icons/paint-blue-16.png", | ||
| "32": "icons/paint-blue-32.png" | ||
| } | ||
| }, onCreated); | ||
|
|
||
| browser.menus.create({ | ||
| id: "separator-2", | ||
| type: "separator", | ||
| contexts: ["all"] | ||
| }, onCreated); | ||
|
|
||
| browser.menus.create({ | ||
| id: "check-uncheck", | ||
| type: "checkbox", | ||
| title: browser.i18n.getMessage("menuItemUncheckMe"), | ||
| contexts: ["all"], | ||
| checked: true, | ||
| }, onCreated); | ||
|
|
||
| browser.menus.create({ | ||
| id: "open-sidebar", | ||
| title: browser.i18n.getMessage("menuItemOpenSidebar"), | ||
| contexts: ["all"], | ||
| command: "_execute_sidebar_action" | ||
| }, onCreated); | ||
|
|
||
| browser.menus.create({ | ||
| id: "tools-menu", | ||
| title: browser.i18n.getMessage("menuItemToolsMenu"), | ||
| contexts: ["tools_menu"], | ||
| }, onCreated); | ||
| browser.runtime.onInstalled.addListener(() => { | ||
| browser.menus.create({ | ||
| id: "log-selection", | ||
| title: browser.i18n.getMessage("menuItemSelectionLogger"), | ||
| contexts: ["selection"] | ||
| }, onCreated); | ||
|
|
||
| browser.menus.create({ | ||
| id: "remove-me", | ||
| title: browser.i18n.getMessage("menuItemRemoveMe"), | ||
| contexts: ["all"] | ||
| }, onCreated); | ||
|
|
||
| browser.menus.create({ | ||
| id: "separator-1", | ||
| type: "separator", | ||
| contexts: ["all"] | ||
| }, onCreated); | ||
|
|
||
| browser.menus.create({ | ||
| id: "greenify", | ||
| type: "radio", | ||
| title: browser.i18n.getMessage("menuItemGreenify"), | ||
| contexts: ["all"], | ||
| checked: true, | ||
| icons: { | ||
| "16": "icons/paint-green-16.png", | ||
| "32": "icons/paint-green-32.png" | ||
| } | ||
| }, onCreated); | ||
|
|
||
| browser.menus.create({ | ||
| id: "bluify", | ||
| type: "radio", | ||
| title: browser.i18n.getMessage("menuItemBluify"), | ||
| contexts: ["all"], | ||
| checked: false, | ||
| icons: { | ||
| "16": "icons/paint-blue-16.png", | ||
| "32": "icons/paint-blue-32.png" | ||
| } | ||
| }, onCreated); | ||
|
|
||
| browser.menus.create({ | ||
| id: "separator-2", | ||
| type: "separator", | ||
| contexts: ["all"] | ||
| }, onCreated); | ||
|
|
||
| browser.menus.create({ | ||
| id: "check-uncheck", | ||
| type: "checkbox", | ||
| title: browser.i18n.getMessage("menuItemUncheckMe"), | ||
| contexts: ["all"], | ||
| checked: true, | ||
| }, onCreated); | ||
|
|
||
| browser.menus.create({ | ||
| id: "open-sidebar", | ||
| title: browser.i18n.getMessage("menuItemOpenSidebar"), | ||
| contexts: ["all"], | ||
| command: "_execute_sidebar_action" | ||
| }, 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. | ||
| Sets a colored border on the document in the tab returned by the onClicked listener. | ||
|
|
||
| Note that this only work on normal web pages, not special pages | ||
| like about:debugging. | ||
| Note that this only works on normal web pages, not special pages, such as about:debugging. | ||
| */ | ||
| let blue = 'document.body.style.border = "5px solid blue"'; | ||
| let green = 'document.body.style.border = "5px solid green"'; | ||
| const blue = "5px solid blue"; | ||
| const green = "5px solid green"; | ||
|
|
||
| function borderify(tabId, color) { | ||
| browser.tabs.executeScript(tabId, { | ||
| code: color | ||
| browser.scripting.executeScript({ | ||
| target: { tabId }, | ||
| func: (border) => { document.body.style.border = border; }, | ||
| args: [color] | ||
| }); | ||
| } | ||
|
|
||
| /* | ||
| Update the menu item's title according to current "checked" value. | ||
| Updates the menu item's title according to "checked" value. | ||
| */ | ||
| function updateCheckUncheck(checkedState) { | ||
| if (checkedState) { | ||
|
|
@@ -129,17 +130,15 @@ function updateCheckUncheck(checkedState) { | |
| } | ||
|
|
||
| /* | ||
| The click event listener, where we perform the appropriate action given the | ||
| ID of the menu item that was clicked. | ||
| The click event listener, where the extension performs the appropriate action given the ID of the menu item clicked. | ||
| */ | ||
| browser.menus.onClicked.addListener((info, tab) => { | ||
| switch (info.menuItemId) { | ||
| case "log-selection": | ||
| console.log(info.selectionText); | ||
| break; | ||
| case "remove-me": | ||
| let removing = browser.menus.remove(info.menuItemId); | ||
| removing.then(onRemoved, onError); | ||
| browser.menus.remove(info.menuItemId).then(onRemoved, onError); | ||
| break; | ||
| case "bluify": | ||
| borderify(tab.id, blue); | ||
|
|
@@ -151,7 +150,7 @@ browser.menus.onClicked.addListener((info, tab) => { | |
| updateCheckUncheck(info.checked); | ||
| break; | ||
| case "open-sidebar": | ||
| console.log("Opening my sidebar"); | ||
| console.warn("_execute_sidebar_action not supported"); | ||
| break; | ||
| case "tools-menu": | ||
| console.log("Clicked the tools menu item"); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep
runtime.lastErroras an example, see my comment in background.js.