Skip to content

Commit

Permalink
feat: add firefox_profile_path config, document changes and add error…
Browse files Browse the repository at this point in the history
… handling
  • Loading branch information
GhoulBoii committed Jan 15, 2025
1 parent 82d3a44 commit 3c02ee0
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 53 deletions.
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"ublock-origin",
"violentmonkey",
"windscribe"
]
],
"firefox_profile_path": ""
}
202 changes: 150 additions & 52 deletions setup.ps1
Original file line number Diff line number Diff line change
@@ -1,80 +1,178 @@
# Read config file
$jsonContent = Get-Content -Raw -Path "config.json" | ConvertFrom-Json
Set-Variable addonsList -Option Constant -Value $jsonContent.addons_list -
$addonsList = $jsonContent.addons_list -split ','
# Read and parse configuration file
try {
$jsonContent = Get-Content -Raw -Path "config.json" | ConvertFrom-Json
} catch {
Write-Host "Error reading config.json. Please ensure the file exists and is valid JSON." -ForegroundColor Red
exit 1
}

# Initialize constants from config
$ADDONS_LIST = $jsonContent.addons_list -split ',' | ForEach-Object { $_.Trim() }
$FIREFOX_PROFILE_PATH = if ($jsonContent.firefox_profile_path) {
$jsonContent.firefox_profile_path
} else {
"$env:APPDATA\Mozilla\Firefox"
}

function Create-Profile {
<#
.SYNOPSIS
Creates a new Firefox profile and returns the profile directory path.
.OUTPUTS
System.String. The path to the newly created profile directory.
#>
Write-Host "Creating Profile..." -ForegroundColor Green

Set-Variable firefoxProfilePath -Option Constant -Value "$env:APPDATA\Mozilla\Firefox"
Set-Variable profilesIniPath -Option Constant -Value "$firefoxProfilePath\profiles.ini"
$profilesIniPath = "$FIREFOX_PROFILE_PATH\profiles.ini"

Write-Host "Enter name of the firefox profile: " -ForegroundColor Green -NoNewline
$name = Read-Host
Start-Process "firefox" -ArgumentList "-CreateProfile $name"
Start-Sleep -Seconds 1 # Wait for Firefox to update profile.ini
Start-Process "firefox" -ArgumentList "-CreateProfile $name" -Wait

# Find and cd into the new profile
$folder = (Get-Content $profilesIniPath | Select-String -Pattern "Path=.*$name$" | ForEach-Object { $_ -replace 'Path=', '' })
$profileDir = Join-Path -Path $firefoxProfilePath "$folder"
Set-Location -Path $profileDir
$profileContent = Get-Content $profilesIniPath -ErrorAction Stop
$folder = $profileContent |
Select-String -Pattern "Path=.*$name$" |
ForEach-Object { $_ -replace 'Path=', '' } |
Select-Object -First 1 # Added to handle multiple matches

if (-not $folder) {
Write-Host "Error: Could not find newly created profile." -ForegroundColor Red
exit 1
}

$profileDir = Join-Path -Path $FIREFOX_PROFILE_PATH $folder
Set-Location -Path $profileDir -ErrorAction Stop
Write-Host "Profile Creation Finished" -ForegroundColor Green
return $profileDir
}

function Init-GitRepo {
<#
.SYNOPSIS
Initializes a Git repository with Firefox user preferences and configurations.
#>
Write-Host "Initializing Git Repository..." -ForegroundColor Green

# Initialize the Git repository and fetch updates
git init
git remote add origin https://github.com/ghoulboii/foxdots
git fetch
git checkout --force --track origin/master
git submodule update --init --recursive --remote

robocopy userjs . user.js prefsCleaner.bat updater.bat

.\updater.bat -unattended

Write-Host "Git Repository Initialized" -ForegroundColor Green
try {
# Check if git is installed
if (-not (Get-Command git -ErrorAction SilentlyContinue)) {
throw "Git is not installed or not in PATH"
}

# Initialize repository and configure
git init
git remote add origin https://github.com/ghoulboii/foxdots

# Fetch and checkout master branch
git fetch --quiet
git checkout --force --track origin/master

# Update submodules
git submodule update --init --recursive --remote

# Copy necessary files
$filesToCopy = @("user.js", "prefsCleaner.bat", "updater.bat")
foreach ($file in $filesToCopy) {
if (Test-Path "userjs\$file") {
Copy-Item "userjs\$file" -Destination "." -Force
}
}

# Run updater if it exists
if (Test-Path ".\updater.bat") {
.\updater.bat -unattended
}

Write-Host "Git Repository Initialized" -ForegroundColor Green
} catch {
Write-Host "Error during Git initialization: $_" -ForegroundColor Red
exit 1
}
}

