Jason Champion
Jason Champion

Reputation: 2750

How Do I Write a Firefox Add-On That Turns Itself Off When in Private Mode?

I've written a working Firefox add-on. I would like this add-on to disable itself when the browser is in private mode. According to the docs at https://developer.mozilla.org/EN/docs/Supporting_per-window_private_browsing I've built this wrapper around my addon:

const {Cu} = require("chrome");
Cu.import("resource://gre/modules/PrivateBrowsingUtils.jsm");

// This plugin should not activate in a private browsing session.
if (!PrivateBrowsingUtils.isWindowPrivate(window)) {
  // Add-on code goes here. includes things like:
  var button = buttons.ActionButton(...);
  tabs.on('ready', ...);
  tabs.on('activate', ...);
}

Trouble is, I get an error, "Message: ReferenceError: window is not defined" in the line that checks "isWindowPrivate".

Is there some other way I should be accessing this property or an additional bit I need to include? This is an ActionButton add-on for Firefox 29+. Maybe the interface changed?

Upvotes: 0

Views: 205

Answers (3)

Wladimir Palant
Wladimir Palant

Reputation: 57681

There is a quirk to importing modules in the Add-on SDK, you must import the symbols explicitly:

var {PrivateBrowsingUtils} = Cu.import("resource://gre/modules/PrivateBrowsingUtils.jsm");

Other than that your code seems correct

Upvotes: 0

paa
paa

Reputation: 5054

Add-on SDK extensions by default opt-out of private browsing. That means you don't have to take any extra steps, the SDK simply will not allow your code to interfere with private browsing windows. And this includes ui components like ActionButton.

window is not defined because main.js runs in a windowless context. You can obtain it, if you want, but in this case you don't need it.

Upvotes: 2

Akhil
Akhil

Reputation: 1083

Determining whether or not the user is currently in private browsing mode is simple. Just check the value of the privateBrowsingEnabled attribute on the nsIPrivateBrowsingService service.

var pbs = Components.classes["@mozilla.org/privatebrowsing;1"]
                    .getService(Components.interfaces.nsIPrivateBrowsingService);
var inPrivateBrowsingMode = pbs.privateBrowsingEnabled;

if (!inPrivateBrowsingMode) {
  /* save private information */
}

You can find more coding samples related to Private browsing mode from here

Upvotes: 0

Related Questions