Enhance user interaction with new dialog features and improve default browser handling
This commit is contained in:
@@ -1,4 +1,105 @@
|
||||
# ── Feature metadata ──────────────────────────────────────────────────────────
|
||||
# ── Browser detection helper ──────────────────────────────────────────────────
|
||||
|
||||
function Get-InstalledBrowsers {
|
||||
$knownBrowsers = @(
|
||||
'ChromeHTML',
|
||||
'FirefoxURL',
|
||||
'OperaGXStable',
|
||||
'Opera GXStable',
|
||||
'SafariHTML',
|
||||
'MSEdgeHTM',
|
||||
'BraveHTML',
|
||||
'Vivaldi',
|
||||
'IEexplore'
|
||||
)
|
||||
|
||||
$installed = @()
|
||||
foreach ($progId in $knownBrowsers) {
|
||||
if (Test-Path "HKLM:\SOFTWARE\Classes\$progId" -ErrorAction SilentlyContinue) {
|
||||
$installed += $progId
|
||||
}
|
||||
}
|
||||
|
||||
return @($installed | Sort-Object)
|
||||
}
|
||||
|
||||
function Get-BrowserSearchText {
|
||||
param([string]$ProgId)
|
||||
|
||||
if (-not $ProgId) { return 'browser' }
|
||||
|
||||
switch -Regex ($ProgId) {
|
||||
'^Opera\s?GXStable$' { return 'opera gx' }
|
||||
'^ChromeHTML$' { return 'chrome' }
|
||||
'^FirefoxURL' { return 'firefox' }
|
||||
'^MSEdgeHTM$' { return 'edge' }
|
||||
'^BraveHTML$' { return 'brave' }
|
||||
'^Vivaldi$' { return 'vivaldi' }
|
||||
default { return $ProgId }
|
||||
}
|
||||
}
|
||||
|
||||
function Invoke-DefaultBrowserGuidedChange {
|
||||
param([Parameter(Mandatory)][string]$TargetProgId)
|
||||
|
||||
$regPath = 'HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice'
|
||||
|
||||
try {
|
||||
$searchText = Get-BrowserSearchText -ProgId $TargetProgId
|
||||
|
||||
Stop-Process -ErrorAction Ignore -Name SystemSettings
|
||||
Start-Process 'ms-settings:defaultapps'
|
||||
|
||||
$ps = Get-Process -ErrorAction Stop SystemSettings
|
||||
do {
|
||||
Start-Sleep -Milliseconds 100
|
||||
$ps.Refresh()
|
||||
} while ([int]$ps.MainWindowHandle -eq 0)
|
||||
|
||||
Start-Sleep -Milliseconds 200
|
||||
|
||||
$shell = New-Object -ComObject WScript.Shell
|
||||
|
||||
foreach ($i in 1..5) {
|
||||
$shell.SendKeys('{TAB}')
|
||||
Start-Sleep -Milliseconds 100
|
||||
}
|
||||
|
||||
$shell.SendKeys($searchText)
|
||||
Start-Sleep -Seconds 1
|
||||
|
||||
$shell.SendKeys('{TAB}')
|
||||
Start-Sleep -Milliseconds 100
|
||||
$shell.SendKeys('{ENTER}')
|
||||
Start-Sleep -Milliseconds 200
|
||||
$shell.SendKeys('{ENTER}')
|
||||
Start-Sleep -Milliseconds 200
|
||||
$shell.SendKeys('%{F4}')
|
||||
Start-Sleep -Milliseconds 300
|
||||
|
||||
$browser = (Get-ItemProperty -Path $regPath -Name 'ProgId' -ErrorAction Stop).ProgId
|
||||
if ($browser -eq $TargetProgId) {
|
||||
Write-Log -Level Info -Message "Default browser successfully changed to '$browser'." -Feature 'DefaultBrowser'
|
||||
return $true
|
||||
}
|
||||
|
||||
Write-Log -Level Warn -Message "Browser change attempted, but current ProgId is '$browser' (expected '$TargetProgId')." -Feature 'DefaultBrowser'
|
||||
return $false
|
||||
}
|
||||
catch {
|
||||
Write-Log -Level Error -Message "Failed during guided default-browser change: $_" -Feature 'DefaultBrowser'
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
# ── Feature metadata ──────────────────────────────────────────────────────────
|
||||
|
||||
$installedBrowsers = Get-InstalledBrowsers
|
||||
$browserListText = if ($installedBrowsers.Count -gt 0) {
|
||||
"Detected on this system: " + ($installedBrowsers -join ', ')
|
||||
} else {
|
||||
"No known browsers detected. Examples: ChromeHTML, FirefoxURL, OperaGXStable, SafariHTML"
|
||||
}
|
||||
|
||||
$FeatureMeta = @{
|
||||
Name = 'DefaultBrowser'
|
||||
@@ -9,7 +110,7 @@ $FeatureMeta = @{
|
||||
Label = 'Target ProgId'
|
||||
Type = 'string'
|
||||
Default = 'OperaGXStable'
|
||||
Description = 'ProgId of the desired default browser (e.g. OperaGXStable, ChromeHTML, FirefoxURL-308046B0AF4A39CB)'
|
||||
Description = $browserListText
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -23,16 +124,10 @@ function Invoke-Feature {
|
||||
)
|
||||
|
||||
if (-not $State) { $State = @{} }
|
||||
if (-not $State.ContainsKey('lastShownDate')) { $State['lastShownDate'] = $null }
|
||||
if (-not $State.ContainsKey('ignoreUntilDate')) { $State['ignoreUntilDate'] = $null }
|
||||
|
||||
$today = (Get-Date).ToString('yyyy-MM-dd')
|
||||
|
||||
# Only notify once per day
|
||||
if ($State['lastShownDate'] -eq $today) {
|
||||
Write-Log -Level Info -Message 'Already checked today, skipping.' -Feature 'DefaultBrowser'
|
||||
return $State
|
||||
}
|
||||
|
||||
$regPath = 'HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice'
|
||||
|
||||
try {
|
||||
@@ -49,7 +144,14 @@ function Invoke-Feature {
|
||||
Write-Log -Level Info `
|
||||
-Message "Default browser OK: '$currentProgId'." `
|
||||
-Feature 'DefaultBrowser'
|
||||
$State['lastShownDate'] = $today
|
||||
$State['ignoreUntilDate'] = $null
|
||||
return $State
|
||||
}
|
||||
|
||||
if ($State['ignoreUntilDate'] -eq $today) {
|
||||
Write-Log -Level Info `
|
||||
-Message "Mismatch ignored for today (current '$currentProgId', expected '$($Config['targetProgId'])')." `
|
||||
-Feature 'DefaultBrowser'
|
||||
return $State
|
||||
}
|
||||
|
||||
@@ -57,17 +159,29 @@ function Invoke-Feature {
|
||||
-Message "Default browser mismatch — found '$currentProgId', expected '$($Config['targetProgId'])'." `
|
||||
-Feature 'DefaultBrowser'
|
||||
|
||||
# Note: Windows 11 blocks programmatic default-browser changes via registry hash protection.
|
||||
# We guide the user to the Settings page instead.
|
||||
Show-ToastNotification `
|
||||
-Title 'Default Browser' `
|
||||
-Body ("Default browser is '$currentProgId'. Click below to set it to $($Config['targetProgId']).") `
|
||||
-Body ("Default browser is '$currentProgId'. Do you want to change it now?") `
|
||||
-Buttons @(
|
||||
@{ Label = 'Open Default Apps Settings'; Action = 'ms-settings:defaultapps' },
|
||||
@{ Label = 'Dismiss'; Action = 'dismiss' }
|
||||
@{ Label = 'Yes'; Action = 'ms-settings:defaultapps' },
|
||||
@{ Label = 'No'; Action = 'dismiss' }
|
||||
)
|
||||
|
||||
$State['lastShownDate'] = $today
|
||||
$decision = Show-ConfirmationDialog `
|
||||
-Title 'Default Browser' `
|
||||
-Message ("Default browser is '$currentProgId', expected '$($Config['targetProgId'])'.`n`nDo you want to change it now?") `
|
||||
-Feature 'DefaultBrowser' `
|
||||
-Default 'No'
|
||||
|
||||
if ($decision -eq 'Yes') {
|
||||
[void](Invoke-DefaultBrowserGuidedChange -TargetProgId $Config['targetProgId'])
|
||||
$State['ignoreUntilDate'] = $null
|
||||
}
|
||||
else {
|
||||
$State['ignoreUntilDate'] = $today
|
||||
Write-Log -Level Info -Message 'User selected No; mismatch notifications suppressed until tomorrow.' -Feature 'DefaultBrowser'
|
||||
}
|
||||
|
||||
return $State
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user