How to add observer for nsIBrowserSearchService - javascript

I'm having trouble adding an observer to watch for changes in firefox's search engines. I read the nsIBrowserSearchService page on the Mozilla Developer Site which suggests to use the init() method of the Services.search object.
void init([optional] in nsIBrowserSearchInitObserver observer);
I tried that and I managed to get it to execute the function once on start up but it never calls it again when I add or remove or reorder the search engines. So I'm doing something wrong.
I have experience with observers but only with using general preferences and I generally use add them and remove them using the code below. I'm not sure how to do it with the nsIBrowserSearchService . I would like to observe the nsIBrowserSearchService in the same or a similar way but I'm not sure what I would put for
branch = Services.prefs.getBranch("preferenceNameHere");
I'm not observing Services.prefs but Services.search I assume, and that has no getBranch method as far as I can tell.
This is how I normally add and remove the observer in Chrome.js
const {Ci, Cu} = require("chrome");
const {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
branch = Services.prefs.getBranch("preferenceNameHere");
branch.addObserver("", observe, false);
function observe(subject, topic, data) {
// instanceof actually also "casts" subject
if (!(subject instanceof Ci.nsIPrefBranch)) {
return;
}
//do stuff here
};
exports.onUnload = function(reason) {
// Need to remove our observer again! This isn't automatic and will leak
// otherwise.
branch.removeObserver("", observe);
if(reason == "disable" || reason == "uninstall"){
//restore changes made by addon
}
};
Can anyone advise me on how to do this so I can observe for changes and ensure that I remove the observer properly with the search object. Thanks

What you are trying above is trying to use pref observer on non-prefs, this is not possible. You have to use the regular observer service like this.
This notifies you when the user changes the engine.
Cu.import('resource://gre/modules/Services.jsm');
var observers = {
'browser-search-engine-modified': {
aTopic: 'browser-search-engine-modified',
observe: function (aSubject, aTopic, aData) {
if (aData == 'engine-current') {
console.log('current engine was changed!');
//console.log('aSubject on change:', aSubject.name, 'same as Services.search.currentEngine.name:', Services.search.currentEngine.name); //aSubject is the engine
//console.log('aTopic on change:', aTopic); //aTopic is obviously `browser-search-engine-modified`
}
},
reg: function () {
Services.obs.addObserver(observers[this.aTopic], this.aTopic, false);
},
unreg: function () {
Services.obs.removeObserver(observers[this.aTopic], this.aTopic);
}
}
};
To start listening do this:
for (var o in observers) {
observers[o].reg();
}
To stop listening do this:
for (var o in observers) {
observers[o].unreg();
}
I'm not sure what happens when user adds a new engine but doenst select it. Or if he removes a engine. Please let me know what those messages are when user does that.

Related

Getting Thunderbird's email editor object from a restartless (bootstrapped) addon

I'm creating a restartless (bootstrapped) addon for Thunderbird to replicate the functionality of an overlay-based addon I made years ago. The desire is to manipulate some of the text in the new email's body. In the old overlay version, it was pretty straightforward: I could just use the gMsgCompose object. How do I get the gMsgCompose object for a compose window in a restartless addon?
Here's my code right now. For a while I thought that edElem was nearly what I wanted, which is why I'm test-dumping edElem.editortype (which should be either "textmail" or "htmlmail" when I really do have the right object).
'use strict';
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
Cu.import("resource://gre/modules/Services.jsm");
function startup(data, reason)
{
Services.wm.addListener(MyAddon.windowListener);
}
function shutdown(data, reason)
{
Services.wm.removeListener(MyAddon.windowListener);
}
function install(aData, aReason) { }
function uninstall(aData, aReason) { }
if(!MyAddon) var MyAddon = {};
MyAddon.windowListener = {
onOpenWindow: function(aXULWindow) {
let aDOMWindow = aXULWindow
.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindow);
if (!aDOMWindow) return;
aDOMWindow.addEventListener("load", function _cl_load(event){
aDOMWindow.removeEventListener("load", _cl_load, false);
MyAddon.handleOpenWindow(aDOMWindow, event.target);
}, false);
}
}; // MyAddon.windowListener
MyAddon.handleOpenWindow = function(aDOMWindow, DOMdocument) {
if(DOMdocument.documentURI != "chrome://messenger/content/messengercompose/messengercompose.xul")
return;
let edWindow = DOMdocument.documentElement;
// Just to be sure
if(edWindow.id != 'msgcomposeWindow')
return;
let edElem = DOMdocument.getElementById("content-frame");
/* I thought edElem was the element I want, but the following
* returns an empty result, which I take to mean the element isn't
* fully loaded yet.
*/
dump("XXX handleOpenWindow: edElem.editortype="+edElem.editortype+"\n");
/* Adding a listener to this element doesn't get the behaviour
* I want either. I've tried hooking several events
* in the following fashion, and the event handler code doesn't
* execute for any of them, so maybe edElem isn't what I want.
*/
[ "load",
"unload",
"compose-window-init",
"compose-window-close",
"compose-fields-ready",
"compose-send-message",
].forEach(function(eventName) {
edElem.addEventListener(eventName, function _eE_FIXME(event){
dump("XXX "+eventName+": edElem.editortype="+edElem.editortype+"\n");
}, false);
});
/* So, how do I get control of the editor,
* so I can read and play with its content?
*/
}; // MyAddon.handleOpenWindow
The only question I found on stackoverflow that's close to what I want is Get sender and recipients in Thunderbird extension upon sending message. I've tried what they do there, but all it seems to do is give me an alternate way of getting the compose window as a whole, which I can already get.
I've made an ugly stub with a timer in the onOpenWindow:
domWindow.setTimeout( function() { uploadHook( domWindow ); }, 500 );
So then (in uploadHook) I can get editor with:
var editorNode = domWindow.document.getElementById("content-frame");
// GetCurrentEditor() equivalent:
nsIEditor = editorNode.getEditor(editorNode.contentWindow)
But looking for a better solution.

