Last active
July 30, 2026 02:39
-
-
Save eyalk11/e30bf9fee5e2b6e634094cf661e0f5a8 to your computer and use it in GitHub Desktop.
Opens html in private github repos from browser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| .SYNOPSIS | |
| Open an .html file that lives in a (possibly private) GitHub repository. | |
| .DESCRIPTION | |
| GitHub serves .html as source, and public preview services cannot reach a | |
| private repository. This downloads the repository archive through your | |
| signed-in browser -- which supplies the GitHub session cookie, so no token or | |
| CLI is needed -- unpacks it, and opens the requested file. Taking the whole | |
| archive rather than a single raw file means relative assets (images, css) | |
| still resolve. | |
| .EXAMPLE | |
| .\Open-RepoHtml.ps1 -Repo eyalk11/fei_conjecture | |
| .\Open-RepoHtml.ps1 -Repo eyalk11/fei_conjecture -Path docs/fei_program_map.html | |
| .\Open-RepoHtml.ps1 -Repo twbs/bootstrap -Ref main -Path index.html -Browser msedge | |
| #> | |
| [CmdletBinding()] | |
| param( | |
| [Parameter(Mandatory)][ValidatePattern('^[^/\s]+/[^/\s]+$')] | |
| [string] $Repo, # owner/name | |
| [string] $Ref = 'master', # branch name | |
| [string] $Path = 'README.html', # file inside the repo | |
| [string] $Browser = 'chrome', # must be signed in to GitHub | |
| [string] $DownloadDir = "$HOME\Downloads", # where the browser | |
| [int] $TimeoutSec = 180, | |
| [switch] $KeepArchive # leave the .zip in | |
| ) | |
| $ErrorActionPreference = 'Stop' | |
| $name = $Repo.Split('/')[-1] | |
| $refSlug = $Ref -replace '/', '-' # GitHub flattens f | |
| $pattern = "$name-$refSlug*.zip" | |
| $url = "https://github.com/$Repo/archive/refs/heads/$Ref.zip" | |
| $out = Join-Path $env:TEMP "$name-$refSlug" | |
| # Clear the previous archive and extraction: the browser will not overwrite a | |
| # download (it would save as "...(1).zip" and we would keep finding t | |
| # one), and Expand-Archive -Force overwrites files but leaves behind any that | |
| # have since been deleted upstream. | |
| Remove-Item (Join-Path $DownloadDir $pattern), $out -Recurse -Force -ErrorAction SilentlyContinue | |
| $t = Get-Date | |
| Write-Verbose "fetching $url via $Browser" | |
| try { Start-Process $Browser $url } | |
| catch { throw "could not launch '$Browser': $($_.Exception.Message)" | |
| do { | |
| Start-Sleep 1 | |
| $zip = Get-ChildItem $DownloadDir -Filter $pattern -ErrorAction S | |
| Sort-Object LastWriteTime -Descending | Select-Object -First 1 | |
| } until ($zip -or ((Get-Date) - $t).TotalSeconds -gt $TimeoutSec) | |
| if (-not $zip) { | |
| throw "no archive matching '$pattern' appeared in $DownloadDir within ${TimeoutSec}s. " + | |
| "Is $Browser signed in to GitHub, does the branch '$Ref' ex | |
| "browser save downloads there without prompting?" | |
| } | |
| Expand-Archive $zip.FullName $out -Force | |
| if (-not $KeepArchive) { Remove-Item $zip.FullName -Force -ErrorAction SilentlyContinue } | |
| $root = (Get-ChildItem $out -Directory | Select-Object -First 1).FullName | |
| $target = Join-Path $root ($Path -replace '/', '\') | |
| if (-not (Test-Path $target)) { | |
| throw "'$Path' is not in the archive. Top level holds: " + | |
| ((Get-ChildItem $root | Select-Object -First 12 -ExpandProperty Name) -join ', ') | |
| } | |
| Invoke-Item $target | |
| $target # so the caller can reuse the extracted path |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Opens html in private github repos from browser.