Confirmed users
657
edits
(Document the Private Browsing service) |
(Initial write-up of the "Private Browsing notifications" section) |
||
| Line 29: | Line 29: | ||
// exit the Private Browsing mode | // exit the Private Browsing mode | ||
pbs.privateBrowsing = false; | pbs.privateBrowsing = false; | ||
===Private Browsing notifications=== | |||
Extensions can be notified of private browsing status changes, i.e., when the user enters or leaves the private browsing mode. This is done via standard XPCOM observer service mechanisms. The notification's name is "browser:private-browsing". The data parameter can be either "enter" (indicating that the private browsing mode has been initiated) or "exit" (indicating that the private browsing mode has been terminated). | |||
You can use the following code sample in order to create a listener for the private browsing mode changes notification. | |||
function PrivateBrowsingListener() { | |||
this.init(); | |||
} | |||
PrivateBrowsingListener.prototype = { | |||
_os: null, | |||
_inPrivateBrowsing: false, // whether we are in private browsing mode | |||
_inited: false, | |||
_watcher: null, // the watcher object | |||
QueryInterface : function (iid) { | |||
if (iid.equals(Components.interfaces.nsISupports) || | |||
iid.equals(Components.interfaces.nsIObserver)) | |||
return this; | |||
throw Components.results.NS_ERROR_NO_INTERFACE; | |||
}, | |||
init : function () { | |||
if (!this._inited) { | |||
this._inited = true; | |||
this._os = Components.classes["@mozilla.org/observer-service;1"] | |||
.getService(Components.interfaces.nsIObserverService); | |||
this._os.addObserver(this, "browser:private-browsing", false); | |||
this._os.addObserver(this, "quit-application", false); | |||
} | |||
}, | |||
observe : function (aSubject, aTopic, aData) { | |||
if (aTopic == "browser:private-browsing") { | |||
if (aData == "enter") { | |||
this._inPrivateBrowsing = true; | |||
if (this.watcher && | |||
"onEnterPrivateBrowsing" in this._watcher) { | |||
this.watcher.onEnterPrivateBrowsing(); | |||
} | |||
} else if (aData == "exit") { | |||
this._inPrivateBrowsing = false; | |||
if (this.watcher && | |||
"onExitPrivateBrowsing" in this._watcher) { | |||
this.watcher.onExitPrivateBrowsing(); | |||
} | |||
} | |||
} else if (aTopic == "quit-application") { | |||
this._os.removeObserver(this, "quit-application"); | |||
this._os.removeObserver(this, "browser:private-browsing"); | |||
} | |||
}, | |||
get inPrivateBrowsing() { | |||
return this._inPrivateBrowsing; | |||
}, | |||
get watcher() { | |||
return this._watcher; | |||
}, | |||
set watcher(val) { | |||
this._watcher = val; | |||
} | |||
}; | |||
var listener = PrivateBrowsingListener(); | |||
if (listener.inPrivateBrowsing) { | |||
// we are in the private mode! | |||
} else { | |||
// we are not in the private mode! | |||
} | |||
listener.watcher = { | |||
onEnterPrivateBrowsing : function() { | |||
// we have just entered the private browsing mode! | |||
}, | |||
onExitPrivateBrowsing : function() { | |||
// we have just left the private browsing mode! | |||
} | |||
}; | |||
==Code Samples== | ==Code Samples== | ||