Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Headless downloads #100

Open
Nitemaeric opened this issue Oct 22, 2024 · 3 comments
Open

Headless downloads #100

Nitemaeric opened this issue Oct 22, 2024 · 3 comments
Labels
enhancement New feature or request has workaround

Comments

@Nitemaeric
Copy link

Nitemaeric commented Oct 22, 2024

I'm not sure if it's currently possible with Astral, but how would one allow headless downloads?

https://issues.chromium.org/issues/41304545#comment40

It seems that Browser.setDownloadBehavior needs to be configured with { behaviour: "allow", downloadPath: path } via CDP?

@lino-levan lino-levan added the enhancement New feature or request label Oct 22, 2024
@dex-labs
Copy link

@Nitemaeric try this.

const page = await browser.newPage("https://google.com");

const celestial = page.unsafelyGetCelestialBindings();

await celestial.Browser.setDownloadBehavior({ behavior: "allow", downloadPath: "./downloads"});

@Nitemaeric
Copy link
Author

@Nitemaeric try this.

const page = await browser.newPage("https://google.com");

const celestial = page.unsafelyGetCelestialBindings();

await celestial.Browser.setDownloadBehavior({ behavior: "allow", downloadPath: "./downloads"});

Sorry for the slow response, I've just gotten round to testing this out. This worked great!

One thing comes to mind though, is there a way to await for the download to complete? Something similar to await page.waitForNetworkIdle({ idleConnections: 0, idleTime: 1000 });?

@dex-labs
Copy link

dex-labs commented Jan 24, 2025

AS far as i can tell, not natively within the browser / astral. But we did have a similar issue and found a way around it.

Helper functions:

const getFolderFileCount = (folderPath: string) => {

    let count = 0;

    for (const file of Deno.readDirSync(folderPath)) {
        count++;
    }

    return count;

}

const waitForBool = async (callback: Function, interval: number = 1000) : Promise<boolean> => {

    if (await callback()) return true;

    await new Promise((resolve) => setTimeout(resolve, interval));

    return await waitForBool(callback, interval);

}

Check for new files in download folder before and after the download action is triggered.

const numberOfFiles = getFolderFileCount(storage);

//do action to begin download

await waitForBool(async () => {
    return getFolderFileCount(storage) > numberOfFiles;
})

This won't account for edge cases like files taking too long to download or downloads that stop but it does cover a successful download pretty well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request has workaround
Projects
None yet
Development

No branches or pull requests

3 participants