Observe for highlight?

巧了我就是萌 提交于 2019-12-02 13:22:58

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/22216324/observe-for-highlight

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!