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.
Resources
More information on this topic is available by the following resources.