Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

HTML
<script>

function
 download_file(data, fileName) {
	
var fileData = data;
	
var byteChars = $.base64('decode', fileData);
	
	
var ab = new ArrayBuffer(byteChars.length);
	
var ia = new Uint8Array(ab);
	
for (var i = 0; i < byteChars.length; i++) {
		ia[i] = byteChars.charCodeAt(i);
	}				
	
var blob = new Blob([ab], { type: 'application/pdf' }); 
	
	
if (window.navigator.msSaveOrOpenBlob) { // IE hack; see http://msdn.microsoft.com/en-us/library/ie/hh779016.aspx
 	    window.navigator.msSaveOrOpenBlob(blob, fileName);
	   
	}
	
else {
		
var downloadUrl = URL.createObjectURL(blob);
		$(
"#download")
		.attr({
			
'download': fileName,
			
'href': downloadUrl
		});	
		$(
"#download")[0].click();
		window.history.back();
	}
	
};


function getQueryParameters() {
    var queryParams = {};
    var query = window.location.search.substring(1);
    var pairs = query.split('&');
    for (var i = 0; i < pairs.length; i++) {
        var pair = pairs[i].split('=');
        var key = decodeURIComponent(pair[0]);
        var value = decodeURIComponent(pair[1]);
        queryParams[key] = value;
    }
    return queryParams;
}

function updateMacroOnExistingPage() {

	console.log("updateMacroOnExistingPage");

    var queryParams = {
        param1: 'new-value1',
        param2: 'new-value2'
    };

    // Define the macro name and parameters based on query parameters
    var macroName = 'Mineaftaler';
    var macroParameters = {
 		REST_HTML_URL: 'http://foedus01.nsi.netic.dk:8080/NSI_Aftaler-1.1.1-SNAPSHOT/rest/html',
	    REST_DOWNLOAD_URL: 'http://foedus01.nsi.netic.dk:8080/NSI_Aftaler-1.1.1-SNAPSHOT/rest/base64encodefilebytes',
	    include_user_info: true
     };

    // Generate the updated macro markup
    var updatedMacroMarkup = '{' + macroName + ':';
    for (var key in macroParameters) {
        if (macroParameters.hasOwnProperty(key)) {
            updatedMacroMarkup += key + '=' + macroParameters[key] + '|';
        }
    }
    // Remove the trailing '|' character
    updatedMacroMarkup = updatedMacroMarkup.slice(0, -1);
    updatedMacroMarkup += '}';

    // Page ID of the existing Confluence page
    var pageId = '33594461'; // Replace with the actual Page ID

    // Make an AJAX request to retrieve the current page content
    AJS.$.ajax({
        url: '/rest/api/content/' + pageId + '?expand=body.storage,version',
        type: 'GET',
        success: function(data) {
            // Get the current page content

			console.log("Get content ok - data: ", data);

            var currentContent = data.body.storage.value;
            
            // Replace the old macro markup with the updated macro markup
            var updatedContent = currentContent.replace(/(\{MineAftaler:[^\Mineaftaler:REST_HTML_URL=http://foedus01.nsi.netic.dk:8080/NSI_Aftaler-1.1.1-SNAPSHOT/rest/html|REST_DOWNLOAD_URL=http://foedus01.nsi.netic.dk:8080/NSI_Aftaler-1.1.1-SNAPSHOT/rest/base64encodefilebytes|include_user_info=true}]+\})/g, updatedMacroMarkup);

            // Create a JSON payload for the REST API request to update the page
            var payload = {
                version: {
                   number: 87 // Increment this number if you want to update the page content
                },  
                type: 'page',      
                title: 'Mine aftaler',
                body: {
                    storage: {
                        value: updatedContent,
                        representation: 'storage'
                    }
                }
            };

            // Make an AJAX request to update the page content
            AJS.$.ajax({
                url: '/rest/api/content/' + pageId,
                type: 'PUT',
                contentType: 'application/json',
                data: JSON.stringify(payload),
                success: function(data) {
                    console.log('Macro on the page updated successfully.');
                },
                error: function(error) {
                    console.error('Error updating macro on the page:', error);
                }
            });
        },
        error: function(error) {
            console.error('Error retrieving page content:', error);
        }
    });
}
AJS.toInit(function() {
    updateMacroOnExistingPage();
});

</script>

...