-
Notifications
You must be signed in to change notification settings - Fork 52
/
install.ps1
364 lines (311 loc) · 10.4 KB
/
install.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<#
.SYNOPSIS
Install script for ff2mpv extension for Windows (10/11).
.DESCRIPTION
The install script will setup few things to make the backend work
for the ff2mpv extension.
- Add registry entry for browser.
- Add a json manifest for the extension.
- Update ff2mpv.bat with valid python command.
- Show informative messages (like missing mpv in path).
.LINK
https://github.com/woodruffw/ff2mpv
.LINK
https://github.com/woodruffw/ff2mpv/wiki/Installation-on-Windows
.PARAMETER Uninstall
Use to remove the registry key for ff2mpv
.PARAMETER Help
Prints the help message
.INPUTS
- Name of the browser to install ff2mpv entry in registry.
- Registry path for NativeMessagingHosts (custom browser install only).
.OUTPUTS
- Logs of script processing
- Entry in registry for ff2mpv
- Json file for NativeMessagingHosts
- Batch script ff2mpv.bat
.EXAMPLE
./install.ps1 librewolf
.EXAMPLE
./install.ps1 custom-firefox "Registry::HKEY_CURRENT_USER\SOFTWARE\Mozilla"
.EXAMPLE
./install.ps1 chromium -uninstall
.EXAMPLE
./install.ps1 custom-chromium "Registry::HKEY_CURRENT_USER\SOFTWARE\Google\Chrome" -uninstall
.EXAMPLE
./install.ps1 -help
.EXAMPLE
./install.ps1
.NOTES
Some browsers like librewolf or brave use other browser's registry entries.
#>
param (
[Switch] $Uninstall = $false,
[Switch] $Help = $false
)
$browser = $args[0]
$custom_hkcu = $args[1]
# Repository location
$repository_path = $PSScriptRoot
# Hold registry path in HKEY_CURRENT_USER
$hkcu_dest = ""
# Json
$json_path = $repository_path
# Edit json file for chromium
$use_chromium_json = $true
# Chrome Store Extension ID
$chrome_extension_id = "chrome-extension://ephjcajbkgplkjmelpglennepbpmdpjg/"
# List of browsers that this script support
$supported_browsers = @(
'firefox',
'librewolf',
'chromium',
'chrome',
'edge',
'brave',
'custom-chromium',
'custom-firefox'
)
function testCommand {
Param ($command)
$oldPreference = $ErrorActionPreference
$ErrorActionPreference = 'stop'
try {
if(Get-Command $command){return $true }
} catch { return $false }
finally { $ErrorActionPreference = $oldPreference }
}
function help {
Write-Output "
Installing script for ff2mpv
Command syntax: > .\install.ps1 [browser] [registry_path] [-uninstall] [-help]
Arguments:
browser: The name of the browser to install:
- $($supported_browsers -join "`n - ")
registry_path: The path in the registry for the NativeMessagingHost directory
Only needed if browser is `"custom-chromium`" or `"custom-firefox`"
Flags:
-uninstall Uninstall the registry key value for the given browser
-help Show this message
Usage:
Install a supported browser (E.g. chromium):
> .\install.ps1 chromium
Install a custom browser (E.g. google chrome):
> .\install.ps1 custom-chromium `"Registry::HKEY_CURRENT_USER\SOFTWARE\Google\Chrome`"
Uninstall a supported browser (E.g. librewolf):
> .\install.ps1 librewolf -uninstall
Uninstall a custom browser (E.g. firefox):
> .\install.ps1 custom-firefox `"Registry::HKEY_CURRENT_USER\SOFTWARE\Mozilla`" -uninstall
Print help
> .\install.ps1 -help
"
}
function select_browser () {
switch -Regex ($browser) {
"^(firefox|librewolf)$" {
$script:hkcu_dest = "Registry::HKEY_CURRENT_USER\SOFTWARE\Mozilla"
$script:json_path = "$script:json_path\ff2mpv-windows.json"
$script:use_chromium_json = $false
break
}
"^chrome$" {
$script:hkcu_dest = "Registry::HKEY_CURRENT_USER\SOFTWARE\Google\Chrome"
$script:json_path = "$script:json_path\ff2mpv-windows-chrome.json"
break
}
"^chromium$" {
$script:hkcu_dest = "Registry::HKEY_CURRENT_USER\SOFTWARE\Chromium"
$script:json_path = "$script:json_path\ff2mpv-windows-chrome.json"
break
}
"^edge$" {
$script:hkcu_dest = "Registry::HKEY_CURRENT_USER\SOFTWARE\Microsoft\Edge"
$script:json_path = "$script:json_path\ff2mpv-windows-chrome.json"
break
}
"^brave$" {
$script:hkcu_dest = "Registry::HKEY_CURRENT_USER\SOFTWARE\Google\Chrome"
$script:json_path = "$script:json_path\ff2mpv-windows-chrome.json"
break
}
"^custom-(chromium|firefox)$" {
if (-not $custom_hkcu) {
Write-Output "Please enter a path for HKEY_CURRENT_USER"
$new_path = Read-Host -Prompt "> "
$new_path = $new_path.Trim()
if ($new_path -like 'exit*') {
exit 0
} elseif ($new_path -like 'help*') {
help
exit 0
}
$script:custom_hkcu = $new_path
select_browser
break
} else {
$script:hkcu_dest = $custom_hkcu.replace("/", "\")
if (-not $script:hkcu_dest.StartsWith('Registry::','CurrentCultureIgnoreCase')) {
$script:hkcu_dest = "Registry::$script:hkcu_dest"
}
}
}
"^custom-chromium$" {
$script:json_path = "$script:json_path\ff2mpv-windows-$($script:hkcu_dest.split("\")[-1]).json"
break
}
"^custom-firefox$" {
$script:use_chromium_json = $false
$script:json_path = "$script:json_path\ff2mpv-windows-$($script:hkcu_dest.split("\")[-1]).json"
break
}
"^help$" {
help
exit 0
}
"^exit$" {
exit 0
}
default {
Write-Output ""
Write-Output "Invalid option. Please select a valid browser:"
$supported_browsers | % { Write-Output "> $_" }
Write-Output ""
Write-Output "E.g. To install for chromium:"
Write-Output "> : `"chromium`""
Write-Output ""
Write-Output "For custom options provide the path in the registry as a second argument"
Write-Output "E.g. If you want to custom install Google Chrome you can do:"
Write-Output "> : `"custom-chromium`" `"Registry::HKEY_CURRENT_USER\SOFTWARE\Google\Chrome`""
Write-Output ""
Write-Output "Enter `"help`" to display more information or `"exit`" to quit."
$new_input = Read-Host -Prompt "> "
$new_input = $new_input.Trim()
$brow, $hkcu = $new_input -split '\s+'
$script:browser = $brow
$script:custom_hkcu = $hkcu
select_browser
}
}
}
function install () {
select_browser
$registry_dest = "$hkcu_dest\NativeMessagingHosts"
$ff2mpv = "$registry_dest\ff2mpv"
# Check if registry entry location for browser exists
# The browser itself should create its own entry in registry but
# some browsers default to search in the entries of some more popular browsers
if (
(($browser -eq 'librewolf') -or ($browser -eq 'brave')) -and -not (Test-Path "$hkcu_dest")
) {
# LibreWolf: HKEY_CURRENT_USER\Software\LibreWolf.
# But reads the entries for Mozilla Firefox and ignores the entries for LibreWolf.
#
# Brave: HKEY_CURRENT_USER\Software\BraveSoftware\Brave-Browser
# But reads the entried for Google Chrome and ignores the entries for brave.
Write-Output ""
Write-Output "Creating `"$hkcu_dest`""
Write-Output "entry in registry for $browser"
# WARNING: The -Force will override the registry entry if exists.
# Checking if the path exists (Test-Path) is very important to prevent that.
New-Item -Path "$hkcu_dest" -Force
}
# Test if the browser path entry exist
if (-not (Test-Path $hkcu_dest)) {
Write-Output ""
Write-Output "Please start your browser at least once to generate the required registry entries"
exit 1
}
# Create NativeMessagingHosts if it doesn't exits
if (-not (Test-Path $registry_dest)) {
Write-Output ""
Write-Output "Creating NativeMessagingHosts entry..."
New-Item -Path $registry_dest
}
# Handle ff2mpv registry
if (Test-Path "$ff2mpv") {
$current = (Get-ItemProperty "$ff2mpv")."(default)"
if ($current -eq "$json_path") {
Write-Output ""
Write-Output "Registry key already exists for browser `"$browser`""
} else {
Write-Output ""
Write-Output "Registry key found with value:"
Write-Output "$current"
Write-Output "Overriding with: $json_path"
New-Item -Path "$ff2mpv" -Value "$json_path" -Force
}
} else {
Write-Output ""
Write-Output "Creating registry key in:"
Write-Output "$ff2mpv"
Write-Output "with value: $json_path"
New-Item -Path "$ff2mpv" -Value "$json_path"
}
# Detect Python Launcher
$python = ""
if (testCommand py) {
Write-Output ""
Write-Output "Found `"py`" launcher"
$python = "py -3"
} elseif (testCommand python3) {
$python = "python3"
} elseif (testCommand python) {
$python = "python"
} else {
Write-Output ""
Write-Output "Python not found!"
Write-Output "Please install python and add it to your path to continue..."
exit 1
}
# Batch script update
$bat = "@echo off`ncall $python ff2mpv.py"
[System.IO.File]::WriteAllLines("$repository_path\ff2mpv.bat", $bat)
# JSON manipulation if using chromium based browser
if ($use_chromium_json) {
Write-Output ""
Write-Output "Creating JSON for $browser browser..."
$json_file = Get-Content "$repository_path\ff2mpv-windows.json" -Raw | ConvertFrom-Json
$json_file.PSObject.Properties.Remove('allowed_extensions')
$tmp_list = New-Object System.Collections.ArrayList
$_ = $tmp_list.add($chrome_extension_id)
$json_file | Add-Member -MemberType NoteProperty -Name "allowed_origins" -Value $tmp_list
$json_file | ConvertTo-Json -Depth 32 | Set-Content "$json_path"
} elseif ($browser -eq 'custom-firefox') {
Write-Output ""
Write-Output "Creating JSON for $browser browser..."
Copy-Item "$repository_path\ff2mpv-windows.json" -Destination "$json_path"
}
# Show warning if mpv cannot be found in path
if (-not (testCommand mpv)) {
Write-Output ""
Write-Output "**** WARNING: mpv was not found!!! ****"
Write-Output "Make sure it is installed and in your path to use the extension!!!"
}
Write-Output ""
Write-Output "DONE!"
}
function uninstall () {
select_browser
$registry_dest = "$hkcu_dest\NativeMessagingHosts"
$ff2mpv = "$registry_dest\ff2mpv"
# Test if the browser path entry exist
if (Test-Path "$ff2mpv") {
Write-Output ""
Write-Output "Removing registry key..."
Remove-Item "$ff2mpv"
Write-Output "Remove successful"
} else {
Write-Output ""
Write-Output "Registry key for `"$browser`" not found."
}
}
if ($help) {
help
exit 0
}
if ($uninstall) {
uninstall
} else {
install
}
exit 0