2010-02-16 16 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