This should only show the number of pages visible to you, the current user.

<div id="publicSpacesTable">Loading...</div>

<script>
(async function() {
  const tableContainer = document.getElementById('publicSpacesTable');
  tableContainer.innerHTML = 'Fetching public spaces...';

  async function fetchPublicSpaces(start = 0, limit = 50, accumulated = []) {
    const res = await fetch(`/rest/api/space?label=public&limit=${limit}&start=${start}`);
    const data = await res.json();
    const all = accumulated.concat(data.results);
    if (data._links.next) {
      return fetchPublicSpaces(start + limit, limit, all);
    }
    return all;
  }

  async function countPagesInSpace(spaceKey) {
    const res = await fetch(`/rest/api/search?cql=space=${spaceKey}+AND+type=page&limit=1`);
    const data = await res.json();
    return data.totalSize || 0;
  }

  try {
    const publicSpaces = await fetchPublicSpaces();

    if (publicSpaces.length === 0) {
      tableContainer.innerHTML = '<p>No public spaces found.</p>';
      return;
    }

    // Now fetch page counts in parallel
    const results = await Promise.all(publicSpaces.map(async space => {
      const pageCount = await countPagesInSpace(space.key);
      return {
        name: space.name,
        link: space._links.webui,
        count: pageCount
      };
    }));

    // Build table
    const table = document.createElement('table');
    table.style.borderCollapse = 'collapse';
    table.style.width = '100%';

    const headerRow = table.insertRow();
    ['Space Name', 'Page Count'].forEach(text => {
      const th = document.createElement('th');
      th.innerText = text;
      th.style.borderBottom = '1px solid #ccc';
      th.style.textAlign = 'left';
      th.style.padding = '6px';
      headerRow.appendChild(th);
    });

    for (const space of results) {
      const row = table.insertRow();

      const nameCell = row.insertCell();
      const link = document.createElement('a');
      link.href = space.link;
      link.target = '_blank';
      link.innerText = space.name;
      nameCell.appendChild(link);
      nameCell.style.padding = '6px';

      const countCell = row.insertCell();
      countCell.innerText = space.count;
      countCell.style.padding = '6px';
    }

    tableContainer.innerHTML = '';
    tableContainer.appendChild(table);
  } catch (error) {
    console.error(error);
    tableContainer.innerHTML = 'Error fetching public spaces.';
  }
})();
</script>