Can Firefox's User.js file have conditional user_pref() statements?

734 views Asked by At

Is it possible to create a conditional user_pref() in Mozilla Firefox user.js?

I have tried using an if-else statement, but the contents of the first if statement is executed, even if the given condition is false. The contents of the else statement is ignored.

Example #1:

if ( false )
{
    // This is executed even though the given condition is false in Mozilla Firefox user.js.
    user_pref( 'browser.display.background_color', '#000000' );
}

else
{
    // Unfortunately, this is not executed in Mozilla Firefox user.js.
    user_pref( 'browser.display.background_color', '#FFFFFF' );
}

Additionally, I have also tried using a ternary expression to conditionally change a user preference, but the expression is skipped entirely.

Example #2:

// This is not executed at all in Mozilla Firefox user.js.
user_pref( 'browser.display.background_color', ( 1 === 2 ? '#000000' : '#FFFFFF' ) );

Overall, I am trying to eventually find a way to change user_pref() properties conditionally depending on the contents of window.location.href.

2

There are 2 answers

0
Brock Adams On BEST ANSWER

Reference the documentation for Firefox's User.js file. Nowhere does it claim that the file takes javascript or any other (Turing complete) programming language.

The file takes either comments or user_pref() lines only.

So, conditional statements are not allowed.

However, since you are modifying styles in your example, you can accomplish the same thing with user style CSS and/or the Stylish add-on (recommended). There you can use some logic, based on the domain and/or URL.

For logic about other, non-style, prefs, you might be able to write an extension to do what you wish.

1
swiftzor On

First of all are you sure you want to use === and not ==? Remember === uses a strict conversion and not just typecasting so in most cases == will work fine.

Additionally if you are setting some sort of background color of theme it is better to use that data directly instead of just setting it in a preference setting (i.e. storing the value in a variable then storing that in the user.js preference setting).