User Macro to display the number of days since the last modification date

Anusha Wilson May 6, 2024

Hi,

I am creating a user Macro to display the number of days since the last modification date.It works on few pages,but on few pages it doesnt load. Could you help figure out if there is anything missing

## @Param pageReviewDays:title=Page Review Days|type=int|required=true
<html>
<body>

<h2>Outdated Pages</h2>

<table border=1 id="t1"></table>

</body>
</html>
<script>
window.alert("Setting Parameter");
#set ($limit = $parampageReviewDays)
var element = document.getElementById("t1")
window.alert("Getting descendents $limit");

#foreach ( $descendent in $content.getDescendents() )
var lmdate = "$descendent.getLastModificationDate()";
var lm = lmdate.split(" ")[0];
var lmsplit = lm.split("-");
var lmjoin = lmsplit.join("/");
var limt = "$limit";
var today = new Date();
var date = (today.getFullYear()+'/'+(today.getMonth()+1)+'/'+today.getDate()).toString();
//window.alert(lmjoin);
var days = daysdifference(lmjoin, date);

element. innerHTML = element. innerHTML + "<tr><td><a href="+ contextPath +"$descendent.getUrlPath()> $descendent.title </a></td></tr>";

#end
//window.alert("loaded!")

function daysdifference (firstDate, secondDate){

let startDay = new Date(firstDate);
let endDay = new Date(secondDate);
// Determine the time difference between two dates
let millisBetween = startDay.getTime() - endDay.getTime();
return Math.round(millisBetween / (1000 * 3600 * 24));
}
</script>

1 answer

0 votes
Davin Studer
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 14, 2024

First off you should not include the html or body tags in the user macro. That means that you would have an HTML page structure like this, which is a HUGE HTML no no.

<html>
<body>

... Lot's of Confluence html stuff here ...

<html> --
<body> --
<h2>Outdated Pages</h2> --
<table border=1 id="t1"></table> -- Your user macro code on the left
</body> --
</html> --
<script> --
your JS script --
</script> --

... More of Confluence html stuff here ...

</body>
</html>

 Secondly, your are using velocity to loop through the page descendants and thus duplicating the JavaScript that is output inthe process redeclaring the same JavaScript variables over and over. That is a JavaScript no no.

What are you trying to accomplish with this?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events