Skip to content

Instantly share code, notes, and snippets.

@brunos3d
Created November 11, 2021 21:38
Show Gist options
  • Save brunos3d/4eaedbbd701e16ef736d1f1725fe8691 to your computer and use it in GitHub Desktop.
Save brunos3d/4eaedbbd701e16ef736d1f1725fe8691 to your computer and use it in GitHub Desktop.
This snippet archive all prismic documents

Prismic documents archiver

Archive all prismic documents

Warnings

There are risks when using this script

⚠️ This snippet will archive all prismic documents

⚠️ Use it at your own risk

⚠️ I am not responsible for damages, penalties and/or possible bans

How to use it

  • Open your browser
  • Go to your prismic dashboard
  • open and paste the script bellow into your console
(async () => {
  const MAX_QUERY_ITEMS = 1000;
  const ANTI_DDOS_DELAY_MS = 500;

  function getCookie(name) {
    const value = `; ${document.cookie}`;
    const parts = value.split(`; ${name}=`);
    if (parts.length === 2) return parts.pop().split(';').shift();
  }

  function wait(milliseconds) {
    return new Promise((resolve) => setTimeout(resolve, milliseconds));
  }

  const docsRes = await fetch(`/app/documents/working?language=pt-br&page=1&limit=${MAX_QUERY_ITEMS}`, {
    method: 'GET',
    credentials: 'include',
  });

  if (!docsRes.ok) {
    return;
  }

  const docs = await docsRes.json();

  const publicDocs = docs.filter((doc) => doc.versionStatus === 'published');
   
  console.log(`Moving ${publicDocs.length} documents to archive!!!`);
  
  for (const doc of publicDocs) {
    console.log('Moving to archive', doc.title);

    const csrf = getCookie('X_XSRF'); // get rotating token

    await fetch(`/app/documents/${doc.documentId}/versions/${doc.versionId}/archive?context=published&_=${csrf}`, {
      method: 'POST',
      credentials: 'include',
    });

    await wait(ANTI_DDOS_DELAY_MS);
  }
  
  console.log('Archive process finished');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment