Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

about config preferences and js

I wonder if it is possible to get values of certain preferences set in about:config using javascript?

Incentive is to get value of preferences set in firefox addon i've created when user lands on addon front end. Basically, I'm trying to identify users landing on FE without asking them to login explicitly.

like image 779
Bojan Babic Avatar asked Sep 26 '10 00:09

Bojan Babic


People also ask

What is Prefs js in Firefox?

The prefs. js file is a legitimate system file that stores user preference settings that differ from the default values. Its predefined location is %APPDATA%\Roaming\Mozilla\Firefox\Profiles\ folder. However, some anti-malware detect prefs. js as a potentially unwanted program (PUP).

Can I delete Prefs js?

You can delete prefs. js. and not save personal files when you uninstall. that your prefs settings are causing slow startup.

What is about config in Firefox?

The Configuration Editor (about:config page) lists Firefox settings known as preferences that are read from the prefs. js and user. js files in the Firefox profile and from application defaults. Many of these preferences are advanced settings that are not present in the Settings panel.


1 Answers

Yes, you can.

First, you need to know that Mozilla uses the XPCOM interfaces for the preferences system.

Three used interfaces are nsIPrefService, nsIPrefBranch and nsIPrefBranch2.

The preferences service is instantiated in the same way you instantiate any XPCOM service.

Two examples to make it clear:

// Get the root branch
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
                    .getService(Components.interfaces.nsIPrefBranch);

.

// Get the "extensions.myext." branch
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
                    .getService(Components.interfaces.nsIPrefService);
prefs = prefs.getBranch("extensions.myext.");

And there are 3 types of preferences, they're string, integer and boolean. There are six methods in nsIPrefBranch that read and write preferences: getBoolPref(), setBoolPref(), getCharPref(), setCharPref(), getIntPref() and setIntPref().

More examples on that:

// Get the "accessibility." branch
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
                    .getService(Components.interfaces.nsIPrefService).getBranch("accessibility.");

// prefs is an nsIPrefBranch.
// Look in the above section for examples of getting one.
var value = prefs.getBoolPref("typeaheadfind"); // get a pref (accessibility.typeaheadfind)
prefs.setBoolPref("typeaheadfind", !value); // set a pref (accessibility.typeaheadfind)

You can also use complex types. By using nsISupportsString, that is used to handle strings in preferences, so, use this when the preference value may contain non-ASCII characters.

Example:

// prefs is an nsIPrefBranch

// Example 1: getting Unicode value
var value = prefs.getComplexValue("preference.with.non.ascii.value",
      Components.interfaces.nsISupportsString).data;

// Example 2: setting Unicode value
var str = Components.classes["@mozilla.org/supports-string;1"]
      .createInstance(Components.interfaces.nsISupportsString);
str.data = "some non-ascii text";
prefs.setComplexValue("preference.with.non.ascii.value", 
      Components.interfaces.nsISupportsString, str);

I hope you solve your doubt with this.

More on this page.

like image 117
gfe Avatar answered Sep 27 '22 22:09

gfe