Forked from Noitidart/_ff-addon-template-BootstrapWatchHostEventListener.xpi
Last active
August 29, 2015 13:56
-
-
Save Noitidart/9288991 to your computer and use it in GitHub Desktop.
_ff-addon-template-BootstrapAddEventListenerCustomEvent.xpi - This shows how to listen to and respond to custom events dispatched by web pages.
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
const {interfaces: Ci, utils: Cu} = Components; | |
Cu.import('resource://gre/modules/Services.jsm'); | |
function respondToCustomEvent_peakAhBoo(event) { | |
Services.appShell.hiddenDOMWindow.console.log('respondToCustomEvent_peakAhBoo', event); | |
Services.prompt.alert(null, "Bootstrap Custom Event Listeners", 'peakAhBoo fired!'); | |
} | |
/*start - windowlistener*/ | |
var windowListener = { | |
//DO NOT EDIT HERE | |
onOpenWindow: function (aXULWindow) { | |
// Wait for the window to finish loading | |
let aDOMWindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow); | |
aDOMWindow.addEventListener("load", function () { | |
aDOMWindow.removeEventListener("load", arguments.callee, false); | |
windowListener.loadIntoWindow(aDOMWindow, aXULWindow); | |
}, false); | |
}, | |
onCloseWindow: function (aXULWindow) {}, | |
onWindowTitleChange: function (aXULWindow, aNewTitle) {}, | |
register: function () { | |
// Load into any existing windows | |
let XULWindows = Services.wm.getXULWindowEnumerator(null); | |
while (XULWindows.hasMoreElements()) { | |
let aXULWindow = XULWindows.getNext(); | |
let aDOMWindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow); | |
windowListener.loadIntoWindow(aDOMWindow, aXULWindow); | |
} | |
// Listen to new windows | |
Services.wm.addListener(windowListener); | |
}, | |
unregister: function () { | |
// Unload from any existing windows | |
let XULWindows = Services.wm.getXULWindowEnumerator(null); | |
while (XULWindows.hasMoreElements()) { | |
let aXULWindow = XULWindows.getNext(); | |
let aDOMWindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow); | |
windowListener.unloadFromWindow(aDOMWindow, aXULWindow); | |
} | |
//Stop listening so future added windows dont get this attached | |
Services.wm.removeListener(windowListener); | |
}, | |
//END - DO NOT EDIT HERE | |
loadIntoWindow: function (aDOMWindow, aXULWindow) { | |
if (!aDOMWindow) { | |
return; | |
} | |
if (aDOMWindow.gBrowser) { | |
aDOMWindow.gBrowser.addEventListener('peakAhBoo', respondToCustomEvent_peakAhBoo, true, true); //4th argument of wantsTrusted must be true: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget.addEventListener?redirectlocale=en-US&redirectslug=DOM%2FEventTarget.addEventListener | |
} else { | |
aDOMWindow.addEventListener('peakAhBoo', respondToCustomEvent_peakAhBoo, true); //removeEventListener does not have 4th argument of wants trusted | |
} | |
}, | |
unloadFromWindow: function (aDOMWindow, aXULWindow) { | |
if (!aDOMWindow) { | |
return; | |
} | |
if (aDOMWindow.gBrowser) { | |
aDOMWindow.gBrowser.removeEventListener('peakAhBoo', respondToCustomEvent_peakAhBoo, true); | |
} else { | |
aDOMWindow.removeEventListener('peakAhBoo', respondToCustomEvent_peakAhBoo, true); | |
} | |
} | |
}; | |
/*end - windowlistener*/ | |
function startup(aData, aReason) { | |
windowListener.register(); | |
} | |
function shutdown(aData, aReason) { | |
if (aReason == APP_SHUTDOWN) return; | |
windowListener.unregister(); | |
} | |
function install() {} | |
function uninstall() {} |
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
<?xml version="1.0" encoding="utf-8"?> | |
<!-- This Source Code Form is subject to the terms of the Mozilla Public | |
- License, v. 2.0. If a copy of the MPL was not distributed with this | |
- file, You can obtain one at http://mozilla.org/MPL/2.0/. --> | |
<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:em="http://www.mozilla.org/2004/em-rdf#"> | |
<Description about="urn:mozilla:install-manifest"> | |
<em:id>Bootstrap-Custom-Event-Listeners@jetpack</em:id> | |
<em:version>initial</em:version> | |
<em:type>2</em:type> | |
<em:bootstrap>true</em:bootstrap> | |
<em:unpack>false</em:unpack> | |
<!-- Firefox --> | |
<em:targetApplication> | |
<Description> | |
<em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id> | |
<em:minVersion>4.0</em:minVersion> | |
<em:maxVersion>27.0.1</em:maxVersion> | |
</Description> | |
</em:targetApplication> | |
<!-- Front End MetaData --> | |
<em:name>Bootstrap Custom Event Listeners</em:name> | |
<em:description>This shows how to listen to and respond to custom events dispatched by web pages.</em:description> | |
<em:creator>Noitidart</em:creator> | |
</Description> | |
</RDF> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
README
Rev1 - Rev4
Ignore these revisions, they are apart of the fork and not specifically related to this Gist.
Rev5
Updated description of Gist
Rev6
First revision which is related specifically to this Gist of listening to custom events dispatched by web pages to window.
How to see demo. Install addon. Then go to Scratchpad in either Browser or Content environment and run this code to see the addon react to it:
Rev7
wantsTrusted
, if you want to accept from windows must set thattrue
NOTE However if you set it to true then if the event is dispatched from privelaged chrome context the event listener will not catch it, therefore if 4th argument istrue
must only be dispatching event from non-privileged html windows.