Confluence example: Show recent page contributors
This fragment lists the people who changed the current page recently. It reads the page's versions and shows each author once, so you can see who to ask about the page.
Applies to: Script Master for Confluence Β· Cloud. The editor's Recent contributors template does the same job.
Set it upβ
- Create a fragment with Location set to Page byline item.
- Paste the code below into Frontend HTML + JavaScript.
- In the preview, pick a space and a page with a few versions, and check the list.
- Turn on Enable and select Save.
Result: Under a page title, Custom Actions lists the fragment. Selecting it shows the recent contributors, separated by commas.
The codeβ
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@atlaskit/css-reset@3.0.1/dist/bundle.css" />
<div>
Recent contributors: <div id="contributors">Loading...</div>
</div>
<div id="errors" style="color:red"></div>
<script type="module">
// Show uncaught errors in the page
window.onerror = (e) => document.getElementById('errors').textContent = e.toString();
// Follow the viewer's light or dark theme
document.querySelector(":root").setAttribute("data-color-mode", theme.colorMode);
document.querySelector(":root").setAttribute("data-theme", `${theme.colorMode}:${theme.colorMode}`);
document.querySelector(":root").insertAdjacentHTML("afterbegin", `<link rel="stylesheet" href="https://forge.cdn.prod.atlassian-dev.net/atlaskit-tokens_${theme.colorMode}.css" />`);
const context = await view.getContext();
const pageId = context.extension.content.id;
const versionsResponse = await requestConfluence(`/wiki/api/v2/pages/${pageId}?include-version=false&include-versions=true`, {
headers: { 'Accept': 'application/json' }
});
if (versionsResponse.status !== 200) throw new Error(`Error getting page versions ${versionsResponse.statusText}`);
const pageWithVersions = await versionsResponse.json();
const authorIds = [...new Set(pageWithVersions.versions.results.map(version => version.authorId))];
const authorsQuery = authorIds.map(authorId => `accountId=${authorId}&`).join('');
const authorsResponse = await requestConfluence(`/wiki/rest/api/user/bulk?${authorsQuery}`, {
headers: { 'Accept': 'application/json' }
});
if (authorsResponse.status !== 200) throw new Error(`Error getting contributor details ${authorsResponse.statusText}`);
const contributorsData = await authorsResponse.json();
const names = [...new Set(contributorsData.results.map(contributor => contributor.displayName))];
document.getElementById('contributors').textContent = names.length ? names.join(', ') : 'no contributors';
</script>
Relatedβ
- Write fragment code: the context fields a byline item gets.
- Fragments: every location and how people open it.