Ok so I felt bad that I couldn’t find a tutorial for you that does what you want. So…
https://bitbucket.org/dwester42a/sample-confluence-showing-click-javascript
The basic of this is from the basic atlas-create-confluence-plugin-module command. So let’s get started. First we’ll add the rest module that we can trigger from javascript.
In the pom.xml I added in the rest dependency module: Bitbucket
<dependency>
<groupId>com.atlassian.plugins.rest</groupId>
<artifactId>atlassian-rest-common</artifactId>
<version>2.9.7</version>
<scope>provided</scope>
</dependency>
This will let us get cracking on adding in the end point. This end point is going to be really simple and just return back the current date ( Bitbucket ). The end point is basically Jersey so there’s no Atlassian magic there.
@Path("/time")
public class CurrentDateEndPoint
{
This basically tells us the root of the paths is /time. Our method:
@Path("/fetch")
@GET
@Produces({MediaType.APPLICATION_JSON})
public Response fetchCurrentTime()
{
Date d = new Date();
Map<String, String> values = new HashMap<String, String>();
values.put("date", d.toString());
return Response.ok( values).build();
}
We are responding to the GET method call at /time/fetch. And we’ll return back a application/json representation of the values hash map (this can be anything that Jackson/Jersey can serialize btw).
Then in the atlassian-plugin.xml we declare that we’ve got a rest end point ( Bitbucket ):
<rest key="some-rest-end-point" path="/my-rest-end" version="1.0">
<description>General rest end point</description>
</rest>
At this point we can call the rest end point at /rest/my-rest-end/1.0/time/fetch and get the current time. Let’s wire it up.
First let’s add the button. In the atlassian-plugin.xml ( Bitbucket ) we add:
<web-item key="my-butto" name="Some random button to add int" section="system.dashboard.button" weight="100">
<label>Click me!</label>
<link linkId="my-link-here">/</link>
</web-item>
This will give us the button to appear on the dashboard. Now to wire it up with the rest call we’ll need some javascript. Now note the linkId attribute of the link node above. That usually becomes the id attribute of the link/button but in some rare cases there will be some “extra” prefix/suffixes added depending on the location. In this case we’re good though.
So we create our javascript file ( Bitbucket ).
AJS.toInit(function()
{
is an easy way to be called whenever the Atlassian product deems the page to be ready to be manipulated.
After that it’s mostly normal javascript. With the exception of:
AJS.contextPath()
This will always return back the context path of the web app.
The flag piece:
require(['aui/flag'], function(flag)
{
var myFlag = flag({
type: 'info',
title: 'Current time',
persistent: false,
body: 'The current time is '+ data.date
});
});
is basically from the page at AUI - Documentation
Now we just need to load the javascript on every page so back to atlassian-plugin.xml ( Bitbucket ):
<web-resource key="trigger-javascript-from-web-item-resources" name="trigger-javascript-from-web-item Web Resources">
<dependency>com.atlassian.auiplugin:ajs</dependency>
<dependency>com.atlassian.auiplugin:aui-flag</dependency> <!-- from https://docs.atlassian.com/aui/5.7.48/docs/flag.html -->
<resource type="download" name="trigger-javascript-from-web-item.js" location="/js/trigger-javascript-from-web-item.js"/>
<context>atl.general</context>
</web-resource>
Where atl.general is pretty much every page (pretty much because every once in a while there are pages that don’t have it). The aui-flag dependency allows us to declare that we need access to the javascript that does the flag.
End result is that on the dashboard you get a button:

And when you click it:
