1

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.

Community
  • 1
  • 1
Grant Miller
  • 27,532
  • 16
  • 147
  • 165

2 Answers2

2

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.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
-1

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

swiftzor
  • 15
  • 5
  • Yes, the `1 === 2` is just an example to show a `false` expression, and the question applies to any and all `user_pref()` options. – Grant Miller Aug 30 '17 at 20:57