function Download-Addons {
<#
.SYNOPSIS
Downloads and installs Firefox addons from Mozilla's addon store.
.PARAMETER profileDir
The Firefox profile directory where addons should be installed.
#>
param (
[Parameter(Mandatory=$true)]
[string]$profileDir
)
Write-Host "Downloading Addons..." -ForegroundColor Green

Set-Variable mozillaAddonURL -Option Constant -Value "https://addons.mozilla.org"
$addonTmpDir = New-Item -ItemType Directory -Path "$env:TEMP\addon" -Force
$extensionsDir = New-Item -ItemType Directory -Path "$profileDir\extensions" -ErrorAction SilentlyContinue

foreach ($addon in $addonsList) {
$addon = $addon.Trim()

Write-Host "Installing addon: $addon" -ForegroundColor Green

# Get addon download URL
$addonPageContent = Invoke-WebRequest -Uri "$mozillaAddonURL/en-US/firefox/addon/$addon/" -UseBasicParsing
$addonDownloadURL = $addonPageContent.Content | Select-String -Pattern "$mozillaAddonURL/firefox/downloads/file/[^`"]*" | Select-Object -ExpandProperty Matches | ForEach-Object { $_.Value }

# Download and extract the add-on
$addonFileName = $addonDownloadURL -split '/' | Select-Object -Last 1
$addonDownloadPath = Join-Path -Path $addonTmpDir.FullName -ChildPath $addonFileName

Invoke-WebRequest -Uri $addonDownloadURL -OutFile $addonDownloadPath
$addonExtractDir = Join-Path -Path $addonTmpDir.FullName -ChildPath "${addonFileName}folder"
Expand-Archive -Path $addonDownloadPath -DestinationPath $addonExtractDir -Force

# Extract addon ID and move to profile extensions folder
$addonId = (Select-String -Path "$addonExtractDir/manifest.json" -Pattern '"id"' -Raw | ForEach-Object { $_ -replace '\s*"id":\s*"([^"]*)"', '$1' }).Trim(',')
Move-Item -Path "$addonTmpDir\$addonFileName" -Destination "$extensionsDir\$addonId.xpi" -Force
$MOZILLA_ADDON_URL = "https://addons.mozilla.org"
$addonTmpDir = New-Item -ItemType Directory -Path "$env:TEMP\firefox_addons" -Force
$extensionsDir = New-Item -ItemType Directory -Path "$profileDir\extensions"

foreach ($addon in $ADDONS_LIST) {
try {
Write-Host "Installing addon: $addon" -ForegroundColor Green

# Get addon download URL
$addonPageUrl = "$MOZILLA_ADDON_URL/en-US/firefox/addon/$addon/"
$addonPageContent = Invoke-WebRequest -Uri $addonPageUrl -UseBasicParsing
$addonDownloadURL = $addonPageContent.Links |
Where-Object { $_.href -match "/firefox/downloads/file/" } |
Select-Object -First 1 -ExpandProperty href

if (-not $addonDownloadURL) {
throw "Could not find download URL for addon: $addon"
}

# Download and extract addon
$addonFileName = Split-Path $addonDownloadURL -Leaf
$addonDownloadPath = Join-Path -Path $addonTmpDir.FullName -ChildPath $addonFileName

Invoke-WebRequest -Uri $addonDownloadURL -OutFile $addonDownloadPath
$addonExtractDir = Join-Path -Path $addonTmpDir.FullName -ChildPath "${addon}_extracted"
Expand-Archive -Path $addonDownloadPath -DestinationPath $addonExtractDir -Force

# Get addon ID and install
$manifestPath = Join-Path $addonExtractDir "manifest.json"
if (-not (Test-Path $manifestPath)) {
throw "manifest.json not found in addon package"
}

$manifestContent = Get-Content $manifestPath -Raw | ConvertFrom-Json

# Handle both Manifest V2 and V3 addon ID locations
$addonId = if ($manifestContent.applications -and $manifestContent.applications.gecko.id) {
# Manifest V2
$manifestContent.applications.gecko.id
} elseif ($manifestContent.browser_specific_settings -and $manifestContent.browser_specific_settings.gecko.id) {
# Manifest V3
$manifestContent.browser_specific_settings.gecko.id
} else {
throw "Could not find addon ID in manifest (neither V2 nor V3 format)"
}

if (-not $addonId) {
throw "Could not find addon ID in manifest"
}

Move-Item -Path $addonDownloadPath -Destination "$extensionsDir\$addonId.xpi" -Force

} catch {
Write-Host "Error installing addon $addon`: $_" -ForegroundColor Red
continue
}
}

Write-Host "Addons Installed" -ForegroundColor Green
# Cleanup
Remove-Item -Path $addonTmpDir -Recurse -Force -ErrorAction SilentlyContinue
Write-Host "Addons Installation Completed" -ForegroundColor Green
}

$profileDir = Create-Profile
Init-GitRepo
Download-Addons -profileDir $profileDir
# Main
try {
$profileDir = Create-Profile
Init-GitRepo
Download-Addons -profileDir $profileDir
Write-Host "Firefox profile setup completed successfully!" -ForegroundColor Green
} catch {
Write-Host "An error occurred during profile setup: $_" -ForegroundColor Red
exit 1
}

0 comments on commit 3c02ee0

Please sign in to comment.