Skip to content
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

Closed
crssi opened this issue Dec 3, 2017 · 17 comments
Closed

Script to clean deprecated prefs from prefs.js #303

crssi opened this issue Dec 3, 2017 · 17 comments

Comments

@crssi
Copy link

crssi commented Dec 3, 2017

Anyone interested in that kind of script, before I dig into?
If so, I can make powershell one.

Cheers

@crssi
Copy link
Author

crssi commented Dec 4, 2017

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

@crssi
Copy link
Author

crssi commented Dec 4, 2017

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?
Your ghacks user.js as a source or users user.js file?

And yes, all deprecated prefs are then removed from users prefs.js.
Of course the original prefs.js is backedup together with a change log file.

@crssi
Copy link
Author

crssi commented Dec 4, 2017

Give me please couple of hours... I have found one non-lethal bug (annoyance).
I will also add comments, to see clearly what does what.

Why local? Who says that user didn't strip out Section 9999?
I am using your web file, since is most up-to-date and it really has Section 9999 to be used as good source of what is deprecated.

@crssi
Copy link
Author

crssi commented Dec 4, 2017

Updated.
Cheers

@crssi
Copy link
Author

crssi commented Dec 7, 2017

Implementing local would be simple.
Implementing only local would be too 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.
So there is your FF version detection and there is your profile detection.

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. :)
Just let me know what and how would you like and will do.

It would also be no hard feeling in case you just close this "issue".

Cheers

@claustromaniac
Copy link
Contributor

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).

@claustromaniac
Copy link
Contributor

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:

  1. Save the above script with a .ps1 extension.
  2. Open the command line prompt, and enter the following:
powershell
Set-ExecutionPolicy RemoteSigned
  1. Run the script.

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.

@earthlng
Copy link
Contributor

THIS would be an always-up-to-date solution, rather than the scratchpad sripts

this would not detect any prefs that we removed from the user.js

@claustromaniac
Copy link
Contributor

claustromaniac commented Dec 12, 2017

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).

@claustromaniac
Copy link
Contributor

claustromaniac commented Dec 12, 2017

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.

@crssi
Copy link
Author

crssi commented Dec 14, 2017

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.

@claustromaniac
Copy link
Contributor

I came up with a neat way to make the script reset preferences that have long since been removed from the user.js in addition to those that still are in that file. The method uses an internal list that works like arrays in other scripting/programming languages. It's far less complex than it sounds and the script would then replicate the functionality of the scratchpad scripts, but it would be a bit more comfortable to use.

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.

@claustromaniac
Copy link
Contributor

claustromaniac commented Jan 18, 2018

takes all of 3 seconds to run

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.

I don't think we need another list to maintain ("method uses an internal list"),

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 updater.bat (if accepted) will not overwrite previous backups of the user.js, it will create cumulative backups instead. Since the prefsCleaner.bat already uses user.js as an external list, it would be useful if it was able to reset preferences using all of the backups as sources instead of just the current user.js. It could be an optional feature (or default behaviour, I don't care), and it would help because the script would never miss any of the preferences that get removed from user.js, as long as one does not remove any backups from the profile folder before running the script. Then you guys wouldn't even need to temporarily make the preferences that you want to remove inactive, you could just remove them instead.

Does that sound better?

@claustromaniac
Copy link
Contributor

is it really necessary?

The prefsCleaner.bat is not necessary at all. This was never about fulfilling needs.

and the e10s section should not be reset IMO as some are set internally depending on HW...

Internal settings, of course! I didn't consider that. Taking that into account, this is definitely not a good idea. Good thinking, Pants.

@claustromaniac
Copy link
Contributor

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.

@claustromaniac
Copy link
Contributor

None of us here are doing it for the reward

You sure? Did you ask everyone?

I don't know or care about anyone else's motives.

But if it makes you feel better, I can gift you two rocks that keep tigers away :) Send me your address...

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.

@claustromaniac
Copy link
Contributor

There is no reward.. cogito ergo....

Right, because humans never make futile attempts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants