Skip to content

Instantly share code, notes, and snippets.

@jaredhirsch
Created November 30, 2016 02:11
Show Gist options
  • Save jaredhirsch/5699a18f749fecb0a018c59c07f4c521 to your computer and use it in GitHub Desktop.
Save jaredhirsch/5699a18f749fecb0a018c59c07f4c521 to your computer and use it in GitHub Desktop.
Firefox trick: use the hidden window to access DOM APIs without a window reference
// You may sometimes be doing something in non-UI add-on code,
// but want access to nice DOM APIs, like navigator.sendBeacon().
// Use the hidden window, which is accessible via the AppShell service:
// If you're in an SDK addon, do this to get a Cu reference:
// const { Cu } = require('chrome');
Cu.import('resource://gre/modules/Services.jsm');
// this is your window:
const win = Services.appShell.hiddenDOMWindow;
// you can access all the APIs directly:
Services.appShell.hiddenDOMWindow.navigator.sendBeacon(url, data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment