首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在firefox中设置启动时的首选项

在firefox中设置启动时的首选项
EN

Stack Overflow用户
提问于 2010-02-17 03:57:34
回答 1查看 1.5K关注 0票数 2

提前感谢所有人-

在启动时加载任何窗口之前,我需要加载一个首选项。下面是我一直在使用的一些/component代码。当调用SetPreference方法时,它似乎失败了(后面也没有执行任何东西)--我认为是因为它所需的资源在execution...or时不可用,所以我做错了什么。对这段代码或其他在启动时设置首选项的方法有什么建议吗?

再次感谢,

相同的

由于某些原因,SO的代码格式不能正常工作-这里还有一个指向代码的链接- http://samingrassia.com/_FILES/startup.js

代码语言:javascript
复制
Components.utils.import('resource://gre/modules/XPCOMUtils.jsm');

const Cc = Components.classes;
const Ci = Components.interfaces;

const ObserverService = Cc['@mozilla.org/observer-service;1'].getService(Ci.nsIObserverService);

function MyStartupService() {};

MyStartupService.prototype = {
  observe : function(aSubject, aTopic, aData) {
    switch (aTopic) {
      case 'xpcom-startup':
        this.SetPreference("my.extension.is_running", "false");
        break;
      case 'app-startup':
        this.SetPreference("my.extension.is_running", "false");
        ObserverService.addObserver(this, 'final-ui-startup', false);
        break;
      case 'final-ui-startup':

        //make sure is_running is set to false
        this.SetPreference("my.extension.is_running", "false");

        ObserverService.removeObserver(this, 'final-ui-startup');
        const WindowWatcher = Cc['@mozilla.org/embedcomp/window-watcher;1'].getService(Ci.nsIWindowWatcher);
        WindowWatcher.registerNotification(this);
        break;
      case 'domwindowopened':
        this.initWindow(aSubject);
        break;
    }
  },
  SetPreference : function(Token, Value) {
    var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
    var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
    str.data = Value;
    prefs.setComplexValue(Token, Components.interfaces.nsISupportsString, str);

    //save preferences
    var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
    prefService.savePrefFile(null);  
  },
  initWindow : function(aWindow) {
    if (aWindow != '[object ChromeWindow]') return;
    aWindow.addEventListener('load', function() {
      aWindow.removeEventListener('load', arguments.callee, false);
      aWindow.document.title = 'domwindowopened!';
      // for browser windows
      var root = aWindow.document.documentElement;
      root.setAttribute('title', aWindow.document.title);
      root.setAttribute('titlemodifier', aWindow.document.title);
    }, false);
  },
  classDescription : 'My Startup Service',
  contractID : '@mystartupservice.com/startup;1',
  classID : Components.ID('{770825e7-b39c-4654-94bc-008e5d6d57b7}'),
  QueryInterface : XPCOMUtils.generateQI([Ci.nsIObserver]),
  _xpcom_categories : [{ category : 'app-startup', service : true }]
};

function NSGetModule(aCompMgr, aFileSpec) {
  return XPCOMUtils.generateModule([MyStartupService]);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-02-17 06:14:34

回答你真正的问题,就是

我有在每次加载窗口时加载的代码,我需要确保每次firefox启动时只执行一次。

..you应该只使用module,在您希望执行一次的加载处理程序中,检查从模块导出(即“居住在”模块中)的对象上的标志,然后在运行所需代码后设置该标志。

由于该模块是在所有窗口之间共享的,因此该标志将保持设置,直到您关闭Firefox。

对于中间问题,我建议将observe()中的代码包装在try { ... } catch(e) {dump(e)}中(您需要set a pref and run Firefox in a special way才能看到输出),并检查返回的错误。

我猜xpcom-startup和app-startup还为时过早(我认为你需要一个配置文件),请注意,无论如何你都不需要注册来获得xpcom-startup通知。您可能希望注册profile-after-change

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2275882

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档