-
Notifications
You must be signed in to change notification settings - Fork 519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Script to clean deprecated prefs from prefs.js #303
Comments
It is a bit ugly. But WTH, will be good for first post. # If FF is running then exit, since any change to prefs.js will be overridden when FF if closed
if (Get-Process firefox -ErrorAction SilentlyContinue) { Write-Host 'Firefox must be closed to proceed. Exiting...'; Exit }
# Find the version(s) of installed FF(s)
if (($InstalledVersion = Get-ItemProperty 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\Mozilla Firefox *', 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Mozilla Firefox *' | Sort-Object DisplayName | Select-Object @{Name='Version';Expression={$_.DisplayVersion.Split('.')[0]}}, @{Name='Name'; Expression={$_.DisplayName}}) -eq $null) { Write-Host 'No Firefox installed. Exiting...'; Exit }
# Download latest ghacks user.js
Try { $ghacks_user_js = (Invoke-WebRequest -Uri https://raw.githubusercontent.com/ghacksuserjs/ghacks-user.js/master/user.js).Content } Catch { Write-Host 'There might be no internet connection. Exiting...'; Exit }
# Get only Section 9999 without a header and parrot from ghacks user.js
$Section9999 = ([regex]'\/\*\*\*\s9999:.*\/\*\sEND:').Match($ghacks_user_js.Replace("`n", "°")).Value # get deprecated section
$Section9999 = ([regex]'°\/\*\sFF.*\/\*').Match($Section9999).Value # strip header and kill the parrot
# Display all FFs installed on computer
Write-Host 'Firefox installation(s) found:'
$InstalledVersion | ForEach { "$($_.Version) : $($_.Name)" }
Write-Host ''
# Get FF valid selection options
$ValidVersions = @(($Section9999.Split('°') | Select-String -Pattern '^\/\S\sFF.*') | ForEach { $_ -match '^\/\S\sFF(\d{2}).*' | Out-Null; $Matches[1] })
# Ask up to which (including) version should deprecated be considered
$Version = Read-Host -Prompt "Enter Firefox version (Default: $($InstalledVersion[0].Version); 0: Exit)"
if ($Version -eq 0) { Exit }
if (!$Version) { $Version = $InstalledVersion[0].Version }
if ($ValidVersions -notcontains $Version) { Write-Host 'Selection input error. Exiting...'; Exit }
Write-Host "Selected version: $Version"
Write-Host ''
# Get deprecated prefs for users version selection
$DeprecatedPrefs = @($([regex]".*°\/(\*|\/)\sFF$Version(?=.*)(?(?!\/{2}\s(?:\*{3}|(\*\s){3})\/).)*\/{2}\s(?:\*{3}|(\*\s){3})\/").Match($Section9999).Value -split '°' | Select-String -Pattern 'user_pref' -SimpleMatch | ForEach { $_.ToString().Split('"')[1] })
# Get and parse profiles.ini to find the profile(s) folder(s)
if (!(Test-Path $env:APPDATA\Mozilla\Firefox\Profiles.ini)) { Write-Host "Profile folder can't be found. Exiting..."; Exit }
$FirefoxProfiles = @([String]::Join(' ', (Get-Content $env:APPDATA\Mozilla\Firefox\Profiles.ini)).Replace('[',"`nProfile=").Replace(']','').Trim().Split("`n") | ForEach { New-Object psobject -Property (ConvertFrom-StringData -StringData $_.Trim().Replace(' ',"`n")) })
$FirefoxProfiles = @($FirefoxProfiles | where { $_.Name -ne $null -and $_.Path -ne $null} | Sort-Object Default, Name)
$FirefoxProfiles | ForEach { if ($_.IsRelative) { $_.Path = "$env:APPDATA\Mozilla\Firefox\$($_.Path.Replace('/','\'))"}}
Write-Host ''
# Display FF(s) profile(s)
Write-Host 'Firefox profile(s) found:'
$index = 1
$FirefoxProfiles | ForEach { "$index : $($_.Name) ($($_.Path))"; $index++ }
"$index : Custom path"
Write-Host ''
# Ask for profile to be processed
$SelectedProfile = (Read-Host -Prompt 'Enter profile index (Default: 1; 0: Exit)').ToString().Trim()
if (!$SelectedProfile) { $SelectedProfile = 1 }
if ($SelectedProfile -eq 0) { Exit }
Try { $SelectedProfile = [Int]$SelectedProfile } Catch { Write-Host 'Selection input error. Exiting...'; Exit }
if ($SelectedProfile -gt ($FirefoxProfiles.Count + 1)) { Write-Host 'Selection input error. Exiting...'; Exit }
Write-Host "Selected profile: $SelectedProfile"
Write-Host ''
# If custom is selected, enter the folder of interest
if ($SelectedProfile -eq $FirefoxProfiles.Count + 1) { $ProfilePath = (Read-Host -Prompt 'Enter the profile path').TrimEnd('\') } else { $ProfilePath = "$($FirefoxProfiles[$SelectedProfile - 1].Path)" }
# Read the prefs.js file to be processed
if (!(Test-Path $ProfilePath\prefs.js)) { "$ProfilePath\prefs.js could not be found. Exiting..."; Exit }
$CurrentPrefs = Get-Content $ProfilePath\prefs.js
# Enumerate the prefs that will to be removed from prefs.js
$RemovedPrefs = $CurrentPrefs | where { $DeprecatedPrefs -contains $_.Split('"')[1] }
# Enumerate the prefs.js with removed deprecated prefs
$NewPrefs = $CurrentPrefs | where { $DeprecatedPrefs -notcontains $_.Split('"')[1] }
# If there is any prefs that needs to be removed the continue, otherwise exit without any changes
if ($RemovedPrefs) {
$TimeStamp = (Get-Date).ToString("yyyy.MM.dd-HH.mm.ss")
# Make backup of prefs.js
Move-Item $ProfilePath\prefs.js "$ProfilePath\prefs.js-$TimeStamp-backup.txt"
# Create new prefs.js without deprecated prefs
$NewPrefs | Out-File $ProfilePath\prefs.js -Encoding Default -Force
# Create log file with prefs that has been removed
$RemovedPrefs | Out-File "$ProfilePath\prefs.js-$TimeStamp-removed.txt" -Encoding Default -Force
Write-Host 'Done!'
Write-Host ''
Write-Host "Backup of original: $ProfilePath\prefs.js-$TimeStamp-backup.txt"
Write-Host "Removed prefs log : $ProfilePath\prefs.js-$TimeStamp-removed.txt"
Exit
}
Write-Host 'No changes needed. Exiting...' Last edit: just cosmetics |
It takes all prefs (commented and not commented) from Section 9999 up to and included your version of FF. But you can also choose to override by entering desired version of FF. What do you think with entire user.js? And yes, all deprecated prefs are then removed from users prefs.js. |
Why local? Who says that user didn't strip out Section 9999? |
Updated. |
Implementing local would be simple. Upper script looks complicated, since it takes ghachs-user.js from the web and parses all deprecated up to, including, you version of FF. Script would be a few lines long if I do it to process only the folder in which the script is started (so there is no profile detection needed) and with the use of file with a list of deprecated prefs of interest (so there is no FF version detection needed). I actually don't need time... if you find that I could be in any help here, then here I am to use me. :) It would also be no hard feeling in case you just close this "issue". Cheers |
For convenience, I could add similar functionality to the batch updater if there was a demand for it (or if I'm bored and can spare the time to do so). |
I haven't yet tested the script @crssi provided here, but if I understand correctly it already more or less covers that. For a standalone solution, the only advantage a batch script would have over this powershell script, as far as I know, is that it wouldn't require the user to change powershell's execution policy first (which defaults to restricted). However, if you hosted the script in this repository you could simply add instructions for that in the wiki or somewhere else. It is as simple as:
It seems it would still need a few changes to make it work exactly the way you describe, though. Also the RemoteSigned policy requires that users create the .ps1 files themselves, otherwise they would need to set the execution policy to Unrestricted. |
this would not detect any prefs that we removed from the user.js |
Pants, is there any good reason not to remove every preference (active and inactive) in user.js from prefs.js? Active preferences would end up in prefs.js anyway, and it would simplify such a script a lot. amirite? The only downside I see: a log would include removed active preferences (making it useless). |
I just meant that if we remove from prefs.js all the preferences in user.js, commented-out (inactive) or not, then the active ones will end up in prefs.js anyway. This would simplify the script since it wouldn't need to determine what is active or inactive in user.js first. Is there any good reason to only remove commented-out preferences instead? I'm asking because, even though removing only inactive preferences as you described is doable, it is not trivial to implement in Windows shell syntax, and can take as much as twice the number of lines of code or even more. It makes the script slower, too. |
I don't see a reason to keep this one open simply because @claustromaniac script makes more sense and even doe PS is more capable, the BAT is easier for end user. |
I came up with a neat way to make the script reset preferences that have long since been removed from the I've been wondering if that would be a worthwhile addition to the script. Maybe as an optional feature or something. What do you guys think? I wouldn't mind writing the code first so you can judge by yourselves if it is good enough or not. Just let me know if you're interested. |
In the wiki there are 9 steps for the scratchpad scripts vs 2 clicks for the prefsCleaner.bat. But yeah, I never said they were hard to use or anything like that.
Yeah, we don't need it, and it's understandable if you don't want one. I get that you're busy enough as it is. That's why I asked before doing anything. Here's another idea that doesn't seem to have any drawbacks. The version 4.3 of the Does that sound better? |
The
Internal settings, of course! I didn't consider that. Taking that into account, this is definitely not a good idea. Good thinking, Pants. |
Sounds pretty shallow when you put it that way, but yeah. I like challenges. Life would be boring without them, and they are somewhat more rewarding when they produce something that can be useful to someone else, even if they don't get to thank me for it. |
You sure? Did you ask everyone? I don't know or care about anyone else's motives.
If that was a reference to The Simpsons, I don't see how it applies to this discussion. Sorry, I know I can be dense sometimes. |
Right, because humans never make futile attempts. |
Anyone interested in that kind of script, before I dig into?
If so, I can make powershell one.
Cheers
The text was updated successfully, but these errors were encountered: