提前感谢所有人-
在启动时加载任何窗口之前,我需要加载一个首选项。下面是我一直在使用的一些/component代码。当调用SetPreference方法时,它似乎失败了(后面也没有执行任何东西)--我认为是因为它所需的资源在execution...or时不可用,所以我做错了什么。对这段代码或其他在启动时设置首选项的方法有什么建议吗?
再次感谢,
相同的
由于某些原因,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]);
}发布于 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。
https://stackoverflow.com/questions/2275882
复制相似问题