OK, I am duplicating an existing legacy addon, and have the menu structure in a spreadsheet, along with the existing i18n locale data.
I can export the pertinent fields to a JSON file, which I save to disk in a sub-directory of the addon: (data/DefMenu.json, which is currently a subset of the 100+ menu items)
[
{
"Navigation Menu": "TRUE",
"menuId": "bbcwbx.bbcode",
"argument": "",
"menuTitle": "bbCode",
"icons": null,
"parentId": null
},
{
"Navigation Menu": "TRUE",
"menuId": "bbcwbx.bbcode.clp",
"argument": "",
"menuTitle": "Clipboard",
"icons": null,
"parentId": "bbcwbx.bbcode"
},
{
"Navigation Menu": "FALSE",
"menuId": "bbcwbx.bbcode.clp.quote",
"argument": "[quote{{popup,\"Please insert author name or leave empty:\"}}]{{clipboard}}[/quote]",
"menuTitle": "Quote",
"icons": null,
"parentId": "bbcwbx.bbcode.clp"
},
{
"Navigation Menu": "FALSE",
"menuId": "bbcwbx.bbcode.clp.code",
"argument": "[code]{{clipboard}}[/quote]",
"menuTitle": "Code",
"icons": null,
"parentId": "bbcwbx.bbcode.clp"
},
{
"Navigation Menu": "FALSE",
"menuId": "bbcwbx.bbcode.clp.list",
"argument": "{{createList,{{clipboard}},\"bbcode\"}}",
"menuTitle": "List",
"icons": null,
"parentId": "bbcwbx.bbcode.clp"
}
]
Using this code, I can step through the individual records (rows): (sort of)
const defMenuURL = chrome.runtime.getURL('data/DefMenu.json');
fetch(defMenuURL)
.then(function(response) {
return response.json();
})
.then(function(defaultMenu) {
for(i = 0; i < defaultMenu.length; i++){
browser.menus.create({
id: defaultMenu[i].menuId,
title: defaultMenu[i].menuTitle, //eventually this becomes an i18n call
contexts: ["all"],
icons: defaultMenu[i].icons,
parentId: defaultMenu[i].parentId
}, console.log(defaultMenu[i].argument));
The problem is that some of the menus are top level menus, and hence have no parents, and some (most) of the menus have no icons, and when a blank or null argument is used, it throws an error.
Obviously, I could use a series of if then else statements with different browser.create.menu
statements, but if I could do it inline, it would be cleaner and more readable.
I am looking for a syntax that is something like this:
browser.menus.create({
id: defaultMenu[i].menuId,
title: defaultMenu[i].menuTitle, //eventually this becomes an i18n call
contexts: ["all"],
if (defaultMenu[i].icons != "") {
icons: defaultMenu[i].icons,
}
if (defaultMenu[i].parentId != "") {
parentId: defaultMenu[i].parentId
}
}, console.log(defaultMenu[i].argument));
Is this possible, or do I have to use if else
or case
statements for complete menu declarations?