• You need the administrator rights in Confluence
  • Go to the User Macros configuration page
  • Click on Create a User Macro

Configure the User macro:

## @noparams
  <button class="aui-button" onclick="removeLabels()">Remove all labels</button>
  <script>
    var removeLabels = function() {
      jQuery.ajax({
        url: contextPath + "/rest/api/content/" + $content.id + "?expand=metadata.labels",
        success: function(response) {
          jQuery(response.metadata.labels.results).each(function() {
            jQuery.post(contextPath+'/json/removelabelactivity.action', {'entityIdString': '$content.id', 'labelIdString': this.name, 'atl_token': jQuery('#atlassian-token').attr('content') });
          });
        }
      });
      jQuery("li.aui-label").remove();
      jQuery("ul.label-list-right").prepend('<li class="no-labels-message">No labels</li>');
      alert("All labels removed.");
    }
  </script>

The variable $content.id contains the PageId reference and is inherited in the User Macro code

We use in this case jQuery to do actions in the Macro calling REST endpoints of our Confluence

That’s all! Here the result: go to some page, insert the new Macro and add some “labels” to the page. Once added push the button “Remove all labels

Proposed exercise: Create a new Macro that removes automatically all labels when you see the Confluencepage

Tutorial: How to create the exercise Macro using TamperMonkey in Google Chrome (to easy inject and develop jQuery in Confluence)

// ==UserScript==
// @name         Confluence remove all labels button
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://jira.mycompany.com/wiki/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=edreamsodigeo.com
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...
    var getUrlParameter = function getUrlParameter(sParam) {
        var sPageURL = window.location.search.substring(1),
            sURLVariables = sPageURL.split('&'),
            sParameterName,
            i;

        for (i = 0; i < sURLVariables.length; i++) {
            sParameterName = sURLVariables[i].split('=');

            if (sParameterName[0] === sParam) {
                return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
            }
        }
        return false;
    };


    jQuery("div[id=main-content]").append('<button class="aui-button" id="remove-labels">Remove all labels</button>')
    jQuery("#remove-labels").click(function(){removeLabels()})
    var removeLabels = function() {
        //var content = {id:278790147} //IMPORTANT Page to do the test (This is filled automatically in Confluence inside the UserMacros
        var content = {id:getUrlParameter('pageId')}

        console.log("content value: " + content.id)

        jQuery.ajax({
            url: contextPath + "/rest/api/content/" + content.id + "?expand=metadata.labels",
            success: function(response) {
                jQuery(response.metadata.labels.results).each(function() {
                    jQuery.post(contextPath+'/json/removelabelactivity.action', {'entityIdString': content.id, 'labelIdString': this.name, 'atl_token': jQuery('#atlassian-token').attr('content') });
                    console.log("Label Id removed " +this.name );
                });
            }
        });
        jQuery("li.aui-label").remove();
        jQuery("ul.label-list-right").prepend('<li class="no-labels-message">No labels</li>');
        alert("All labels removed.");
    }


})();

Note some things:

  • We use // @match https://jira.mycompany.com/wiki/* in the code to match our Confluence pages, change the url to your instance
  • $content.id is not inherited in TamperMonkey and we need to simulate, examples:
    • var content = {id:getUrlParameter(‘pageId’)} //Note we use another function to get URL Params
  • var content = {id:278790147} //Note content variable is without $ symbol
  • We create the button in different way using jQuery
 jQuery("div[id=main-content]").append('<button class="aui-button" id="remove-labels">Remove all labels</button>')
 jQuery("#remove-labels").click(function(){removeLabels()})

You can use TamperMonkey to inject the script and see the User Macro results and continue developing. Once create the script you must transform a little bit as you can see

MrAddon by TecnoFor

.

Posted by:.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s