Skip to content

Commit 9bbe032

Browse files
committed
LatestStableChromeVersion
ability to update version using parameter while importing module
1 parent c140f1d commit 9bbe032

File tree

5 files changed

+112
-1
lines changed

5 files changed

+112
-1
lines changed

CRX/CRX.psm1

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
param(
2+
[parameter(Position=0,Mandatory=$false)][boolean]$UpdateChromeVersion
3+
)
4+
5+
$Global:LatestStableChromeVersion = [version]"132.0.6834.159"
6+
17
$Public = @( Get-ChildItem -Path $PSScriptRoot\Public -Recurse -Filter *.ps1 -ErrorAction SilentlyContinue )
28
$Private = @( Get-ChildItem -Path $PSScriptRoot\Private -Recurse -Filter *.ps1 -ErrorAction SilentlyContinue )
39

@@ -9,4 +15,12 @@ Foreach ($import in @($Private + $Public )) {
915
Write-Error -Message "Failed to import function $($import.fullname): $_"
1016
}
1117
}
18+
19+
if ($UpdateChromeVersion) {
20+
$version = Get-LatestStableChromeVersion
21+
if ($version) {
22+
$Global:LatestStableChromeVersion = $version
23+
}
24+
}
25+
1226
Export-ModuleMember -Function $Public.Basename

CRX/Private/Get-CRXUpdateUrl.ps1

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ function Get-CRXUpdateUrl {
44
[string]$Id
55
)
66

7-
"https://clients2.google.com/service/update2/crx?prodversion=131.0.6778.205&acceptformat=crx2,crx3&x=id%3D$Id%26installsource%3Dondemand%26uc"
7+
$prodVersion = $Global:LatestStableChromeVersion.ToString()
8+
"https://clients2.google.com/service/update2/crx?prodversion=$prodVersion&acceptformat=crx2,crx3&x=id%3D$Id%26installsource%3Dondemand%26uc"
89
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function Get-LatestStableChromeVersion {
2+
$uri = "https://chromiumdash.appspot.com/fetch_releases?channel=Stable&platform=Windows&num=1"
3+
try {
4+
$data = Invoke-RestMethod -Uri $uri
5+
$available = [version]$data.version
6+
} catch {
7+
Write-Warning "Failed to retrieve the latest stable Chrome version. Please check your internet connection."
8+
return $null
9+
}
10+
$available
11+
}

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ Install-Module -Name CRX
1717
Import-Module -Name CRX
1818
```
1919

20+
> [!NOTE]
21+
> **Latest stable chrome version is hardcoded in `$Global:LatestStableChromeVersion` by default**
22+
>
23+
> You can update it manually or use `-ArgumentList` during module import so the latest version will be automatically fetched from https://chromiumdash.appspot.com
24+
>
25+
> `Import-Module CRX -ArgumentList $true`
26+
2027
### Example Usage
2128
**Downloads the CRX file for a specified extension.**
2229
```powershell

tests/CRX.Tests.ps1

+78
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,68 @@ if (-not (Get-Module -Name Pester)) {
33
}
44
Import-Module .\CRX\CRX.psm1 -Force
55

6+
Describe 'CRX Module Import' {
7+
BeforeAll {
8+
# hardcoded version - shipped with the module
9+
$chromeVersion = [version]"132.0.6834.159"
10+
}
11+
12+
Context 'When UpdateChromeVersion parameter is used' {
13+
It 'Should get the latest stable Chrome version' {
14+
Import-Module .\CRX\CRX.psm1 -ArgumentList $true -Force
15+
16+
$Global:LatestStableChromeVersion | Should -BeOfType [version]
17+
$Global:LatestStableChromeVersion -gt $chromeVersion | Should -Be $true
18+
}
19+
20+
It 'Should not get the latest stable Chrome version' {
21+
Import-Module .\CRX\CRX.psm1 -ArgumentList $false -Force
22+
23+
$Global:LatestStableChromeVersion | Should -BeOfType [version]
24+
$Global:LatestStableChromeVersion.ToString() | Should -Be $chromeVersion
25+
}
26+
}
27+
28+
Context 'When UpdateChromeVersion parameter is not used' {
29+
It 'Should not get the latest stable Chrome version' {
30+
Import-Module .\CRX\CRX.psm1 -Force
31+
32+
$Global:LatestStableChromeVersion | Should -BeOfType [version]
33+
$Global:LatestStableChromeVersion.ToString() | Should -Be $chromeVersion
34+
}
35+
}
36+
}
37+
38+
Describe 'Get-LatestStableChromeVersion' {
39+
Context 'When API call is successful' {
40+
It 'Should return a valid version' {
41+
Mock -CommandName Invoke-RestMethod -ModuleName CRX -MockWith {
42+
return @{
43+
channel = 'Stable'
44+
milestone = 132
45+
platform = "Windows"
46+
version = "132.0.6834.160"
47+
}
48+
}
49+
50+
$result = Get-LatestStableChromeVersion
51+
$result | Should -BeOfType [version]
52+
$result.ToString() | Should -Be "132.0.6834.160"
53+
}
54+
}
55+
56+
Context 'When API call fails' {
57+
It 'Should return $null' {
58+
Mock -CommandName Invoke-RestMethod -ModuleName CRX -MockWith {
59+
throw "API call failed"
60+
}
61+
62+
$result = Get-LatestStableChromeVersion
63+
$result | Should -Be $null
64+
}
65+
}
66+
}
67+
668
Describe 'CRX.Tests' {
769
BeforeAll {
870
$testUrl = 'https://localhost.com/crx/blobs/ASuc5ohLVu-itAJfZqe6NgPkB0pCREbOH49PhxJq4pMdp7MWQx-ycGQt8dsD8WUSM_dTlB5sLwXljaUve7GTKh485NrRlNGdmT7O5aT9uS4R9jmIqNJBAMZSmuV9IZ0e0VV7jGd-rrI-YR5eoIra2Q/AOCLHCCCFDKJDDGPAAAJLDGLJHLLHGMD_4_0_0_0.crx'
@@ -105,3 +167,19 @@ Describe 'CRX.Tests' {
105167
}
106168
}
107169
}
170+
171+
InModuleScope CRX {
172+
Describe 'Get-CRXUpdateUrl' {
173+
BeforeAll {
174+
$Global:LatestStableChromeVersion = [version]"128.0.0.256"
175+
$testExtensionId = "aoclhcccfdkjddgpaaajldgljhllhgmd"
176+
177+
}
178+
179+
It 'Should return the correct update URL' {
180+
$expectedUrl = "https://clients2.google.com/service/update2/crx?prodversion=128.0.0.256&acceptformat=crx2,crx3&x=id%3D$testExtensionId%26installsource%3Dondemand%26uc"
181+
$result = Get-CRXUpdateUrl -Id $testExtensionId
182+
$result | Should -Be $expectedUrl
183+
}
184+
}
185+
}

0 commit comments

Comments
 (0)