Page tree

 

A small JavaScript library to easily check activation information.

Type
Since
1.0

The tool provides functions to check activation properties from within userscripts.

The checks include 

  1. group membership of the current user
  2. categories attached to the space visited by the current user
  3. labels attached to the page visited by the current user

This allows a more fine grained process of checking the context of the current user.

Details

All functions are provided via USERSCRIPT4C_ACTIVE.

Helper Functions

The library contains the following helpers to check if the groups, space categories, or page labels contain specific elements.

The following functions return a boolean value.

  • containsAll(requiredValues, actualValues)
    Returns true if every element in the array requiredValues is also a member in the array actualValues,
    false if at least one element in requiredValues is not element in actualValues.
  • containsAny(requiredValues, actualValues)
    Returns true if at least one element in the array requiredValues is also a member in the array actualValues,
    false if no element in requiredValues is also a member of actualValues.
  • contains(requiredElement, actualValues)
    Returns true if the requiredElement is a member in the array actualValues,
    false if it is not.

Group Membership

Access information regarding the groups the current user is member of.

getGroups()

Returns an array with the names of groups the current user is member of.

const groups = USERSCRIPT4C_ACTIVE.getGroups();

if (USERSCRIPT4C_ACTIVE.contains('confluence-administrators', groups)) {
 ...
}

isCurrentUserMemberOfAll(...requiredNames)

Provide a list of requiredNames of groups to check that the current user is member of all of them.

if (USERSCRIPT4C_ACTIVE.isCurrentUserMemberOfAll('confluence-administrators', 'userscripts-administrators')) {
 ...
}

The function returns true if the current user is member of all specified groups, false if the user is not member of at least one specified group.

isCurrentUserMemberOfAny(...requiredNames)

Provide a list of requiredNames of groups to check that the current user is member of at least one of them.

if (USERSCRIPT4C_ACTIVE.isCurrentUserMemberOfAny('confluence-administrators', 'userscripts-administrators')) {
 ...
}

The function returns true if the current user is member of at least one of the specified groups, false if the user no member of any of the specified groups.

Space Categories

Access information regarding the categories of the space the current user is visiting.

getSpaceCategories()

Returns an array with the names of categories the current space is tagged with. The current space is the space the user is currently visiting.

const categories = USERSCRIPT4C_ACTIVE.getSpaceCategories();

if (USERSCRIPT4C_ACTIVE.contains('product', categories)) {
 ...
}

isCurrentSpaceOfCategoryAll(...requiredNames)

Provide a list of requiredNames of space categories to check that the current space is tagged with all of them.

if (USERSCRIPT4C_ACTIVE.isCurrentSpaceOfCategoryAll('product', 'tool')) {
 ...
}

The function returns true if the current space is tagged with all specified categories, false if the space is at least not tagged with one of the specified categories.

isCurrentSpaceOfCategoryAny(...requiredNames)

Provide a list of requiredNames of space categories to check that the current space is tagged with at least one of them.

if (USERSCRIPT4C_ACTIVE.isCurrentSpaceOfCategoryAny('product', 'tool')) {
 ...
}

The function returns true if the current space is tagged with at least one of the specified categories, false if the space is not tagged with any of the specified categories.

Page Labels

Access information regarding the labels of the page the current user is visiting.

getPageLabels()

Returns an array with the names of labels the current page is tagged with. The current page is the page the user currently is visiting.

const labels = USERSCRIPT4C_ACTIVE.getPageLabels();

if (USERSCRIPT4C_ACTIVE.contains('important', labels)) {
 ...
}

isCurrentPageLabeledWithAll(...requiredNames)

Provide a list of requiredNames of page labels to check that the current page is tagged with all of them.

if (USERSCRIPT4C_ACTIVE.isCurrentPageLabeledWithAll('important', 'fav')) {
 ...
}

The function returns true if the current page is tagged with all specified labels, false if the page is at least not tagged with one of the specified labels.

isCurrentPageLabeledWithAny(...requiredNames)

Provide a list of requiredNames of page labels to check that the current page is tagged with at least one of them.

if (USERSCRIPT4C_ACTIVE.isCurrentPageLabeledWithAny('important', 'fav')) {
 ...
}

The function returns true if the current page is tagged with at least one of the specified labels, false if the page is not tagged with any of the specified labels.

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.