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:
[code type=”javascript”]
Components.utils.import("resource://gre/modules/Services.jsm");
Services.prompt.alert(null, "Title", "Message");
[/code]
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:
[code type=”javascript”]
//
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");
[/code]
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:
[code type=”javascript”]
//
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();
[/code]
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).
Leave a Reply