Skip to content

Allow downloads when headless, via Chrome Command #33

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

Merged
merged 3 commits into from
Aug 27, 2019
Merged

Allow downloads when headless, via Chrome Command #33

merged 3 commits into from
Aug 27, 2019

Conversation

Windos
Copy link
Contributor

@Windos Windos commented Aug 27, 2019

I've been using this module to automate the bulk download of CSV files from our RMM system (because their API is rubbish!), and today went to try headless with the beta for when it's running on a schedule.

Unfortunately, downloading and headless is disabled by default for security.

Luckily, from some Chromium 62 build and onward it's possible to enable it.

This PR executes a Chrome Command, allowing downloads to the DefaultDownloadPath if both the path and -Headless has been specified.

p.s. I'm sorry about all the misc other small edits in the file. VS Code cleaned up a bunch of trailing white space and I didn't spot it in the diff.

@the-mentor
Copy link
Collaborator

the-mentor commented Aug 27, 2019

@Windos is there no chrome_options object/way that will do the same thing ?
The reason I'm asking is because that will probably be a nicer/cleaner implementation.

@Windos
Copy link
Contributor Author

Windos commented Aug 27, 2019

No that I'd found. I did wonder and do a quick Google... but could well have missed something.

@the-mentor
Copy link
Collaborator

the-mentor commented Aug 27, 2019

@Windos can you check if the code below works for you ?

        if($DefaultDownloadPath){
            Write-Verbose "Setting Default Download directory: $DefaultDownloadPath"
            $Chrome_Options.AddUserProfilePreference('download', @{'default_directory' = $($DefaultDownloadPath.FullName); 'prompt_for_download' = $false; })
            if($Headless){
                $Chrome_Options.AddUserProfilePreference('browser', @{'setDownloadBehavior' = 'allow' })
                #chromePrefs.put("Browser.setDownloadBehavior", "allow");
            }
        }

@Windos
Copy link
Contributor Author

Windos commented Aug 27, 2019

Unfortunately, no luck on that. I also tried (and it didn't work):

if($Headless){
    $Chrome_Options.AddUserProfilePreference('Page.setDownloadBehavior', @{'behavior' = 'allow'; 'downloadPath' = $DefaultDownloadPath.FullName})
}

No errors or anything when making those changes, just no download.

@the-mentor
Copy link
Collaborator

the-mentor commented Aug 27, 2019

@adamdriscoll @Windos this PR also has a bunch of formatting changes and as more people start working on the project we should probably discuses some form of standard for the project's code formatting so we don't end up with new formatting all the time.
I'm fine with whatever we pick but we should stick to it.

@Windos

  1. We should add a test for this scenario to our Pester tests.
  2. I'll test this PR on Linux a bit later since we are now cross platform I want to make sure it doesn't break Linux support.
  3. Its a bit strange that we need to specify the download folder twice.
  4. Just FYI: It looks like when downloading large files (I tested with a 100MB file) the script continue running while the download is still in progress.

PS. Thank you very much for the PR and contribution!!

@Windos
Copy link
Contributor Author

Windos commented Aug 27, 2019

I can go back and revert those formatting changes, I'll just need to remember to turn off the formatter when I send through future PRs lol.

My understanding of specifying it a second time is that you're granting permission to download to that folder. In theory if it tries to download to any other folder it'll fail.

I thought the script continuing was a standard thing, headless or not? I'm sure that's why I've got a sleep after each download to make doubly sure it's on disk. I am only dealing with tiny files though, not large ones.

It does make sense as far as browser operation goes, you can start a download and then continue off onto other pages, etc. while it finishes. It's just a pain if you're wanting to act on those files right away.

@adamdriscoll
Copy link
Owner

Looks good. Thanks!!

@adamdriscoll adamdriscoll merged commit b76093f into adamdriscoll:master Aug 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants