Question

Is it possible to use nsISelectionController to watch when a a highlight/selection is made?

I know that there are different selection scopes. I want to watch when a user makes a selection in the default scope seen at MXR - nsISelectionController Constants.

Kind of like an addEventListener on select change but on the text nodes of the document.

Thanks

Was it helpful?

Solution

I found a solution but it doesn't use nsIController as @Neil had recommended in a SO topic HERE to look at viewSource.js.

Im still interested in a nsIController solution if possible, im trying to understand that sucker it confuses me.

So this is how you observer for a selection:

 var mylis = {
   timeout: 0,
   notifySelectionChanged: function(doc, sel, reason)
   {
       if (!this.timeout) {
           this.timeout = setTimeout(function() {
               console.log('notifySelectionChanged','doc=',doc,'sel=',sel,'reason=',reason);
               mylis.timeout = 0;
           }, 1000);
       }
   }
 }

gBrowser.contentWindow.getSelection().QueryInterface(Ci.nsISelectionPrivate).addSelectionListener(mylis);

//gBrowser.contentWindow.getSelection().QueryInterface(Ci.nsISelectionPrivate).removeSelectionListener(mylis);

the timeout is important because otherwise it will slow down the browser thread. you can see as you highlight its all gimicky. viewSource.js used 100ms so I would reocmmend that.

MXR - viewSource.js

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top