Return to Snippet

Revision: 33440
at October 8, 2010 20:46 by toto898


Updated Code
/*e.g.

Pref.setIntPref('browser.link','open_newwindow',9);
Pref.getIntPref('browser.link','open_newwindow')
	*/
var Pref = {
    get psvc() {
        return Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
    },
    _get_branch: function(branch) {
        return this.psvc.getBranch(branch+".");
    },
    getIntPref: function(b,prefName) {
        try {
            return this._get_branch(b).getIntPref(prefName);
        } catch(e) {
            Components.utils.reportError(e);
            return null;
        }
    },
    setIntPref: function(b,prefName, value) {
        try {
            this._get_branch(b).setIntPref(prefName, value);
        } catch(e) {
            Components.utils.reportError(e);
        }
    },
    getCharPref: function(b,prefName) {
        try {
            return this._get_branch(b).getCharPref(prefName);
        } catch(e) {
            Components.utils.reportError(e);
            return null;
        }
    },
    setCharPref: function(b,prefName, value) {
        try {
            this._get_branch(b).setCharPref(prefName, value);
        } catch(e) {
            Components.utils.reportError(e);
        }
    },
    getBoolPref: function(b,prefName) {
        try {
            return this._get_branch(b).getBoolPref(prefName);
        } catch(e) {
            Components.utils.reportError(e);
            return null;
        }
    },
    setBoolPref: function(b,prefName, value) {
        try {
            this._get_branch(b).setBoolPref(prefName, value);
        } catch(e) {
            Components.utils.reportError(e);
        }
    },
    getFilePref: function(b,prefName) {
        try {
            var store = im_Pref.getBoolPref("store-in-profile");
            if (store && !oldpref) {
                var ds = imns.Cc["@mozilla.org/file/directory_service;1"].getService(imns.Ci.nsIProperties);
                var profdir = ds.get("ProfD", imns.Ci.nsILocalFile);
                profdir.append("iMacros");
                switch (prefName) {
                case "defdownpath":
                    profdir.append("Downloads");
                    break;
                case "defdatapath":
                    profdir.append("Datasources");
                    break;
                case "deflogpath":
                    break;
                case "defsavepath":
                    profdir.append("Macros");
                    break;
                default:
                    return this._get_branch(b).getComplexValue(prefName, imns.Ci.nsILocalFile);
                }
                return profdir;
            } else {
                if (oldpref) return this._get_branch(oldpref).getCharPref(prefName);
                else return this._get_branch(oldpref).getComplexValue(prefName, imns.Ci.nsILocalFile);
            }
        } catch(e) {
            Components.utils.reportError(e);
            return null;
        }
    },
    setFilePref: function(b,prefName, value) {
        try {
            this._get_branch(b).setComplexValue(prefName, imns.Ci.nsILocalFile, value);
        } catch(e) {
            Components.utils.reportError(e);
        }
    },
    getStringPref: function(b,prefName) {
        try {
            return this._get_branch(b).getComplexValue(prefName, imns.Ci.nsISupportsString).data;
        } catch(e) {
            Components.utils.reportError(e);
            return null;
        }
    },
    setStringPref: function(b,prefName, value) {
        try {
            this._get_branch(b).setComplexValue(prefName, imns.Ci.nsISupportsString, value);
        } catch(e) {
            Components.utils.reportError(e);
        }
    },
    clearPref: function(b,prefName) {
        try {
            this._get_branch(b).clearUserPref(prefName);
        } catch(e) {
            Components.utils.reportError(e);
        }
    }
};

Revision: 33439
at October 8, 2010 20:42 by toto898


Initial Code
/*使用举例 
Pref.setIntPref('browser.link','open_newwindow',9);
Pref.getIntPref('browser.link','open_newwindow')
	*/
var Pref = {
    get psvc() {
        return Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
    },
    _get_branch: function(branch) {
        return this.psvc.getBranch(branch+".");
    },
    getIntPref: function(b,prefName) {
        try {
            return this._get_branch(b).getIntPref(prefName);
        } catch(e) {
            Components.utils.reportError(e);
            return null;
        }
    },
    setIntPref: function(b,prefName, value) {
        try {
            this._get_branch(b).setIntPref(prefName, value);
        } catch(e) {
            Components.utils.reportError(e);
        }
    },
    getCharPref: function(b,prefName) {
        try {
            return this._get_branch(b).getCharPref(prefName);
        } catch(e) {
            Components.utils.reportError(e);
            return null;
        }
    },
    setCharPref: function(b,prefName, value) {
        try {
            this._get_branch(b).setCharPref(prefName, value);
        } catch(e) {
            Components.utils.reportError(e);
        }
    },
    getBoolPref: function(b,prefName) {
        try {
            return this._get_branch(b).getBoolPref(prefName);
        } catch(e) {
            Components.utils.reportError(e);
            return null;
        }
    },
    setBoolPref: function(b,prefName, value) {
        try {
            this._get_branch(b).setBoolPref(prefName, value);
        } catch(e) {
            Components.utils.reportError(e);
        }
    },
    getFilePref: function(b,prefName) {
        try {
            var store = im_Pref.getBoolPref("store-in-profile");
            if (store && !oldpref) {
                var ds = imns.Cc["@mozilla.org/file/directory_service;1"].getService(imns.Ci.nsIProperties);
                var profdir = ds.get("ProfD", imns.Ci.nsILocalFile);
                profdir.append("iMacros");
                switch (prefName) {
                case "defdownpath":
                    profdir.append("Downloads");
                    break;
                case "defdatapath":
                    profdir.append("Datasources");
                    break;
                case "deflogpath":
                    break;
                case "defsavepath":
                    profdir.append("Macros");
                    break;
                default:
                    return this._get_branch(b).getComplexValue(prefName, imns.Ci.nsILocalFile);
                }
                return profdir;
            } else {
                if (oldpref) return this._get_branch(oldpref).getCharPref(prefName);
                else return this._get_branch(oldpref).getComplexValue(prefName, imns.Ci.nsILocalFile);
            }
        } catch(e) {
            Components.utils.reportError(e);
            return null;
        }
    },
    setFilePref: function(b,prefName, value) {
        try {
            this._get_branch(b).setComplexValue(prefName, imns.Ci.nsILocalFile, value);
        } catch(e) {
            Components.utils.reportError(e);
        }
    },
    getStringPref: function(b,prefName) {
        try {
            return this._get_branch(b).getComplexValue(prefName, imns.Ci.nsISupportsString).data;
        } catch(e) {
            Components.utils.reportError(e);
            return null;
        }
    },
    setStringPref: function(b,prefName, value) {
        try {
            this._get_branch(b).setComplexValue(prefName, imns.Ci.nsISupportsString, value);
        } catch(e) {
            Components.utils.reportError(e);
        }
    },
    clearPref: function(b,prefName) {
        try {
            this._get_branch(b).clearUserPref(prefName);
        } catch(e) {
            Components.utils.reportError(e);
        }
    }
};

Initial URL


Initial Description


Initial Title
reading & writing firefox preferences (nsIPrefService)

Initial Tags


Initial Language
JavaScript