Created
January 29, 2015 18:08
-
-
Save Infocatcher/2e616b2958bbf5539820 to your computer and use it in GitHub Desktop.
Windows watcher example for Custom Buttons (just a simplified demo)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Windows watcher, based on code from | |
// https://github.com/Infocatcher/Custom_Buttons/tree/master/CB_Editor_Toggle_on_Top | |
const watcherId = "customButtonsWindowsWatcher_" + this.id; | |
var {Application, Components} = window; // Prevent garbage collection in Firefox 3.6 and older | |
var watcher = Application.storage.get(watcherId, null); | |
if(!watcher) { | |
watcher = { | |
REASON_STARTUP: 1, | |
REASON_SHUTDOWN: 2, | |
REASON_WINDOW_LOADED: 3, | |
REASON_WINDOW_CLOSED: 4, | |
get obs() { | |
delete this.obs; | |
return this.obs = Components.classes["@mozilla.org/observer-service;1"] | |
.getService(Components.interfaces.nsIObserverService); | |
}, | |
get ww() { | |
delete this.ww; | |
return this.ww = Components.classes["@mozilla.org/embedcomp/window-watcher;1"] | |
.getService(Components.interfaces.nsIWindowWatcher); | |
}, | |
get wm() { | |
delete this.wm; | |
return this.wm = Components.classes["@mozilla.org/appshell/window-mediator;1"] | |
.getService(Components.interfaces.nsIWindowMediator); | |
}, | |
init: function(reason) { | |
this.obs.addObserver(this, "quit-application-granted", false); | |
var ws = this.wm.getEnumerator(null); | |
while(ws.hasMoreElements()) | |
this.initWindow(ws.getNext(), reason); | |
this.ww.registerNotification(this); | |
}, | |
destroy: function(reason) { | |
this.obs.removeObserver(this, "quit-application-granted"); | |
var ws = this.wm.getEnumerator(null); | |
while(ws.hasMoreElements()) | |
this.destroyWindow(ws.getNext(), reason); | |
this.ww.unregisterNotification(this); | |
}, | |
initWindow: function(window, reason) { | |
// Do something with window | |
window.addEventListener("keydown", this, true); | |
}, | |
destroyWindow: function(window, reason) { | |
if(reason == this.REASON_WINDOW_CLOSED) | |
window.removeEventListener("load", this, false); // Window can be closed before "load" event | |
// Cleanup | |
window.removeEventListener("keydown", this, true); | |
}, | |
observe: function(subject, topic, data) { | |
if(topic == "quit-application-granted") | |
this.destroy(); | |
else if(topic == "domwindowopened") | |
subject.addEventListener("load", this, false); | |
else if(topic == "domwindowclosed") | |
this.destroyWindow(subject, this.REASON_WINDOW_CLOSED); | |
}, | |
handleEvent: function(e) { | |
switch(e.type) { | |
case "load": this.loadHandler(e); break; | |
case "keydown": this.keyDownHandler(e); | |
} | |
}, | |
loadHandler: function(e) { | |
var window = e.currentTarget; | |
window.removeEventListener("load", this, false); | |
this.initWindow(window, this.REASON_WINDOW_LOADED); | |
}, | |
keyDownHandler: function(e) { | |
Services.console.logStringMessage("[watcher] " + e.type + " " + e.keyCode); | |
} | |
}; | |
Application.storage.set(watcherId, watcher); | |
watcher.init(watcher.REASON_STARTUP); | |
} | |
function destructor(reason) { | |
if(reason == "update" || reason == "delete") { | |
watcher.destroy(watcher.REASON_SHUTDOWN); | |
Application.storage.set(watcherId, null); | |
} | |
} | |
if( | |
typeof addDestructor == "function" // Custom Buttons 0.0.5.6pre4+ | |
&& addDestructor != ("addDestructor" in window && window.addDestructor) | |
) | |
addDestructor(destructor, this); | |
else | |
this.onDestroy = destructor; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment