Skip to content

Commit

Permalink
Update Add_Firefox_Extensions.ps1
Browse files Browse the repository at this point in the history
  • Loading branch information
farag2 authored Sep 19, 2024
1 parent 8f3d225 commit 6fc8584
Showing 1 changed file with 109 additions and 109 deletions.
218 changes: 109 additions & 109 deletions Add_Firefox_Extensions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,158 +20,158 @@ if (-not (Test-Path -Path "$DownloadsFolder\Extensions"))

<#
.SYNOPSIS
Add extensions to Firefox automatically
.PARAMETER ExtensionUri
Copy URL on an extesion page by right-clicking on the Download button
Install extensions to Firefox automatically
.EXAMPLE
$Parameters = @{
ExtensionUris = @("https://addons.mozilla.org/firefox/addon/ublock-origin")
"https://addons.mozilla.org/firefox/addon/ublock-origin" = "$env:APPDATA\Mozilla\Firefox\Profiles\ProfileName\extensions\[email protected]"
}
Add-FirefoxExtension @Parameters
.NOTES
If an extension doesn't have a valid manifets.json you have to find out the ID by yourself
Example: https://github.com/farag2/Mozilla-Firefox/discussions/1#discussioncomment-1218530
.NOTES
Leave "ProfileName" in extention installation path as it is
.NOTES
Enable extension manually
#>
function Add-FirefoxExtension
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[string[]]
$ExtensionUris
)

foreach ($Uri in $ExtensionUris)
if (-not (Test-Path -Path "$env:APPDATA\Mozilla\Firefox\installs.ini"))
{
# Downloading extension
$Parameters = @{
Uri = $Uri
UseBasicParsing = $false # Disabled
Verbose = $true
}
$Request = Invoke-Webrequest @Parameters
$URL = $Request.ParsedHtml.getElementsByClassName("InstallButtonWrapper-download-link") | ForEach-Object -Process {$_.href}
Write-Warning -Message "Please launch Firefox first"
exit
}

$Extension = Split-Path -Path $URL -Leaf
# Get Firefox profile name
$String = (Get-Content -Path "$env:APPDATA\Mozilla\Firefox\installs.ini" -Encoding Default | Select-String -Pattern "^\s*Default\s*=\s*.+" | ConvertFrom-StringData).Default
$ProfileName = Split-Path -Path $String -Leaf

