|
| 1 | +if($ENV:OS -ne 'Windows_NT'){ |
| 2 | + Write-Host '請在 Windows 作業系統上使用!' -ForegroundColor Red |
| 3 | + return |
| 4 | +} |
| 5 | +if($PSVersionTable.PSVersion.Major -lt 3){ |
| 6 | + Write-Host "請使用 PowerShell 主要版本 >= 3, 目前版本為 $($PSVersionTable.PSVersion.Major)" -ForegroundColor Red |
| 7 | + return |
| 8 | +} |
| 9 | +$installLocation = $env:ggdir |
| 10 | +$arch = $env:ggarch |
| 11 | +#$branch = $env:ggbranch |
| 12 | +$ggApi = 'https://api.pzhacm.org/iivb/cu.json' |
| 13 | +$gcApi = 'https://api.pzhacm.org/iivb/gc.json' |
| 14 | + |
| 15 | +if($PSVersionTable.PSVersion.Major -lt 5){ |
| 16 | + if (-not ([System.Management.Automation.PSTypeName]'Branch').Type){ |
| 17 | + Add-Type -TypeDefinition @" |
| 18 | + public enum Branch |
| 19 | + { |
| 20 | + Stable, |
| 21 | + Beta, |
| 22 | + Dev, |
| 23 | + Canary |
| 24 | + } |
| 25 | +"@ |
| 26 | + } |
| 27 | +} |
| 28 | +else{ |
| 29 | + Enum Branch{ |
| 30 | + Stable |
| 31 | + Beta |
| 32 | + Dev |
| 33 | + Canary |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +#$arch check |
| 38 | +if ([string]::IsNullOrEmpty($arch)){ |
| 39 | + if($ENV:PROCESSOR_ARCHITECTURE -eq 'AMD64'){ |
| 40 | + $arch = 'x64' |
| 41 | + } |
| 42 | + else{ |
| 43 | + $arch = 'x86' |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +#$installLocation check |
| 48 | +if ([string]::IsNullOrEmpty($installLocation)){ |
| 49 | + $installLocation = (Resolve-Path .\).Path |
| 50 | +} |
| 51 | + |
| 52 | +#$branch check |
| 53 | +switch ($env:ggbranch) |
| 54 | +{ |
| 55 | + 'canary' {$branch = [Branch]::Canary} |
| 56 | + 'dev' {$branch = [Branch]::Dev} |
| 57 | + 'beta' {$branch = [Branch]::Beta} |
| 58 | + default {$branch = [Branch]::Stable} |
| 59 | +} |
| 60 | +#if([Enum]::Getvalues([Branch]) -contains $branch) { |
| 61 | +# $Branch = [Enum]::Parse([Type]"Branch",$branch) |
| 62 | +#} |
| 63 | +Write-Host "目前設定分支為 " -NoNewline -ForegroundColor DarkYellow |
| 64 | +Write-Host $branch -ForegroundColor Green |
| 65 | +if ($env:TEMP -eq $null) { |
| 66 | + $env:TEMP = Join-Path $installLocation 'temp' |
| 67 | +} |
| 68 | + |
| 69 | +function Check-InstallLocation { |
| 70 | + if((Test-Path $installLocation)){ |
| 71 | + if((Test-Path "$installLocation\chrome.exe")){ |
| 72 | + $onlineVersion = [System.Version]($JSON.$branch.$arch.version) |
| 73 | + $localVersion = (Get-Item "$installLocation\chrome.exe").VersionInfo.FileVersion |
| 74 | + if($onlineVersion -gt $localVersion){ |
| 75 | + Write-Host " 線上版本為 " -NoNewline |
| 76 | + Write-Host $onlineVersion -NoNewline -ForegroundColor Green |
| 77 | + Write-Host ", 本機版本為 " -NoNewline |
| 78 | + Write-Host $localVersion -NoNewline -ForegroundColor Yellow |
| 79 | + Write-Host ', 讓我們更新吧!' |
| 80 | + } |
| 81 | + else{ |
| 82 | + Write-Host "你已使用最新版本($localVersion)/$branch/$arch" -ForegroundColor Green |
| 83 | + return $false |
| 84 | + } |
| 85 | + } else { |
| 86 | + if(-Not ((Get-ChildItem $installLocation | Measure-Object).Count -eq 0)){ |
| 87 | + Write-Host '請在空資料夾中运行!' -ForegroundColor Red |
| 88 | + return $false |
| 89 | + } |
| 90 | + } |
| 91 | + } else { |
| 92 | + Write-Host "建立目錄 $installLocation" -ForegroundColor Yellow |
| 93 | + New-Item -ItemType Directory -Force -Path $installLocation | Out-Null |
| 94 | + } |
| 95 | + return $true |
| 96 | +} |
| 97 | + |
| 98 | +function Download-String { |
| 99 | +param ( |
| 100 | + [string]$url |
| 101 | + ) |
| 102 | + $downloader = new-object System.Net.WebClient |
| 103 | + $defaultCreds = [System.Net.CredentialCache]::DefaultCredentials |
| 104 | + if ($defaultCreds -ne $null) { |
| 105 | + $downloader.Credentials = $defaultCreds |
| 106 | + } |
| 107 | + $downloader.Proxy = [System.Net.GlobalProxySelection]::GetEmptyWebProxy() |
| 108 | + return $downloader.DownloadString($url) |
| 109 | +} |
| 110 | + |
| 111 | +function Download-File { |
| 112 | +param ( |
| 113 | + [string]$url, |
| 114 | + [string]$targetFile |
| 115 | + ) |
| 116 | + #https://blogs.msdn.microsoft.com/jasonn/2008/06/13/downloading-files-from-the-internet-in-powershell-with-progress/ |
| 117 | + $uri = New-Object "System.Uri" "$url" |
| 118 | + $request = [System.Net.HttpWebRequest]::Create($uri) |
| 119 | + $request.Proxy = [System.Net.GlobalProxySelection]::GetEmptyWebProxy() |
| 120 | + $request.Timeout = 60000 #60 second timeout |
| 121 | + $response = $request.GetResponse() |
| 122 | + $totalLength = [System.Math]::Floor($response.get_ContentLength()/1024) |
| 123 | + $responseStream = $response.GetResponseStream() |
| 124 | + $targetStream = New-Object -TypeName System.IO.FileStream -ArgumentList $targetFile, Create |
| 125 | + $buffer = new-object byte[] 256KB |
| 126 | + $count = $responseStream.Read($buffer,0,$buffer.length) |
| 127 | + $downloadedBytes = $count |
| 128 | + while ($count -gt 0) |
| 129 | + { |
| 130 | + $targetStream.Write($buffer, 0, $count) |
| 131 | + $count = $responseStream.Read($buffer,0,$buffer.length) |
| 132 | + $downloadedBytes = $downloadedBytes + $count |
| 133 | + Write-Progress -activity "正在下載檔案 '$($url.split('/') | Select -Last 1)'" -status "下載進度 (已下載 $([System.Math]::Floor($downloadedBytes/1024))K , 檔案大小 $($totalLength)K): " -PercentComplete ((([System.Math]::Floor($downloadedBytes/1024)) / $totalLength) * 100) |
| 134 | + } |
| 135 | + Write-Progress -activity "已完成 '$($url.split('/') | Select -Last 1)' 下載" -Status "Ready" -Completed |
| 136 | + $targetStream.Flush() |
| 137 | + $targetStream.Close() |
| 138 | + $targetStream.Dispose() |
| 139 | + $responseStream.Dispose() |
| 140 | +} |
| 141 | + |
| 142 | +function Extract-File { |
| 143 | +param ( |
| 144 | + [string]$fileName, |
| 145 | + [string]$dest |
| 146 | + ) |
| 147 | + #WARNING: this function copy from chocolatey.org install.ps1 |
| 148 | + #Write-Host "Extract $fileName to $dest" -ForegroundColor Yellow |
| 149 | + Write-Host "正在解壓縮 $($fileName.split('\') | Select -Last 1)" -ForegroundColor Yellow |
| 150 | + $7zaExe = Join-Path $env:TEMP '7za.exe' |
| 151 | + if (-Not (Test-Path ($7zaExe))) { |
| 152 | + Write-Output "為了解壓縮 Chrome 安裝包,正在下載 7-Zip 命令列工具。" |
| 153 | + Download-File 'https://chocolatey.org/7za.exe' "$7zaExe" |
| 154 | + } |
| 155 | + $params = "x -o`"$dest`" -bd -y `"$fileName`"" |
| 156 | + $process = New-Object System.Diagnostics.Process |
| 157 | + $process.StartInfo = New-Object System.Diagnostics.ProcessStartInfo($7zaExe, $params) |
| 158 | + $process.StartInfo.RedirectStandardOutput = $true |
| 159 | + $process.StartInfo.UseShellExecute = $false |
| 160 | + $process.StartInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden |
| 161 | + $process.Start() | Out-Null |
| 162 | + $process.BeginOutputReadLine() |
| 163 | + $process.WaitForExit() |
| 164 | + $exitCode = $process.ExitCode |
| 165 | + $process.Dispose() |
| 166 | + |
| 167 | + $errorMessage = "無法使用 7-Zip 解壓縮 Chrome 安裝包。 錯誤:" |
| 168 | + switch ($exitCode) { |
| 169 | + 0 { break } |
| 170 | + 1 { throw "$errorMessage Some files could not be extracted" } |
| 171 | + 2 { throw "$errorMessage 7-Zip encountered a fatal error while extracting the files" } |
| 172 | + 7 { throw "$errorMessage 7-Zip command line error" } |
| 173 | + 8 { throw "$errorMessage 7-Zip out of memory" } |
| 174 | + 255 { throw "$errorMessage Extraction cancelled by the user" } |
| 175 | + default { throw "$errorMessage 7-Zip signalled an unknown error (code $exitCode)" } |
| 176 | + } |
| 177 | + |
| 178 | +} |
| 179 | + |
| 180 | +function Remove-IfExists { |
| 181 | +param ( |
| 182 | + [string]$file |
| 183 | + ) |
| 184 | + if(Test-Path $file){ |
| 185 | + Remove-Item $file |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +function Download-Chrome { |
| 190 | + $chrome7z = Join-Path $installLocation 'chrome.7z' |
| 191 | + $url = $JSON.$branch.$arch.cdn |
| 192 | + $downloadFileName = Join-Path $installLocation $($url.split('/') | Select -Last 1) |
| 193 | + Download-File $url $downloadFileName |
| 194 | + if(-Not (Test-Path $downloadFileName)){ |
| 195 | + Write-Host '下載 Chrome 失敗!' -ForegroundColor Red |
| 196 | + return |
| 197 | + } |
| 198 | + $hash = (Get-FileHash $downloadFileName -Algorithm SHA256).Hash |
| 199 | + if($hash -ne $JSON.$branch.$arch.sha256){ |
| 200 | + Write-Host "SHA256 不相符!" -ForegroundColor Red |
| 201 | + Remove-IfExists $downloadFileName |
| 202 | + return; |
| 203 | + } |
| 204 | + Extract-File $downloadFileName $installLocation |
| 205 | + Extract-File $chrome7z $installLocation |
| 206 | + Remove-IfExists $chrome7z |
| 207 | + Move-Item "$installLocation\Chrome-bin\*" -Destination $installLocation |
| 208 | + Remove-IfExists "$installLocation\Chrome-bin" |
| 209 | + Remove-IfExists $downloadFileName |
| 210 | + Write-Host '已完成 Chrome 下載' -ForegroundColor Green |
| 211 | +} |
| 212 | + |
| 213 | +function Check-GCInstallLocation { |
| 214 | + if(Test-Path $gcdllpath){ |
| 215 | + $hash = (Get-FileHash $gcdllpath -Algorithm SHA1).Hash |
| 216 | + } else { |
| 217 | + $hash = '' |
| 218 | + } |
| 219 | + if($arch -eq 'x64'){ |
| 220 | + if($hash -eq $GCJSON.link.x64.sha1){ |
| 221 | + Write-Host "$gcdll 已是最新版本!" -ForegroundColor Green |
| 222 | + return $false |
| 223 | + } |
| 224 | + } else { |
| 225 | + if($hash -eq $GCJSON.link.x86.sha1){ |
| 226 | + Write-Host "$gcdll 已是最新版本!" -ForegroundColor Green |
| 227 | + return $false |
| 228 | + } |
| 229 | + } |
| 230 | + Write-Host "$gcdll 需要更新! 更新内容:" -NoNewline -ForegroundColor Yellow |
| 231 | + Write-Host $GCJSON.description -ForegroundColor Gray |
| 232 | + return $true |
| 233 | +} |
| 234 | + |
| 235 | +function Download-GreenChrome { |
| 236 | + if($arch -eq 'x64'){ |
| 237 | + Download-File $GCJSON.link.x64.url $gcdllpath |
| 238 | + $hash = (Get-FileHash $gcdllpath -Algorithm SHA1).Hash |
| 239 | + if($hash -ne $GCJSON.link.x64.sha1){ |
| 240 | + Write-Host "SHA1 不相符!" -ForegroundColor Red |
| 241 | + return; |
| 242 | + } |
| 243 | + } |
| 244 | + else{ |
| 245 | + Download-File $GCJSON.link.x86.url $gcdllpath |
| 246 | + $hash = (Get-FileHash $gcdllpath -Algorithm SHA1).Hash |
| 247 | + if($hash -ne $GCJSON.link.x86.sha1){ |
| 248 | + Write-Host "SHA1 不相符!" -ForegroundColor Red |
| 249 | + return; |
| 250 | + } |
| 251 | + } |
| 252 | + $gcinipath = Join-Path $installLocation 'GreenChrome.ini' |
| 253 | + if(-Not(Test-Path $gcinipath)){ |
| 254 | + Download-File 'https://static.pzhacm.org/shuax/GreenChromeTW.txt' $gcinipath |
| 255 | + } |
| 256 | + $updaterpath = Join-Path $installLocation 'Update.cmd' |
| 257 | + if(-Not(Test-Path $updaterpath)){ |
| 258 | + '@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString(''https://raw.githubusercontent.com/TkYu/PowerShellScripts/master/ChromeDownload/ChromeGCTW.ps1''))"', "@pause" -join "`r`n" | Out-File -Encoding "Default" $updaterpath |
| 259 | + } |
| 260 | + Write-Host 'GreenChrome(by Shuax) 下載已完成' -ForegroundColor Green |
| 261 | +} |
| 262 | + |
| 263 | +Write-Host "" |
| 264 | +try{ |
| 265 | + $JSON = Download-String $ggApi | ConvertFrom-Json |
| 266 | +}catch{ |
| 267 | + Write-Host '取得Chrome版本號碼失敗!' -ForegroundColor Red |
| 268 | + return |
| 269 | +} |
| 270 | +if(Check-InstallLocation) { |
| 271 | + Download-Chrome |
| 272 | +} |
| 273 | +else{ |
| 274 | + Write-Host 'Chrome 下載已略過(本機已是最新版)' -ForegroundColor Yellow |
| 275 | +} |
| 276 | + |
| 277 | +Write-Host "" |
| 278 | +try{ |
| 279 | + $GCJSON = Download-String $gcApi | ConvertFrom-Json |
| 280 | + if([string]::IsNullOrEmpty($GCJSON.description)){ |
| 281 | + Write-Host '取得GreenChrome版本號碼失敗!' -ForegroundColor Red |
| 282 | + return; |
| 283 | + } |
| 284 | + $gcdll = $GCJSON.link.x64.url.Substring($GCJSON.link.x64.url.LastIndexOf("/") + 1) |
| 285 | + $gcdllpath = Join-Path $installLocation $gcdll |
| 286 | +}catch{ |
| 287 | + Write-Host '取得GreenChrome版本號碼失敗!' -ForegroundColor Red |
| 288 | + return |
| 289 | +} |
| 290 | +if(Check-GCInstallLocation) { |
| 291 | + Download-GreenChrome |
| 292 | +} |
| 293 | +else{ |
| 294 | + Write-Host 'GreenChrome 下載已略過(本機已是最新版)' -ForegroundColor Yellow |
| 295 | +} |
| 296 | +# SIG # Begin signature block |
| 297 | +# MIIFlwYJKoZIhvcNAQcCoIIFiDCCBYQCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB |
| 298 | +# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR |
| 299 | +# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQU3YF6Rt9oQ34Ic1LDPv8gD9VB |
| 300 | +# 4cCgggMtMIIDKTCCAhWgAwIBAgIQE3U7au1O4rZEMExUKPt7LTAJBgUrDgMCHQUA |
| 301 | +# MB8xHTAbBgNVBAMTFFRLUG93ZXJTaGVsbFRlc3RDZXJ0MB4XDTE3MTEwOTA3MTg0 |
| 302 | +# MVoXDTM5MTIzMTIzNTk1OVowHzEdMBsGA1UEAxMUVEtQb3dlclNoZWxsVGVzdENl |
| 303 | +# cnQwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCZwoClq3b+amIlFj53 |
| 304 | +# bpY/0DybAvJHI/mN9EJKGmxeW5DLo7AFon4bimtcC9uRFXzLgKADozHTQt2/2UJj |
| 305 | +# kga7Hbdx5cSZZvXD3rRNhYs2gpUh6YEiuPzJVo1Da9HYFPHdBhX/doT3L4VKGiJd |
| 306 | +# MUBqBzcfEOTCochd74dFbzwdQj+9132XlhSOpT6iEgAz7MoKXxcaYWDdChq4wGwb |
| 307 | +# BGAA/cAdimH2jFIXx5qSs0/SNmkPzkS0rfkwA4c53paVuwjq3Mwj1emifMw1hV5x |
| 308 | +# 0R50uyBDZwF6vWxnMYX9hdG8dNgQptecCP2EhfP9VUrwRM4tN0jH+FoWXYh+q68S |
| 309 | +# PY4RAgMBAAGjaTBnMBMGA1UdJQQMMAoGCCsGAQUFBwMDMFAGA1UdAQRJMEeAEG9T |
| 310 | +# Gh8zK8+owaVTRGAMetOhITAfMR0wGwYDVQQDExRUS1Bvd2VyU2hlbGxUZXN0Q2Vy |
| 311 | +# dIIQE3U7au1O4rZEMExUKPt7LTAJBgUrDgMCHQUAA4IBAQAo0tjSpnncS34kqTut |
| 312 | +# NdZxkyerzpwzbRJsrYYAI0WGErcPUIvKG8un4n50Dtm8b8KwWQUK9FQkNhSv0ULR |
| 313 | +# QCT+Qrxm05IVNBhlbQfbjoYe9wGlXZdlxuUX++a/UemVG0WVOPpDc1PeI++8OnnY |
| 314 | +# hNx1O5hRb7D+zT2eTAXtKYf6FKbj1BLaz9v6hVGpjOX3Ypi79Kx/zXCJC6B5laOK |
| 315 | +# k4msoa2FHibX0L/UUKvHSMcbzTx1XfEbr2RXS+UE2b8nBzx4hE0OkU6wAQp/C/Kq |
| 316 | +# L8P70+b3HpXznOhtCS1lGOJ9H+22TiIVIiGag6Nq61wHpcFaYhw6yyE/En9nB7Ur |
| 317 | +# GhVCMYIB1DCCAdACAQEwMzAfMR0wGwYDVQQDExRUS1Bvd2VyU2hlbGxUZXN0Q2Vy |
| 318 | +# dAIQE3U7au1O4rZEMExUKPt7LTAJBgUrDgMCGgUAoHgwGAYKKwYBBAGCNwIBDDEK |
| 319 | +# MAigAoAAoQKAADAZBgkqhkiG9w0BCQMxDAYKKwYBBAGCNwIBBDAcBgorBgEEAYI3 |
| 320 | +# AgELMQ4wDAYKKwYBBAGCNwIBFTAjBgkqhkiG9w0BCQQxFgQUk1hYmwfTrk02EmmW |
| 321 | +# nyjN98aixGMwDQYJKoZIhvcNAQEBBQAEggEAKSDrjOIApw/Hv1UoIlUbzOg9wkDE |
| 322 | +# +OzXe74GN6Lt4DM1fPtNmXgJG6BruKJdPXTgnlDoxxNzBgmtDbmV80bmqXyrOqlZ |
| 323 | +# Mnno+czEPH8ciRGZVGYzXkfi07fA6CQnPEi1fyFQFUAG8A5g8G/WITDsBwbzaPr/ |
| 324 | +# 1FcdDBs0VCx456Vf52iGSSUx2Scr0Jhb5/RhxmArO8FFP3EGlA1CF/o12JU0vkyG |
| 325 | +# Yx9P/nRczPg35P1eE8GRcXGwFKo5AU0Vwq/S541Wq3b8u2DZbzaa+8++SlL0ixsr |
| 326 | +# RmI4tS5x75xZuk7tRr16oGotSfufwLM61+YWn7RdAukfoOZn8e6D1M6F4Q== |
| 327 | +# SIG # End signature block |
0 commit comments