The register menu function allows to postpone the appending of the menu until a referenced menu is present.
In the example above the created menu $mainMenu is added after the existing menu with identifier inspect. The mode view.menu signals, that the menu is added to the top menu in view mode.
A new section with label Userscripts and identifier userscripts is added with weight 10 to the menu identified by tools. The higher the weight, the deep (lower) the section will be dragged within the specified menu.
A new menu item is added to the section identified by userscripts. The item is identified by userscript-admin-tool with label Admin Tool. Again the weight drags the item deeper within the section, the higher the value.
The last parameter is a JavaScript function to be called once the menu item is clicked.
The following script show how this all fits together. Not the weights of the items. This example shows that the order is defined by the weights, not by the order the items are added.
dynamic-menu-tools.js
/*
* Copyright 2019-2020 Kronseder & Reiner GmbH, smartics
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
"use strict";
AJS.toInit(function () {
const logToConsole = true;
if (logToConsole) AJS.log("[dynamic-menu-tools] Starting Tool menu creation ...");
const menuId = "tools";
const sectionId = "userscripts";
const $mainMenu = USERSCRIPT4C_MENU.createMenu(menuId, "Tools");
USERSCRIPT4C_MENU.registerMenu("view.menu", $mainMenu, "inspect");
USERSCRIPT4C_MENU.addSection(menuId, {
id: sectionId,
label: "Userscripts",
weight: 10
});
USERSCRIPT4C_MENU.addMenuItem(sectionId, {
id: "userscript-admin-tool",
label: "Admin Tool",
weight: "100"
}, function () {
alert("Open Admin Tool ...")
});
USERSCRIPT4C_MENU.addMenuItem(sectionId, {
id: "before",
label: "Before",
weight: "10"
}, function () {
alert("Before ...")
});
USERSCRIPT4C_MENU.addMenuItem(sectionId, {
id: "after",
label: "After",
weight: "200"
}, function () {
alert("After ...")
});
USERSCRIPT4C_MENU.addMenuItem(sectionId, {
id: "last",
label: "Last",
weight: "99999"
}, function () {
alert("Last ...")
});
USERSCRIPT4C_MENU.addMenuItem(sectionId, {
id: "first",
label: "First",
weight: "0"
}, function () {
alert("First ...")
});
});
Resources
More information on this topic is available by the following resources.