$Parameters = @{
Uri = $URL
OutFile = "$DownloadsFolder\Extensions\$Extension"
UseBasicParsing = $true
Verbose = $true
}
Invoke-WebRequest @Parameters
# Parse $Parameters hashtable
$Parameters.GetEnumerator() | ForEach-Object -Process {
# Replace "ProfileName" with real profile name
if (-not (Test-Path -Path $_.Value.Replace("ProfileName", "$ProfileName")))
{
# Downloading extension
$Parameters = @{
Uri = $_.Name
UseBasicParsing = $false # Disabled
Verbose = $true
}
$Request = Invoke-Webrequest @Parameters
$URL = $Request.ParsedHtml.getElementsByClassName("InstallButtonWrapper-download-link") | ForEach-Object -Process {$_.href}

# Copy file and rename it into .zip
Get-Item -Path "$DownloadsFolder\Extensions\$Extension" -Force | Foreach-Object -Process {
Copy-Item -Path $_.FullName -Destination "$DownloadsFolder\Extensions\$($_.BaseName).zip" -Force
}
$Extension = Split-Path -Path $URL -Leaf

<#
.SYNOPSIS
Expand the specific file from ZIP archive. Folder structure will be created recursively
$Parameters = @{
Uri = $URL
OutFile = "$DownloadsFolder\Extensions\$Extension"
UseBasicParsing = $true
Verbose = $true
}
Invoke-WebRequest @Parameters

.Parameter Source
The source ZIP archive
# Copy file and rename it into .zip
Get-Item -Path "$DownloadsFolder\Extensions\$Extension" -Force | Foreach-Object -Process {
Copy-Item -Path $_.FullName -Destination "$DownloadsFolder\Extensions\$($_.BaseName).zip" -Force
}

.Parameter Destination
Where to expand file
<#
.SYNOPSIS
Expand the specific file from ZIP archive. Folder structure will be created recursively
.Parameter File
Assign the file to expand
.Parameter Source
The source ZIP archive
.Example
ExtractZIPFile -Source "D:\Folder\File.zip" -Destination "D:\Folder" -File "Folder1/Folder2/File.txt"
#>
function ExtractZIPFile
{
[CmdletBinding()]
param
(
[string]
$Source,
.Parameter Destination
Where to expand file
[string]
$Destination,
.Parameter File
Assign the file to expand
[string]
$File
)
.Example
ExtractZIPFile -Source "D:\Folder\File.zip" -Destination "D:\Folder" -File "Folder1/Folder2/File.txt"
#>
function ExtractZIPFile
{
[CmdletBinding()]
param
(
[string]
$Source,

Add-Type -Assembly System.IO.Compression.FileSystem
[string]
$Destination,

$ZIP = [IO.Compression.ZipFile]::OpenRead($Source)
$Entries = $ZIP.Entries | Where-Object -FilterScript {$_.FullName -eq $File}
[string]
$File
)

$Destination = "$Destination\$(Split-Path -Path $File -Parent)"
Add-Type -Assembly System.IO.Compression.FileSystem

if (-not (Test-Path -Path $Destination))
{
New-Item -Path $Destination -ItemType Directory -Force
}
$ZIP = [IO.Compression.ZipFile]::OpenRead($Source)
$Entries = $ZIP.Entries | Where-Object -FilterScript {$_.FullName -eq $File}

$Entries | ForEach-Object -Process {[IO.Compression.ZipFileExtensions]::ExtractToFile($_, "$($Destination)\$($_.Name)", $true)}
$Destination = "$Destination\$(Split-Path -Path $File -Parent)"

$ZIP.Dispose()
}
if (-not (Test-Path -Path $Destination))
{
New-Item -Path $Destination -ItemType Directory -Force
}

$Parameters = @{
Source = "$DownloadsFolder\Extensions\$Extension"
Destination = "$DownloadsFolder\Extensions"
File = "manifest.json"
}
ExtractZIPFile @Parameters
$Entries | ForEach-Object -Process {[IO.Compression.ZipFileExtensions]::ExtractToFile($_, "$($Destination)\$($_.Name)", $true)}

# Get the author id
$manifest = Get-Content -Path "$DownloadsFolder\Extensions\manifest.json" -Encoding Default -Force | ConvertFrom-Json
# Some extensions don't have valid JSON manifest
if ($manifest.applications.gecko.id)
{
$ApplicationID = $manifest.applications.gecko.id
}
elseif ($manifest.browser_specific_settings.gecko.id)
{
$ApplicationID = $manifest.browser_specific_settings.gecko.id
}
$ZIP.Dispose()
}

if (-not (Test-Path -Path "$DownloadsFolder\Extensions\$ApplicationID.xpi"))
{
Rename-Item -Path "$DownloadsFolder\Extensions\$Extension" -NewName "$ApplicationID.xpi" -Force
}
$Parameters = @{
Source = "$DownloadsFolder\Extensions\$Extension"
Destination = "$DownloadsFolder\Extensions"
File = "manifest.json"
}
ExtractZIPFile @Parameters

# Getting Firefox profile name
$String = (Get-Content -Path "$env:APPDATA\Mozilla\Firefox\installs.ini" -Encoding Default | Select-String -Pattern "^\s*Default\s*=\s*.+" | ConvertFrom-StringData).Default
$ProfileName = Split-Path -Path $String -Leaf
# Get the author id
$manifest = Get-Content -Path "$DownloadsFolder\Extensions\manifest.json" -Encoding Default -Force | ConvertFrom-Json
# Some extensions don't have valid JSON manifest
if ($manifest.applications.gecko.id)
{
$ApplicationID = $manifest.applications.gecko.id
}
elseif ($manifest.browser_specific_settings.gecko.id)
{
$ApplicationID = $manifest.browser_specific_settings.gecko.id
}

if (-not (Test-Path -Path "$env:APPDATA\Mozilla\Firefox\Profiles\$ProfileName\extensions"))
{
New-Item -Path "$env:APPDATA\Mozilla\Firefox\Profiles\$ProfileName\extensions" -ItemType Directory -Force
}
if (-not (Test-Path -Path "$DownloadsFolder\Extensions\$ApplicationID.xpi"))
{
Rename-Item -Path "$DownloadsFolder\Extensions\$Extension" -NewName "$ApplicationID.xpi" -Force
}

# Copy .xpi extension file to the extensions folder
if (-not (Test-Path -Path "$env:APPDATA\Mozilla\Firefox\Profiles\$ProfileName\extensions\$ApplicationID.xpi"))
{
Copy-Item -Path "$DownloadsFolder\Extensions\$ApplicationID.xpi" -Destination "$env:APPDATA\Mozilla\Firefox\Profiles\$ProfileName\extensions" -Force
if (-not (Test-Path -Path "$env:APPDATA\Mozilla\Firefox\Profiles\$ProfileName\extensions"))
{
New-Item -Path "$env:APPDATA\Mozilla\Firefox\Profiles\$ProfileName\extensions" -ItemType Directory -Force
}

# Copy .xpi extension file to the extensions folder
if (-not (Test-Path -Path "$env:APPDATA\Mozilla\Firefox\Profiles\$ProfileName\extensions\$ApplicationID.xpi"))
{
Copy-Item -Path "$DownloadsFolder\Extensions\$ApplicationID.xpi" -Destination "$env:APPDATA\Mozilla\Firefox\Profiles\$ProfileName\extensions" -Force
}
}
}
}

# Retrive extensions' ID to check path for
$Parameters = @{
ExtensionUris = @(
"https://addons.mozilla.org/firefox/addon/ublock-origin",
"https://addons.mozilla.org/firefox/addon/traduzir-paginas-web",
# "https://addons.mozilla.org/firefox/addon/tampermonkey",
# https://greasyfork.org/ru/scripts/436115-return-youtube-dislike
"https://addons.mozilla.org/firefox/addon/sponsorblock",
"https://addons.mozilla.org/firefox/addon/return-youtube-dislikes"
)
"https://addons.mozilla.org/firefox/addon/ublock-origin" = "$env:APPDATA\Mozilla\Firefox\Profiles\ProfileName\extensions\[email protected]"
"https://addons.mozilla.org/firefox/addon/traduzir-paginas-web" = "$env:APPDATA\Mozilla\Firefox\Profiles\ProfileName\extensions\{036a55b4-5e72-4d05-a06c-cba2dfcc134a}.xpi"
"https://addons.mozilla.org/firefox/addon/sponsorblock" = "$env:APPDATA\Mozilla\Firefox\Profiles\ProfileName\extensions\[email protected]"
# https://greasyfork.org/ru/scripts/436115-return-youtube-dislike
"https://addons.mozilla.org/firefox/addon/return-youtube-dislikes" = "$env:APPDATA\Mozilla\Firefox\Profiles\ProfileName\extensions\{762f9885-5a13-4abd-9c77-433dcd38b8fd}.xpi"
}
Add-FirefoxExtension @Parameters

Expand Down

0 comments on commit 6fc8584

Please sign in to comment.