|
1 |
| -# Read config file |
2 |
| -$jsonContent = Get-Content -Raw -Path "config.json" | ConvertFrom-Json |
3 |
| -Set-Variable addonsList -Option Constant -Value $jsonContent.addons_list - |
4 |
| -$addonsList = $jsonContent.addons_list -split ',' |
| 1 | +# Read and parse configuration file |
| 2 | +try { |
| 3 | + $jsonContent = Get-Content -Raw -Path "config.json" | ConvertFrom-Json |
| 4 | +} catch { |
| 5 | + Write-Host "Error reading config.json. Please ensure the file exists and is valid JSON." -ForegroundColor Red |
| 6 | + exit 1 |
| 7 | +} |
| 8 | + |
| 9 | +# Initialize constants from config |
| 10 | +$ADDONS_LIST = $jsonContent.addons_list -split ',' | ForEach-Object { $_.Trim() } |
| 11 | +$FIREFOX_PROFILE_PATH = if ($jsonContent.firefox_profile_path) { |
| 12 | + $jsonContent.firefox_profile_path |
| 13 | +} else { |
| 14 | + "$env:APPDATA\Mozilla\Firefox" |
| 15 | +} |
5 | 16 |
|
6 | 17 | function Create-Profile {
|
| 18 | + <# |
| 19 | + .SYNOPSIS |
| 20 | + Creates a new Firefox profile and returns the profile directory path. |
| 21 | + .OUTPUTS |
| 22 | + System.String. The path to the newly created profile directory. |
| 23 | + #> |
7 | 24 | Write-Host "Creating Profile..." -ForegroundColor Green
|
8 |
| - |
9 |
| - Set-Variable firefoxProfilePath -Option Constant -Value "$env:APPDATA\Mozilla\Firefox" |
10 |
| - Set-Variable profilesIniPath -Option Constant -Value "$firefoxProfilePath\profiles.ini" |
| 25 | + $profilesIniPath = "$FIREFOX_PROFILE_PATH\profiles.ini" |
11 | 26 |
|
12 | 27 | Write-Host "Enter name of the firefox profile: " -ForegroundColor Green -NoNewline
|
13 | 28 | $name = Read-Host
|
14 |
| - Start-Process "firefox" -ArgumentList "-CreateProfile $name" |
15 |
| - Start-Sleep -Seconds 1 # Wait for Firefox to update profile.ini |
| 29 | + Start-Process "firefox" -ArgumentList "-CreateProfile $name" -Wait |
16 | 30 |
|
17 | 31 | # Find and cd into the new profile
|
18 |
| - $folder = (Get-Content $profilesIniPath | Select-String -Pattern "Path=.*$name$" | ForEach-Object { $_ -replace 'Path=', '' }) |
19 |
| - $profileDir = Join-Path -Path $firefoxProfilePath "$folder" |
20 |
| - Set-Location -Path $profileDir |
| 32 | + $profileContent = Get-Content $profilesIniPath -ErrorAction Stop |
| 33 | + $folder = $profileContent | |
| 34 | + Select-String -Pattern "Path=.*$name$" | |
| 35 | + ForEach-Object { $_ -replace 'Path=', '' } | |
| 36 | + Select-Object -First 1 # Added to handle multiple matches |
| 37 | + |
| 38 | + if (-not $folder) { |
| 39 | + Write-Host "Error: Could not find newly created profile." -ForegroundColor Red |
| 40 | + exit 1 |
| 41 | + } |
21 | 42 |
|
| 43 | + $profileDir = Join-Path -Path $FIREFOX_PROFILE_PATH $folder |
| 44 | + Set-Location -Path $profileDir -ErrorAction Stop |
22 | 45 | Write-Host "Profile Creation Finished" -ForegroundColor Green
|
23 | 46 | return $profileDir
|
24 | 47 | }
|
25 | 48 |
|
26 | 49 | function Init-GitRepo {
|
| 50 | + <# |
| 51 | + .SYNOPSIS |
| 52 | + Initializes a Git repository with Firefox user preferences and configurations. |
| 53 | + #> |
27 | 54 | Write-Host "Initializing Git Repository..." -ForegroundColor Green
|
28 | 55 |
|
29 |
| - # Initialize the Git repository and fetch updates |
30 |
| - git init |
31 |
| - git remote add origin https://github.com/ghoulboii/foxdots |
32 |
| - git fetch |
33 |
| - git checkout --force --track origin/master |
34 |
| - git submodule update --init --recursive --remote |
35 |
| - |
36 |
| - robocopy userjs . user.js prefsCleaner.bat updater.bat |
37 |
| - |
38 |
| - .\updater.bat -unattended |
39 |
| - |
40 |
| - Write-Host "Git Repository Initialized" -ForegroundColor Green |
| 56 | + try { |
| 57 | + # Check if git is installed |
| 58 | + if (-not (Get-Command git -ErrorAction SilentlyContinue)) { |
| 59 | + throw "Git is not installed or not in PATH" |
| 60 | + } |
| 61 | + |
| 62 | + # Initialize repository and configure |
| 63 | + git init |
| 64 | + git remote add origin https://github.com/ghoulboii/foxdots |
| 65 | + |
| 66 | + # Fetch and checkout master branch |
| 67 | + git fetch --quiet |
| 68 | + git checkout --force --track origin/master |
| 69 | + |
| 70 | + # Update submodules |
| 71 | + git submodule update --init --recursive --remote |
| 72 | + |
| 73 | + # Copy necessary files |
| 74 | + $filesToCopy = @("user.js", "prefsCleaner.bat", "updater.bat") |
| 75 | + foreach ($file in $filesToCopy) { |
| 76 | + if (Test-Path "userjs\$file") { |
| 77 | + Copy-Item "userjs\$file" -Destination "." -Force |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + # Run updater if it exists |
| 82 | + if (Test-Path ".\updater.bat") { |
| 83 | + .\updater.bat -unattended |
| 84 | + } |
| 85 | + |
| 86 | + Write-Host "Git Repository Initialized" -ForegroundColor Green |
| 87 | + } catch { |
| 88 | + Write-Host "Error during Git initialization: $_" -ForegroundColor Red |
| 89 | + exit 1 |
| 90 | + } |
41 | 91 | }
|
42 | 92 |
|
43 | 93 | function Download-Addons {
|
| 94 | + <# |
| 95 | + .SYNOPSIS |
| 96 | + Downloads and installs Firefox addons from Mozilla's addon store. |
| 97 | + .PARAMETER profileDir |
| 98 | + The Firefox profile directory where addons should be installed. |
| 99 | + #> |
44 | 100 | param (
|
| 101 | + [Parameter(Mandatory=$true)] |
45 | 102 | [string]$profileDir
|
46 | 103 | )
|
47 | 104 | Write-Host "Downloading Addons..." -ForegroundColor Green
|
48 | 105 |
|
49 |
| - Set-Variable mozillaAddonURL -Option Constant -Value "https://addons.mozilla.org" |
50 |
| - $addonTmpDir = New-Item -ItemType Directory -Path "$env:TEMP\addon" -Force |
51 |
| - $extensionsDir = New-Item -ItemType Directory -Path "$profileDir\extensions" -ErrorAction SilentlyContinue |
52 |
| - |
53 |
| - foreach ($addon in $addonsList) { |
54 |
| - $addon = $addon.Trim() |
55 |
| - |
56 |
| - Write-Host "Installing addon: $addon" -ForegroundColor Green |
57 |
| - |
58 |
| - # Get addon download URL |
59 |
| - $addonPageContent = Invoke-WebRequest -Uri "$mozillaAddonURL/en-US/firefox/addon/$addon/" -UseBasicParsing |
60 |
| - $addonDownloadURL = $addonPageContent.Content | Select-String -Pattern "$mozillaAddonURL/firefox/downloads/file/[^`"]*" | Select-Object -ExpandProperty Matches | ForEach-Object { $_.Value } |
61 |
| - |
62 |
| - # Download and extract the add-on |
63 |
| - $addonFileName = $addonDownloadURL -split '/' | Select-Object -Last 1 |
64 |
| - $addonDownloadPath = Join-Path -Path $addonTmpDir.FullName -ChildPath $addonFileName |
65 |
| - |
66 |
| - Invoke-WebRequest -Uri $addonDownloadURL -OutFile $addonDownloadPath |
67 |
| - $addonExtractDir = Join-Path -Path $addonTmpDir.FullName -ChildPath "${addonFileName}folder" |
68 |
| - Expand-Archive -Path $addonDownloadPath -DestinationPath $addonExtractDir -Force |
69 |
| - |
70 |
| - # Extract addon ID and move to profile extensions folder |
71 |
| - $addonId = (Select-String -Path "$addonExtractDir/manifest.json" -Pattern '"id"' -Raw | ForEach-Object { $_ -replace '\s*"id":\s*"([^"]*)"', '$1' }).Trim(',') |
72 |
| - Move-Item -Path "$addonTmpDir\$addonFileName" -Destination "$extensionsDir\$addonId.xpi" -Force |
| 106 | + $MOZILLA_ADDON_URL = "https://addons.mozilla.org" |
| 107 | + $addonTmpDir = New-Item -ItemType Directory -Path "$env:TEMP\firefox_addons" -Force |
| 108 | + $extensionsDir = New-Item -ItemType Directory -Path "$profileDir\extensions" |
| 109 | + |
| 110 | + foreach ($addon in $ADDONS_LIST) { |
| 111 | + try { |
| 112 | + Write-Host "Installing addon: $addon" -ForegroundColor Green |
| 113 | + |
| 114 | + # Get addon download URL |
| 115 | + $addonPageUrl = "$MOZILLA_ADDON_URL/en-US/firefox/addon/$addon/" |
| 116 | + $addonPageContent = Invoke-WebRequest -Uri $addonPageUrl -UseBasicParsing |
| 117 | + $addonDownloadURL = $addonPageContent.Links | |
| 118 | + Where-Object { $_.href -match "/firefox/downloads/file/" } | |
| 119 | + Select-Object -First 1 -ExpandProperty href |
| 120 | + |
| 121 | + if (-not $addonDownloadURL) { |
| 122 | + throw "Could not find download URL for addon: $addon" |
| 123 | + } |
| 124 | + |
| 125 | + # Download and extract addon |
| 126 | + $addonFileName = Split-Path $addonDownloadURL -Leaf |
| 127 | + $addonDownloadPath = Join-Path -Path $addonTmpDir.FullName -ChildPath $addonFileName |
| 128 | + |
| 129 | + Invoke-WebRequest -Uri $addonDownloadURL -OutFile $addonDownloadPath |
| 130 | + $addonExtractDir = Join-Path -Path $addonTmpDir.FullName -ChildPath "${addon}_extracted" |
| 131 | + Expand-Archive -Path $addonDownloadPath -DestinationPath $addonExtractDir -Force |
| 132 | + |
| 133 | + # Get addon ID and install |
| 134 | + $manifestPath = Join-Path $addonExtractDir "manifest.json" |
| 135 | + if (-not (Test-Path $manifestPath)) { |
| 136 | + throw "manifest.json not found in addon package" |
| 137 | + } |
| 138 | + |
| 139 | + $manifestContent = Get-Content $manifestPath -Raw | ConvertFrom-Json |
| 140 | + |
| 141 | + # Handle both Manifest V2 and V3 addon ID locations |
| 142 | + $addonId = if ($manifestContent.applications -and $manifestContent.applications.gecko.id) { |
| 143 | + # Manifest V2 |
| 144 | + $manifestContent.applications.gecko.id |
| 145 | + } elseif ($manifestContent.browser_specific_settings -and $manifestContent.browser_specific_settings.gecko.id) { |
| 146 | + # Manifest V3 |
| 147 | + $manifestContent.browser_specific_settings.gecko.id |
| 148 | + } else { |
| 149 | + throw "Could not find addon ID in manifest (neither V2 nor V3 format)" |
| 150 | + } |
| 151 | + |
| 152 | + if (-not $addonId) { |
| 153 | + throw "Could not find addon ID in manifest" |
| 154 | + } |
| 155 | + |
| 156 | + Move-Item -Path $addonDownloadPath -Destination "$extensionsDir\$addonId.xpi" -Force |
| 157 | + |
| 158 | + } catch { |
| 159 | + Write-Host "Error installing addon $addon`: $_" -ForegroundColor Red |
| 160 | + continue |
| 161 | + } |
73 | 162 | }
|
74 | 163 |
|
75 |
| - Write-Host "Addons Installed" -ForegroundColor Green |
| 164 | + # Cleanup |
| 165 | + Remove-Item -Path $addonTmpDir -Recurse -Force -ErrorAction SilentlyContinue |
| 166 | + Write-Host "Addons Installation Completed" -ForegroundColor Green |
76 | 167 | }
|
77 | 168 |
|
78 |
| -$profileDir = Create-Profile |
79 |
| -Init-GitRepo |
80 |
| -Download-Addons -profileDir $profileDir |
| 169 | +# Main |
| 170 | +try { |
| 171 | + $profileDir = Create-Profile |
| 172 | + Init-GitRepo |
| 173 | + Download-Addons -profileDir $profileDir |
| 174 | + Write-Host "Firefox profile setup completed successfully!" -ForegroundColor Green |
| 175 | +} catch { |
| 176 | + Write-Host "An error occurred during profile setup: $_" -ForegroundColor Red |
| 177 | + exit 1 |
| 178 | +} |
0 commit comments