setup-chrome-permissions.ps1 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # PowerShell script to setup Chrome permissions for microphone access
  2. # This script creates a Chrome preferences file that auto-grants microphone permissions
  3. param(
  4. [string]$ChromeUserData = "$env:TEMP\chrome-digital-human",
  5. [string]$Origin = "http://localhost:3000"
  6. )
  7. # Create Chrome user data directory if it doesn't exist
  8. if (-not (Test-Path $ChromeUserData)) {
  9. New-Item -ItemType Directory -Path $ChromeUserData -Force | Out-Null
  10. }
  11. # Create Default directory for preferences
  12. $DefaultDir = Join-Path $ChromeUserData "Default"
  13. if (-not (Test-Path $DefaultDir)) {
  14. New-Item -ItemType Directory -Path $DefaultDir -Force | Out-Null
  15. }
  16. # Create Preferences file path
  17. $PreferencesPath = Join-Path $DefaultDir "Preferences"
  18. # Read existing preferences
  19. $existingJson = "{}"
  20. if (Test-Path $PreferencesPath) {
  21. try {
  22. $existingJson = Get-Content $PreferencesPath -Raw -ErrorAction SilentlyContinue
  23. if ([string]::IsNullOrWhiteSpace($existingJson)) {
  24. $existingJson = "{}"
  25. }
  26. } catch {
  27. $existingJson = "{}"
  28. }
  29. }
  30. # Parse existing JSON
  31. try {
  32. $prefs = $existingJson | ConvertFrom-Json
  33. } catch {
  34. $prefs = @{} | ConvertTo-Json | ConvertFrom-Json
  35. }
  36. # Function to ensure nested object exists
  37. function Ensure-Object {
  38. param($obj, $path)
  39. $parts = $path -split '\.'
  40. $current = $obj
  41. foreach ($part in $parts) {
  42. if (-not $current.$part) {
  43. $current | Add-Member -MemberType NoteProperty -Name $part -Value (@{} | ConvertTo-Json | ConvertFrom-Json) -Force
  44. }
  45. $current = $current.$part
  46. }
  47. return $current
  48. }
  49. # Ensure all required objects exist
  50. Ensure-Object $prefs "profile" | Out-Null
  51. Ensure-Object $prefs "profile.content_settings" | Out-Null
  52. Ensure-Object $prefs "profile.content_settings.exceptions" | Out-Null
  53. Ensure-Object $prefs "profile.content_settings.exceptions.media_stream_mic" | Out-Null
  54. Ensure-Object $prefs "profile.content_settings.exceptions.media_stream_camera" | Out-Null
  55. # Set microphone permission (1 = Allow)
  56. $micKey = "$Origin,*"
  57. $micPerm = @{
  58. setting = 1
  59. } | ConvertTo-Json | ConvertFrom-Json
  60. $prefs.profile.content_settings.exceptions.media_stream_mic | Add-Member -MemberType NoteProperty -Name $micKey -Value $micPerm -Force
  61. # Set camera permission (1 = Allow)
  62. $cameraKey = "$Origin,*"
  63. $cameraPerm = @{
  64. setting = 1
  65. } | ConvertTo-Json | ConvertFrom-Json
  66. $prefs.profile.content_settings.exceptions.media_stream_camera | Add-Member -MemberType NoteProperty -Name $cameraKey -Value $cameraPerm -Force
  67. # Save preferences
  68. try {
  69. $json = $prefs | ConvertTo-Json -Depth 20
  70. Set-Content -Path $PreferencesPath -Value $json -Encoding UTF8 -Force
  71. exit 0
  72. } catch {
  73. exit 1
  74. }