2010-02-16 32 views
2

感谢大家提前 -设置在启动时的偏好在Firefox

我需要在启动时加载的所有窗口前加载的偏好。以下是我一直在使用的一些/组件代码。 SetPreference方法在调用时似乎失败(任何一个都不会执行后缀) - 我假设是因为它需要的资源在执行时不可用......或者我做错了什么。使用此代码的任何建议或其他方法在启动时设置首选项?再次

感谢,

山姆

出于某种原因,代码格式化SO不能正常工作 - 在这里是对代码的链接,以及 - http://samingrassia.com/_FILES/startup.js

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]); 
} 
+0

修复了您的代码格式并删除了“由于某些原因,SO的代码格式无法正常工作..”。 :) – 2010-02-16 20:00:31

+1

为什么在任何Windows启动之前需要设置首选项? – djc 2010-02-16 20:05:01

+0

基本上,因为我有加载在每个窗口加载的代码,我需要确保每次firefox启动时只执行一次。当firefox在没有页面隐藏事件等情况下终止时,我的is_running首选项保持其值为'true',并且在下次启动firefox时,我的扩展无法正常工作。有什么建议么? – 2010-02-16 20:13:05

回答

4

要回答你的真正的问题,这是

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

..你应该只使用一个module,在您要执行一次,检查的国旗(即“生活”)的模块,导出的对象的负载处理程序,然后运行你的代码后,需要设置标志。

由于模块在所有窗口之间共享,因此在关闭Firefox之前该标志将保持设置状态。

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

我想xpcom启动和应用启动为时过早,不喜欢首选项(我认为你需要一个配置文件),请注意,你不注册获得xpcom启动通知。您可能想要注册profile-after-change

+0

muchos gracias! – 2010-02-16 22:41:21