Customizing Firefox - Advanced Autoconfig Files
We're almost done with autoconfig files. My previous posts covered what an autoconfig file is and how to create one. Now we're going to discover how truly powerful the autoconfig feature is.
I've been saying this over and over, but it's worth repeating:
Autoconfig files are Javascript files that have full access to all components of Firefox.
This means we can use autoconfig files to change just about anything the browser that doesn't involve the user interface (and there are even ways to change that).
So how do we access the various components? The best way to use those components is via a helper Javascript module called Services.jsm. Services.jsm provides shortcuts to access frequently used components within Firefox. So for instance, if you wanted to display an alert to a user from inside your autoconfig file, you could use:
Components.utils.import("resource://gre/modules/Services.jsm");
Services.prompt.alert(null, "Title", "Message");
nsIPromptService shows all the functions you can use to prompt a user, including confirmations as well as data input
Or what if you wanted to add a bookmark? Unfortunately, the bookmarks services isn't in Services.jsm, but it's easy to get:
//
Components.utils.import("resource://gre/modules/Services.jsm");
var uri = Services.io.newURI("http://mike.kaply.com", null, null);
var navBookmarksService = Components.classes["@mozilla.org/browser/nav-bookmarks-service;1"]
.getService(Components.interfaces.nsINavBookmarksService);
navBookmarksService.insertBookmark(navBookmarksService.toolbarFolder, uri, -1, "Mike's Blog");
As you can see, you can pretty much do anything from an autoconfig file. I couldn't even begin to scratch the surface. The best place to start looking is MDN (or just ask in the comments).
Before we finish up, I want to give one other code snippet that might be useful for folks. In a couple posts, we'll be covering userChrome.css and userContent.css Putting these files in a user's profile directory is a common way to do simple customization of the browser. Unfortunately that usually requires setting up a default profile (which is another thing we'll be talking about in a few posts). By using an autoconfig file to write out these files, we avoid having to manually place them in each user's profile:
//
const Cc = Components.classes;
const Ci = Components.interfaces;
Components.utils.import("resource://gre/modules/Services.jsm");
var profileDir = Services.dirsvc.get("ProfD", Ci.nsILocalFile);
profileDir.append("chrome");
if( !profileDir.exists() || !profileDir.isDirectory() ) {
profileDir.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0777);
}
profileDir.append("userChrome.css");
var fos =
Cc["@mozilla.org/network/file-output-stream;1"].createInstance(Ci.nsIFileOutputStream);
fos.init(profileDir, -1, -1, false);
var css = "#context-setDesktopBackground { display: none;}";
fos.write(css, css.length);
fos.close();
This example removes the menuitem that changes the desktop background.
Autoconfig files are VERY powerful, even more powerful then even I realized. In combination with userContent.css and userChrome.css, you can do a lot.
So when do you choose to use autoconfig files? The best thing about autoconfig files is how easy they are to get into Firefox. No need for an add-on or anything like that. You just drop in two files and you're done. You can also put the configuration file on a server which makes things even easier. So you should choose autoconfig files when the changes you are making to Firefox don't involve major changes to the user interface. You should also choose autoconfig files if you need to lock preferences (although you can lock preferences with the CCK - we'll talk about that later).
I'm trying to add popup and cookie exceptions to my autoconfig file but I can't get it to work. Here's what I've tried:
Components.utils.import("resource://gre/modules/Services.jsm");
var uri = Services.io.newURI("http://utexas.edu", null, null);
Services.perms.add(uri, "popup", 1);
Services.perms.add(uri, "cookie", 1);
It generates no errors (that I'm aware of) but neither does it add the exceptions. Everything else in the autoconfig file is getting applied (numerous lockPref() and pref() calls earlier in the file) and I can even get an alert to appear after importing the Services module so I guess that I'm either building the URI or adding the exception incorrectly (or both).
I just cut and pasted your exact code and put it into an autoconfig file and it worked for me. Did you try it by itself in an autoconfig file? (If you do decide to cut and paste to retest, don't forget to fix the double quotes that wordpress messes with)
Indeed it does... I tracked it down to one pref in the autoconfig file:
pref("privacy.clearOnShutdown.siteSettings", true);
"Why yes, of course!" Firefox is launched (for the first time), the autoconfig settings get applied, Firefox relaunches itself and deletes the popup and cookie exceptions as instructed.
I guess that's what I get for being overly paranoid about browsing privacy.
Thanks for the response.
OK, I've got a question for you. I'm trying to set the cache directory to point to a location outside the user home directory. Using the autoconfig file, I set browser.cache.disk.parent_directory to the directory I want: inside Firefox's about:config page I can see that setting, italicized. However, the cache stays right where it is.
If I set it manually in the prefs.js file, the next time Firefox starts the cache goes to the preferred location. However, upon quitting Firefox writes out the preferences and removes that pref.
Any ideas?
What platform are you on? If you are on Windows, did you set the preference with double slashes?
See:
http://kb.mozillazine.org/Browser.cache.disk.parent_directory
I'm on Red Hat Enterprise Linux, running Firefox 10.0.3. I have Windows machines in the company, but I have not tested to see if the cache redirection works on them.
thx
anthony
Sorry for the delay. I've tested this on Windows and it's working for me.
Are you setting the cache in the config file or via a remote config file?
Can you post the exact line you are using to set/lock the prefs?
Hi,
First, many thanks for your "musings" that have been highly time-saving for me.
Is there a way to get a reference to the browser object from the autoconfig file in order to add tab-related event handlers ?
Regards.
The autoconfig is processed very early, so anything browser related wouldn't be setup yet.
Something like that would be best handled via an add-on.
Referring to your post: "This means we can use autoconfig files to change just about anything the browser that doesn't involve the user interface (and
there are even ways to change that). "
Since long, I have been trying to disable buttons in Tools -> Options and also disable the whole tab like "Advanced" tab. Can I achieve this using auto config files.
Your suggestions in this regards will be really helpful.
When it comes to user interface, most of the time it's easier to just write an extension to do it.
Some preference options can be disabled via preferences (if you lock the preferences, it disables the item).
Another option is to write out the file userChrome.css to disable things. That can be done via autoconfig.
any chance you have something to change to a new profile?
for example I want to run firefox esr and firefox nightly with two different profiles. and it would be great if one could automate this.
You can bring up the profile manager by typing:
firefox -profile-manager
Uncheck the box that says "Don't ask at startup".
Then create a new profile for your Nightly.
When you start Firefox, you can start it with "firefox -p "Profile name" -no-remote and then you can start the second Firefox the same way.
I was thinking of distributin the two firefox versions to other users than me. so idealy the profile would just be default in firefox esr and in nightly a new profile created and used automaticly. so without the user doing anything
Ah.
The only way I know to do that would be to have the shortcuts for the two Firefox versions point to different directories for their profiles.
If you start Firefox with -profile "PATH" you can specify where the profile is initially created.
So you could have icons for the different firefoxes like:
firefox -profile "C:\Users\foo\ESR"
and
firefox -profile "C:\Users\foo\Nightly"
That's the only thing that I can think of...
not a bad idea. I will test that out.
I also posted an argument change in mozilla suggestion box about a autocreat profile.
many thanks
works great!
C:\Programfiler\Nightly\firefox.exe -profile "%appdata%\Mozilla\Firefox\Profiles\thor" -no-remote
not sure if -no-remote is necessary
many thanks
I am trying to hide Sync,Security,Privacy and advanced tab.
Firefox version is 19.0
I think this can be achieved via creating userchrome.css file and saving it under chrome folder. However, i am not sure how to write codes in this file. Also, there is no chrome folder in the location where the executable of firefox exists.
Please help me.
userChrome.css goes into the profile directory, not the Firefox directory.
See:
http://kb.mozillazine.org/index.php?title=UserChrome.css&printable=yes
Thanks for the response. I checked the link but there is no mention that i can hide the Security or Privacy tab under Firefox options. Please advise if this is possible using .css file. If yes, please help me with an example code which can hide any of these option so that i can use it to hide other options as per the requirement.
Thanks in advance
You'll need to do some research to find the answers you're looking for. You'll need to find the IDs of the elements you want to hide. From http://mike.kaply.com/2012/03/30/customizing-firefox-default-profiles/