firefox addon install.rdf pass data to server on update

Is there any chance to pass some data to my server through install.rdf when my Firefox add-on check server for update?
Example:
...
<em:updateURL>http://www.site.com/update.php?var=myData</em:updateURL>
...
where "myData" is saved in options.xul or in another place like simple-storage.
Yes, but it is quite nasty. The AddonManager will replace a bunch of predefined and dynamic properties in the URL:
Register a new component implementing nsIPropertyBag2 (or use an existing implementation, such as ["#mozilla.org/hash-property-bag;1"]).
Register your component in the nsICategoryManager under the "extension-update-params" category.
Since you mentioned simple-storage: restartless add-ons must also unregister their stuff when being unloaded.
There is a unit test demonstrating how this stuff works. You of course need to adapt it a bit (if alone for require("chrome").
I found one "simple solution" but I dont know if that is also good practice ...
var origLink = "http://www.site.net/update.php?var=myData";
var newsLink = "http://www.site.net/update.php?var=" + simplePref.prefs.myData;
const {Cc,Ci,Cu} = require("chrome");
var observer = {
QueryInterface: function(iid) {
if (iid.equals(Ci.nsIObserver) || iid.equals(Ci.nsISupports)) return this;
},
observe: function(subject, topic, data){
if (topic == "http-on-modify-request"){
var channel = subject.QueryInterface(Ci.nsIChannel);
if (channel.originalURI.spec == origLink) {
channel.originalURI.spec = newsLink;
}
}
}
};
var ObsService = Cc["#mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
ObsService.addObserver(observer, "http-on-modify-request", false);

How to stop and restart collection observers in Meteor?

I want to be able to stop and restart observers on my collections in Meteor.
Imagine I have the following observer:
// Imagine some collection of Blog posts "Posts"
Posts.find().observe({
changed: notifySubscribedUsers
});
// function notifySubscribedUsers() { ... }
// is some function that will email everyone saying some post was updated
Now imagine I want to update lots of Posts, but I dont want the observers to be called. How can I get access to the observers, stop/pause them and then later restart them (after the db job is finished) ?
TIA
The observer returns a handle:
var handle = Posts.find().observe({
changed: notifySubscribedUsers
});
Then you can stop it with:
handle.stop()
It's not possible to 'pause' it in the conventional sense, if you want to pause it you could just ignore the data it gives you.
To do this in a neat wrapped up method you could do something like:
var handle;
var start = function() {
if(handle) handle.stop();
var handle = Posts.find().observe({
changed: notifySubscribedUsers
});
}
var stop = function() { if(handle) handle.stop }
Or to put it on a collection:
// posts.js collection file
Posts.startObservers = function startObservers() {
Posts.observer = Posts.find().observe({
change: notifySubscribedUsers // or some other function
});
};
Posts.stopObservers = function stopObservers() {
if(Posts.observer) {
Posts.observer.stop(); // Call the stop
}
};
// Trigger Somewhere else in the code
Posts.stopObservers();
MyTool.doWorkOnPosts(); // Some contrived work on the Posts collection
Posts.startObservers();

Navigating in a WinJS app always throws an exception

Every time I try to perform a navigation in the ready function of a page, the application crashes.
Specifically, it fails at the WinJS.Navigation.navigate("/pages/login/login.html", {}); line below:
// This function is called whenever a user navigates to this page. It
// populates the page elements with the app's data.
ready: function (element, options) {
var listView = element.querySelector(".groupeditemslist").winControl;
listView.groupHeaderTemplate = element.querySelector(".headertemplate");
listView.itemTemplate = element.querySelector(".itemtemplate");
listView.oniteminvoked = this._itemInvoked.bind(this);
// Set up a keyboard shortcut (ctrl + alt + g) to navigate to the
// current group when not in snapped mode.
listView.addEventListener("keydown", function (e) {
if (appView.value !== appViewState.snapped && e.ctrlKey && e.keyCode === WinJS.Utilities.Key.g && e.altKey) {
var data = listView.itemDataSource.list.getAt(listView.currentItem.index);
this.navigateToGroup(data.group.key);
e.preventDefault();
e.stopImmediatePropagation();
}
}.bind(this), true);
this._initializeLayout(listView, appView.value);
listView.element.focus();
initialize();
}
function initialize() {
// Check if user is logged in
if (is_logged_in !== true) {
WinJS.Navigation.navigate("/pages/login/login.html", {});
}
else {
// TODO: Replace the data with your real data.
// You can add data from asynchronous sources whenever it becomes available.
generateSampleData().forEach(function (item) {
list.push(item);
});
}
}
Anyone know why this happens?
There are a couple routes you could take here:
Catch the unhandled exception and ignore it
Structure your code to avoid setting up the error condition
To ignore the error you can setup a WinJS.Application.onerror handler that can deal with unhandled exceptions. Here's a forum post that guides you in this solution: http://social.msdn.microsoft.com/Forums/en-US/winappswithhtml5/thread/686188b3-852d-45d5-a376-13115dbc889d
In general, I'd say you're better off avoiding the exception all together. To that end - What's happening here is that only one navigation event (promise) can occur at a time. The navigation promise used to navigate to groupedItems is still running when you're inside of the ready function. When you call initialize, which then calls WinJS.Navigation.navigate("/pages/login/login.html", {}); it sees this and tries to first cancel the currently running navigation promise which results in the exception you're seeing.
Instead, you can use the window.setImmediate function to setup your call to initialize() to run after the current script block exits. To do this, replace your call to initialize() with:
window.setImmediate(this.initialize.bind(this));
If your running your code on the RTM version after coming from the Release Preview this should sort your problem.
function initialize() {
// Check if user is logged in
if (is_logged_in !== true) {
WinJS.Navigation.navigate("/pages/login/login.html", {});
}
else {
// TODO: Replace the data with your real data.
// You can add data from asynchronous sources whenever it becomes available.
generateSampleData().forEach(function (item) {
list.push(item);
});
}
}
var markSupportedForProcessing = WinJS.Utilities.markSupportedForProcessing;
var requireSupportedForProcessing = WinJS.Utilities.requireSupportedForProcessing;
markSupportedForProcessing(initialize);
requireSupportedForProcessing(initialize);
You should probably take a look at the migration docs which details what the above is actually for and why: http://www.microsoft.com/en-us/download/details.aspx?id=30706

Preventing Browser Location Change in xulrunner

I've been reading and hacking around with https://developer.mozilla.org/en/XUL_School/Intercepting_Page_Loads but can seem to do what I need.
I'm working on Chromeless, trying to prevent the main xulbrowser element from ever being navigated away from, e.g., links should not work, neither should window.location.href="http://www.example.com/".
I'm assuming I can do this via browser.webProgress.addProgressListener and then listen to onProgressChange but I can't figure out how to differentiate between a resource request and the browser changing locations (it seems that onLocationChange is too late as the document is already being unloaded).
browser.webProgress.addProgressListener({
onLocationChange: function(){},
onStatusChange: function(){},
onStateChange: function(){},
onSecurityChange: function(){},
onProgressChange: function(){
aRequest.QueryInterface(Components.interfaces.nsIHttpChannel)
if( /* need to check if the object triggering the event is the xulbrowser */ ){
aRequest.cancel(Components.results.NS_BINDING_ABORTED);
}
},
QueryInterface: xpcom.utils.generateQI([Ci.nsIWebProgressListener, Ci.nsISupportsWeakReference])
}, wo._browser.webProgress.NOTIFY_ALL);
Another option that sounds promising is the nsIContentPolicy.shouldLoad() method but I really have no clue how to "create an XPCOM component that extends nsIContentPolicy and register it to the "content-policy" category using the nsICategoryManager."
Any Ideas?
I got help on this from the mozilla's #xulrunner irc channel.
Resulting solution follows.
Note: this is a module for use in Mozilla Chromeless, the require("chrome") and require("xpcom") bits will NOT be available under normal circumstances.
const {Cc, Ci, Cu, Cm, Cr} = require("chrome");
const xpcom = require("xpcom");
/***********************************************************
class definition
***********************************************************/
var description = "Chromeless Policy XPCOM Component";
/* UID generated by http://www.famkruithof.net/uuid/uuidgen */
var classID = Components.ID("{2e946f14-72d5-42f3-95b7-4907c676cf2b}");
// I just made this up. Don't know if I'm supposed to do that.
var contractID = "#mozilla.org/chromeless-policy;1";
//class constructor
function ChromelessPolicy() {
//this.wrappedJSObject = this;
}
// class definition
var ChromelessPolicy = {
// properties required for XPCOM registration:
classDescription: description,
classID: classID,
contractID: contractID,
xpcom_categories: ["content-policy"],
// QueryInterface implementation
QueryInterface: xpcom.utils.generateQI([Ci.nsIContentPolicy,
Ci.nsIFactory, Ci.nsISupportsWeakReference]),
// ...component implementation...
shouldLoad : function(aContentType, aContentLocation, aRequestOrigin, aContext, aMimeTypeGuess, aExtra) {
let result = Ci.nsIContentPolicy.ACCEPT;
// only filter DOCUMENTs (not SUB_DOCUMENTs, like iframes)
if( aContentType === Ci.nsIContentPolicy["TYPE_DOCUMENT"]
// block http(s) protocols...
&& /^http(s):/.test(aContentLocation.spec) ){
// make sure we deny the request now
result = Ci.nsIContentPolicy.REJECT_REQUEST;
}
// continue loading...
return result;
},
createInstance: function(outer, iid) {
if (outer)
throw Cr.NS_ERROR_NO_AGGREGATION;
return this.QueryInterface(iid);
}
};
let registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
try
{
Cm.nsIComponentRegistrar.registerFactory(classID, description, contractID, ChromelessPolicy);
}
catch (e) {
// Don't stop on errors - the factory might already be registered
Cu.reportError(e);
}
const categoryManager = Cc["#mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
for each (let category in ChromelessPolicy.xpcom_categories) {
categoryManager.addCategoryEntry(category, ChromelessPolicy.classDescription, ChromelessPolicy.contractID, false, true);
}
Pull Request on github for those that are interested: https://github.com/mozilla/chromeless/pull/114

Categories

Resources