Page tree

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.

Iteration
Filled

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.

Resources

How to use JavaScript in Confluence
Provides basic information on how to use JavaScript on Confluence. Userscripts provide an alternative way to integrate JavaScript code, but the design rules for writing JavaScript code for Confluence still apply.
Atlassian User Interface
AUI is a tailor-made frontend library for creating a user interface according to the Atlassian Design Guidelines.
JQuery Learning Center
Information on how to use jQuery on the jQuery website.
JavaScript
Information on JavaScript on the MDN web docs website.
  • No labels