Skip to content

Commit 9433f84

Browse files
authored
Code now reflects newer versions of browser.menus API (#523)
* Extension now is up-to-date with the Menus API In the previous version of this extension, the author used a comment to explain that the `browser.menus.onClicked.addListener` event listener doesn't pass the current check state of a checkbox menu. To solve this, an additional variable checkedState was used to keep track of the current value of the context menu. But now, this function is supported by the menus API in Firefox. I changed the code to reflect how you would use it with the recent versions, making it easier for new developers to learn this functionality. * Comment now reflects new function body
1 parent a2da599 commit 9433f84

File tree

1 file changed

+4
-12
lines changed

1 file changed

+4
-12
lines changed

menu-demo/background.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,12 @@ browser.menus.create({
7777
contexts: ["all"]
7878
}, onCreated);
7979

80-
let checkedState = true;
81-
8280
browser.menus.create({
8381
id: "check-uncheck",
8482
type: "checkbox",
8583
title: browser.i18n.getMessage("menuItemUncheckMe"),
8684
contexts: ["all"],
87-
checked: checkedState
85+
checked: true,
8886
}, onCreated);
8987

9088
browser.menus.create({
@@ -116,15 +114,9 @@ function borderify(tabId, color) {
116114
}
117115

118116
/*
119-
Toggle checkedState, and update the menu item's title
120-
appropriately.
121-
122-
Note that we should not have to maintain checkedState independently like
123-
this, but have to because Firefox does not currently pass the "checked"
124-
property into the event listener.
117+
Update the menu item's title according to current "checked" value.
125118
*/
126-
function updateCheckUncheck() {
127-
checkedState = !checkedState;
119+
function updateCheckUncheck(checkedState) {
128120
if (checkedState) {
129121
browser.menus.update("check-uncheck", {
130122
title: browser.i18n.getMessage("menuItemUncheckMe"),
@@ -156,7 +148,7 @@ browser.menus.onClicked.addListener((info, tab) => {
156148
borderify(tab.id, green);
157149
break;
158150
case "check-uncheck":
159-
updateCheckUncheck();
151+
updateCheckUncheck(info.checked);
160152
break;
161153
case "open-sidebar":
162154
console.log("Opening my sidebar");

0 commit comments

Comments
 (0)