reading & writing firefox preferences (nsIPrefService)


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /*e.g.
  2.  
  3. Pref.setIntPref('browser.link','open_newwindow',9);
  4. Pref.getIntPref('browser.link','open_newwindow')
  5. */
  6. var Pref = {
  7. get psvc() {
  8. return Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
  9. },
  10. _get_branch: function(branch) {
  11. return this.psvc.getBranch(branch+".");
  12. },
  13. getIntPref: function(b,prefName) {
  14. try {
  15. return this._get_branch(b).getIntPref(prefName);
  16. } catch(e) {
  17. Components.utils.reportError(e);
  18. return null;
  19. }
  20. },
  21. setIntPref: function(b,prefName, value) {
  22. try {
  23. this._get_branch(b).setIntPref(prefName, value);
  24. } catch(e) {
  25. Components.utils.reportError(e);
  26. }
  27. },
  28. getCharPref: function(b,prefName) {
  29. try {
  30. return this._get_branch(b).getCharPref(prefName);
  31. } catch(e) {
  32. Components.utils.reportError(e);
  33. return null;
  34. }
  35. },
  36. setCharPref: function(b,prefName, value) {
  37. try {
  38. this._get_branch(b).setCharPref(prefName, value);
  39. } catch(e) {
  40. Components.utils.reportError(e);
  41. }
  42. },
  43. getBoolPref: function(b,prefName) {
  44. try {
  45. return this._get_branch(b).getBoolPref(prefName);
  46. } catch(e) {
  47. Components.utils.reportError(e);
  48. return null;
  49. }
  50. },
  51. setBoolPref: function(b,prefName, value) {
  52. try {
  53. this._get_branch(b).setBoolPref(prefName, value);
  54. } catch(e) {
  55. Components.utils.reportError(e);
  56. }
  57. },
  58. getFilePref: function(b,prefName) {
  59. try {
  60. var store = im_Pref.getBoolPref("store-in-profile");
  61. if (store && !oldpref) {
  62. var ds = imns.Cc["@mozilla.org/file/directory_service;1"].getService(imns.Ci.nsIProperties);
  63. var profdir = ds.get("ProfD", imns.Ci.nsILocalFile);
  64. profdir.append("iMacros");
  65. switch (prefName) {
  66. case "defdownpath":
  67. profdir.append("Downloads");
  68. break;
  69. case "defdatapath":
  70. profdir.append("Datasources");
  71. break;
  72. case "deflogpath":
  73. break;
  74. case "defsavepath":
  75. profdir.append("Macros");
  76. break;
  77. default:
  78. return this._get_branch(b).getComplexValue(prefName, imns.Ci.nsILocalFile);
  79. }
  80. return profdir;
  81. } else {
  82. if (oldpref) return this._get_branch(oldpref).getCharPref(prefName);
  83. else return this._get_branch(oldpref).getComplexValue(prefName, imns.Ci.nsILocalFile);
  84. }
  85. } catch(e) {
  86. Components.utils.reportError(e);
  87. return null;
  88. }
  89. },
  90. setFilePref: function(b,prefName, value) {
  91. try {
  92. this._get_branch(b).setComplexValue(prefName, imns.Ci.nsILocalFile, value);
  93. } catch(e) {
  94. Components.utils.reportError(e);
  95. }
  96. },
  97. getStringPref: function(b,prefName) {
  98. try {
  99. return this._get_branch(b).getComplexValue(prefName, imns.Ci.nsISupportsString).data;
  100. } catch(e) {
  101. Components.utils.reportError(e);
  102. return null;
  103. }
  104. },
  105. setStringPref: function(b,prefName, value) {
  106. try {
  107. this._get_branch(b).setComplexValue(prefName, imns.Ci.nsISupportsString, value);
  108. } catch(e) {
  109. Components.utils.reportError(e);
  110. }
  111. },
  112. clearPref: function(b,prefName) {
  113. try {
  114. this._get_branch(b).clearUserPref(prefName);
  115. } catch(e) {
  116. Components.utils.reportError(e);
  117. }
  118. }
  119. };

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.