A simple JavaScript library to organize actions on Confluence in menus. These actions are typically based on userscripts.

Type
Since
1.0

The tool provides functions to add sections and items to menus. To also allows to create menus from scratch.

Details

The functions to create a menu are part of USERSCRIPT4C_MENU.

The following sample code shows how a new menu can be created.

Create Menu

const menuId = "tools";
const $mainMenu = USERSCRIPT4C_MENU.createMenu(menuId, "Tools");

Creates a new menu identified by tools with the label Tools.

Register Menu

USERSCRIPT4C_MENU.registerMenu("view.menu", $mainMenu, "inspect");

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.

Add Menu Section

const menuId = "tools";
const sectionId = "userscripts";
USERSCRIPT4C_MENU.addSection(menuId, {
  id: sectionId,
  label: "Userscripts",
  weight: 10
});

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.

Add Menu Item

const sectionId = "userscripts";
USERSCRIPT4C_MENU.addMenuItem(sectionId, {
  id: "userscript-admin-tool",
  label: "Admin Tool",
  weight: "100"
}, function () {
  alert("Open Admin Tool ...")
});

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.

Writing Userscripts
A short introduction to write userscripts for the Userscripts for Confluence App.
MutationObserver
The MutationObserver interface provides the ability to watch for changes being made to the DOM tree.