diff options
author | Ehsan Akhgari <ehsan@mozilla.com> | 2010-05-30 20:46:45 -0400 |
---|---|---|
committer | Ehsan Akhgari <ehsan@mozilla.com> | 2010-05-30 20:46:45 -0400 |
commit | ef369e0ba4f4c8c1f0e3eaad49ff99050e798542 (patch) | |
tree | 66b64d5e5e9fae2436675fa9c64f9bb027fa9665 /lib/persistent-page-mod.js | |
parent | 7d892a30966ce621e592ad861e3e76ddf78fd05b (diff) | |
download | bugzilla-triage-ef369e0ba4f4c8c1f0e3eaad49ff99050e798542.tar.gz |
Switch to using the new equivalent of the pageMods API
Diffstat (limited to 'lib/persistent-page-mod.js')
-rw-r--r-- | lib/persistent-page-mod.js | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/lib/persistent-page-mod.js b/lib/persistent-page-mod.js new file mode 100644 index 0000000..5c00a8f --- /dev/null +++ b/lib/persistent-page-mod.js @@ -0,0 +1,56 @@ +var timer = require("timer"); + +function PersistentPageMod(window, callback) { + memory.track(this); + this.window = window; + this.callback = callback; + this.window.addEventListener("unload", this, false); + this.window.addEventListener("DOMSubtreeModified", this, false); + this.doMod(); + require("unload-2").ensure(this); +} + +PersistentPageMod.prototype = { + REPLACE_DELAY: 100, + doMod: function doMod() { + try { + this.callback.call(undefined, this.window); + } catch (e) { + console.exception(e); + } + this.timerID = null; + }, + handleEvent: function handleEvent(event) { + switch (event.type) { + case "unload": + if (event.target == this.window.document) + this.unload(); + break; + case "DOMSubtreeModified": + if (this.timerID == null) { + // Wait a bit to do the replacing. Otherwise, we just get called + // tons of times in a tiny period and end up hanging the browser + // for a while. + var self = this; + this.timerID = timer.setTimeout(function() self.doMod(), + this.REPLACE_DELAY); + } + break; + } + }, + unload: function unload() { + if (this.timerID != null) { + timer.clearTimeout(this.timerID); + this.timerID = null; + } + this.window.removeEventListener("DOMSubtreeModified", this, false); + this.window.removeEventListener("unload", this, false); + } +}; + +require("errors").catchAndLogProps(PersistentPageMod.prototype, + "handleEvent"); + +var register = exports.register = function register(window, callback) { + new PersistentPageMod(window, callback); +}; |