Alternative Ways to Do Large Context Menus

I’m working on a formatting addon that works from the context menu, intended to be as close as possible to bbCodeXtra as possible.

I have taken the existing menu structure, and have it ready to impliment, and there are over 100 menus and submenus.

The standard way to do menus and sub menus is with a menu statement:

browser.menus.create({
            id: "test_clipboard",
            title: " &Test Clipboard",
            contexts: ["all"]
        }, onCreated);
browser.menus.create({
            id: "clipboard",
            title: " Check Clipboard",
            contexts: ["all"]
        }, onCreated);
browser.menus.create({
            id: "bbcode",
            title: browser.i18n.getMessage("bbcodewebex.menu_bbcodewebex"),
            contexts: ["all"]
        }, onCreated);
browser.menus.create({
            id: "htmlcode",
            title: browser.i18n.getMessage("bbcodewebex.menu_htmlWebEx"),
            contexts: ["all"]
        }, onCreated);

and then feed it into a case statement:

browser.menus.onClicked.addListener((info, tab) => {
    switch (info.menuItemId) {
        case "test_clipboard":
            browser.tabs.sendMessage(tab.id,{runwhat: "zzBBCode", ParseArg: "[b]{{selection}}[/b]"});
        break;
        case "clipboard":
            browser.tabs.sendMessage(tab.id,{runwhat: "zzBBCode", ParseArg: "[b]{{selection}}[/b]"});
        break;
        case "bbcode":
            browser.tabs.sendMessage(tab.id,{runwhat: "zzBBCode", ParseArg: "[b]{{selection}}[/b]"});
        break;
        case "htmlcode":
            browser.tabs.sendMessage(tab.id,{runwhat: "zzBBCode", ParseArg: "\[b\]{{selection}}\[/b\]"});
        break;
    }
});

Obviously, with 100+ menu items, this gets rather verbose.

I was wondering if anyone was aware of some way to represent this in the program in a less verbose way, and then generate the menu.

In a preferred situation, I’d want something like a 1 line per menu item setup where you had something like:

Menu ID Menu Title Parent Menu Code to Process
bbcode bbCode
bbcode.bold Bold bbcode [b]{{selection}}[/b]

So it seems you could create an object with those properties.

As far as I can tell, browser.menus.create could loop over an object. and your onClicked listener could look up message text in that object.

But I haven’t tried it myself.