Issue with script to display the no. of days from the Last modified date

Anusha Wilson May 6, 2024

HI,

I have written the below user macro script to display the pages which have crossed the given number of days from the last modified date. The macro works on few pages. But on few pages the table does not load/the script doesnt work. Could you help to check if something is to be corrected.

 

## @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><td>"+days+"</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

1 accepted

0 votes
Answer accepted
Andrii Maliuta
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 8, 2024

Hello @Anusha Wilson ,

Can you provide the error or issues you get on those pages the macro does not work?

What you need to pay attention to is use of "window.alert()" as the page loading is asynchronous and it can conflict with the AJS modules being loaded on page. You need to check if the page is fully loaded.

The JS code should be inside the "AJS.toInit()" method according to the documentation (https://developer.atlassian.com/server/confluence/including-javascript-and-css-resources/#web-resource-configuration) .

AJS.toInit(function () {
// ... your initialisation code here
});

Also, some pages have last modification date as NULL, so you need to check for this too.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events