Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firefox addons: How to set default preferences depending on platform?

I am creating an addon for Mozilla Firefox, and one of the preferences is a filepath in the file system (specifically, the executable file or the command for VLC player). That filepath is changeable through the preference window, but an addition of a default value for that preference would be helpful.

Since the addon is supposed to work on both Linux and Windows operating systems, my wish is to include default filepaths for each operating system, so it would look something like this:

Linux: /usr/bin/vlc

Windows x86 C:\\Program Files\\VideoLan\\VLC\\vlc.exe

Windows x86_64 C:\\Program Files (x86)\\VideoLan\\VLC\\vlc.exe (since the default VLC installation will always be 32-bit)

Is there a way to achieve this? So far I can only put one defaults.js file describing the preference (for example for Linux):

pref("extensions.vlc_shortcut.vlc_filepath", "/usr/bin/vlc");

That file is located in my_extension/defaults/preferences/defaults.js, I have tried putting if-else statements in the file, but to no effect. I even tried putting the following if statement:

if(true)
    pref("extensions.vlc_shortcut.vlc_filepath", "/usr/bin/vlc");

but it ignores everything, as if it is a blank file.

like image 972
Milan Kosanovic Avatar asked Oct 20 '25 18:10

Milan Kosanovic


1 Answers

No, you can't set platform-specific values in the default preferences. According to the MDN article on default preferences:

The actual file, despite having .js extension, is not a JavaScript file. You may not set variables inside of it, nor may do any kind of program flow control (ifs, loops etc.) nor even calculated values (i.e. 3600 * 24 * 5). Doing so will cause Mozilla to stop processing your preferences file without any notification, warning, error, or exception. Think of it more as an .ini file. Comments are perfectly acceptable.

Another approach is to use nsIXULRuntime to determine the host OS and path accordingly.

Here's the example from MDN:

var xulRuntime = Components.classes["@mozilla.org/xre/app-info;1"].getService(Components.interfaces.nsIXULRuntime);

alert(xulRuntime.OS);

If desired, you can save the path as a preference using the Preferences API.

Another option to consider is bundling (if the license allows), in which case you can indicate platform-specific files in the manifest.

like image 155
christi3k Avatar answered Oct 22 '25 07:10

christi3k



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!