Skip to content

Instantly share code, notes, and snippets.

@ozencb
Created December 26, 2020 18:00
Show Gist options
  • Select an option

  • Save ozencb/ac7ed14ae87aac5ce49e5c2dd376b42c to your computer and use it in GitHub Desktop.

Select an option

Save ozencb/ac7ed14ae87aac5ce49e5c2dd376b42c to your computer and use it in GitHub Desktop.

Revisions

  1. ozencb created this gist Dec 26, 2020.
    59 changes: 59 additions & 0 deletions archiveCourses.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    // Archive every course on udemy.com
    // This script is pretty much a copy of: https://gist.github.com/JamieMason/7580315
    //
    // 1. Go to https://www.udemy.com/home/my-courses/learning/
    // 2. Open the Developer Console. (COMMAND+ALT+I on Mac)
    // 3. Paste this into the Developer Console and run it


    (() => {
    const $archiveButtons = '[data-purpose$="toggle-archived"]';

    const retry = {
    count: 0,
    limit: 3,
    };

    const retryLimitReached = () => retry.count === retry.limit;
    const addNewRetry = () => retry.count++;

    const sleep = ({ seconds }) =>
    new Promise((proceed) => {
    console.log(`Waiting for ${seconds} seconds...`);
    setTimeout(proceed, seconds * 1000);
    });

    const archiveAll = async (archiveButtons) => {
    console.log(`Archiving ${archiveButtons.length} courses...`);
    await Promise.all(
    archiveButtons.map(async (archiveButton) => {
    archiveButton && archiveButton.click();
    await sleep({ seconds: 1 });
    })
    );
    };

    const nextBatch = async () => {
    await sleep({ seconds: 2 });

    const archiveButtons = Array.from(document.querySelectorAll($archiveButtons));
    const archiveButtonsWereFound = archiveButtons.length > 0;

    if (archiveButtonsWereFound) {
    await archiveAll(archiveButtons);
    await sleep({ seconds: 3 });
    return nextBatch();
    } else {
    addNewRetry();
    }

    if (retryLimitReached()) {
    console.log(`No more courses left`);
    } else {
    await sleep({ seconds: 3 });
    return nextBatch();
    }
    };

    nextBatch();
    })();