Code: Select all
var observice=Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
var anti_multi_observer_topic="{insert unique observer topic string}";
if(!observice.enumerateObservers(anti_multi_observer_topic).hasMoreElements()){
observice.addObserver({},anti_multi_observer_topic,false);//this observer does nothing, it exists for if check above next time script runs
//insert code to run only first time
}
Replace //insert code to run only first time with code to run only first time.
Use when you need to run something once in script that might run multiple times.
Example: <script> in chrome://browser/content/browser.xul overlay will run every time new browser window is opened.
Example: you need to add only one observer because observers are global for all browser windows in same process.
Links: