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
- group membership of the current user
- categories attached to the space visited by the current user
- 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)
Returnstrueif every element in the arrayrequiredValuesis also a member in the arrayactualValues,falseif at least one element inrequiredValuesis not element inactualValues.containsAny(requiredValues, actualValues)
Returnstrueif at least one element in the arrayrequiredValuesis also a member in the arrayactualValues,falseif no element inrequiredValuesis also a member ofactualValues.contains(requiredElement, actualValues)
Returnstrueif therequiredElementis a member in the arrayactualValues,falseif 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.