You are viewing an old version of this page. View the current version.
Compare with Current
View Page History
« Previous
Version 3
Next »
A short introduction to write userscripts for the Userscripts for Confluence App.
A Userscript is executed in the Confluence environment. Therefore the usual JavaScript infrastructure, like access to libraries and context information via AJS, is available.
Basics
To ensure that the AJS infrastructure is fully initialized when the userscript is executed, wrap your code inside this:
"use strict";
AJS.toInit(function () {
// your code starts here ...
}
The use of the strict mode is recommended, but not strictly necessary.
The AJS object allows to access context information via AJS.params
. AJS.params.remoteUser
provides access to the remote user.
Since AJS wraps the libraries provided by Confluence, including jQuery, elements of a page can be accessed via AJS.$('#my-selector') (see Selecting Elements on the jQuery's website).
This a very short userscript to hide the browse-menu-link for anonymous users, as shown in the example on How to use JavaScript in Confluence by Atlassian. would look like this:
"use strict";
AJS.toInit(function () {
if (AJS.params.remoteUser == '') {
AJS.$('#browse-menu-link').hide();
}
}
This code would hide the referenced element for anonymous users.