Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Noitidart/9288991 to your computer and use it in GitHub Desktop.
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.
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() {}
<?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>
@Noitidart
Copy link
Author

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:

// create and dispatch the event
var myEvent = new CustomEvent('peakAhBoo', {
    'detail': {
        'hazcheeseburger': true
    }
});
window.dispatchEvent(myEvent);
  • Resources: MDN - CustomEvent
  • NOTE this version is bugged and is not working, I commited it so I can post asking for help. When try to fire the event on content window it's not bubbling to gBrowser

Rev7

  • The bug was fixed, addEventListener has a 4th argument for wantsTrusted, if you want to accept from windows must set that true 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 is true must only be dispatching event from non-privileged html windows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment