|
|
Line 1: |
Line 1: |
| {{extdev}}
| | See [http://developer.mozilla.org/en/docs/Building_an_Extension#Defaults_Files Building an Extension - Defaults Files] at [http://developer.mozilla.org MDC]. |
| | |
| | |
| The example code here details a method of copying the default prefs file stored here <code>myext.xpi/defaults/preferences/</code> in your extension to the profile directory during installation. Then on startup it will read the prefs from the copied file and set them as a user prefs if they do not exist. This is a workaround for the lack of support in Mozilla for default preferences in the profile directory. However Mozilla does support default preferences in the application directory so if the user chooses to install your extension there, then the modification code below for [[Install.js]] will simply copy the default prefs file to <code>(appdir)/defaults/pref</code>.
| |
| | |
| | |
| The first step is to add this code to your [[Install.js]] file.
| |
| <pre>
| |
| // Add prefs file
| |
| var prefDir = (this.profileInstall) ? getFolder(getFolder('Profile'),'pref')
| |
| : getFolder(getFolder(getFolder('Program'),'defaults'),'pref');
| |
| if (!File.exists(prefDir)) File.dirCreate(prefDir);
| |
| Install.addFile(null, 'defaults/preferences/' + this.extShortName + '.js', prefDir, null);
| |
| </pre>
| |
| | |
| | |
| Then the second step is to add this code to your main javascript file loaded on startup.
| |
| <pre>
| |
| var DefaultPrefCheck = {
| |
| extName: 'myext', // The name of the default pref file (without the .js part)
| |
|
| |
| check: function()
| |
| {
| |
| var file = Components.classes["@mozilla.org/file/directory_service;1"]
| |
| .getService(Components.interfaces.nsIProperties)
| |
| .get("ProfD", Components.interfaces.nsIFile);
| |
| file.append("pref");
| |
| file.append(this.extName + ".js");
| |
| var prefs = Components.classes["@mozilla.org/preferences-service;1"]
| |
| .getService(Components.interfaces.nsIPrefBranch);
| |
| if (!file.exists()) return; // if not on Mozilla file does not exist so exit
| |
| var data = this.readData(file);
| |
|
| |
| // remove comments and split lines into array
| |
| data = data.replace(/\/\/.*/gm, "").replace(/\/\*([^*][^\/])*[^\0]?\*\//gm, "").split(';');
| |
|
| |
| // check pref type and set, if it does not exist
| |
| for (var i=0; i<data.length; i++)
| |
| {
| |
| //if valid pref line
| |
| if (/^\s*pref\(.*\,.*\)\s*$/i.test(data[i]))
| |
| {
| |
| var pref = data[i].slice(data[i].indexOf('(')+2, data[i].indexOf(',')-1);
| |
| var value = data[i].slice(data[i].indexOf(',')+1, data[i].indexOf(')'));
| |
|
| |
| if (/^\s*false\s*$/i.test(value) || /^\s*true\s*$/i.test(value)) // if boolean pref
| |
| {
| |
| value = (/\s*true\s*/i.test(value));
| |
| try {prefs.getBoolPref(pref);} catch (e) {prefs.setBoolPref(pref, value);}
| |
| }
| |
| else if (/^\s*["][^"]*["]\s*$/.test(value)) // if char pref
| |
| {
| |
| value = value.slice(value.indexOf('"')+1,value.lastIndexOf('"'));
| |
| try {prefs.getCharPref(pref);} catch (e) {prefs.setCharPref(pref, value);}
| |
| }
| |
| else // if integer pref
| |
| {
| |
| value = new Number(value);
| |
| try {prefs.getIntPref(pref);} catch (e) {prefs.setIntPref(pref, value);}
| |
| }
| |
| }
| |
| }
| |
| },
| |
|
| |
| readData: function(file) {
| |
| var data = "";
| |
| var fstream = Components.classes["@mozilla.org/network/file-input-stream;1"]
| |
| .createInstance(Components.interfaces.nsIFileInputStream);
| |
| var sstream = Components.classes["@mozilla.org/scriptableinputstream;1"]
| |
| .createInstance(Components.interfaces.nsIScriptableInputStream);
| |
| fstream.init(file, 1, 0, false);
| |
| sstream.init(fstream);
| |
|
| |
| var str = sstream.read(-1);
| |
| while (str.length > 0) {
| |
| data += str;
| |
| str = sstream.read(-1);
| |
| }
| |
|
| |
| sstream.close();
| |
| fstream.close();
| |
|
| |
| return data;
| |
| }
| |
|
| |
| }
| |
| | |
| DefaultPrefCheck.check();
| |
| </pre>
| |
| | |
| [[Category:Development]]
| |