Firefox Addon SDK onMouseover Event for a Button

為{幸葍}努か 提交于 2019-11-26 23:43:23

问题


With the coming of multi-process Firefox, I have decided to revamp my addon. It is a toolbar addon that was built on XUL. Now I want to build it using the Addon SDK.

The old XUL overlay allowed for onMouseOver events for buttons. But the new addon SDK only has the one listener for click.

How can I get an onMouseOver (Hover) event for a toolbar button?

Maybe there is some way to add css (:hover) to the button element?

I found this, and am working on getting it in order, but maybe there's a better way?

Here is what I have so far:

var {Cu, Cc, Ci} = require("chrome");
Cu.import('resource://gre/modules/Services.jsm');

 var aDOMWindow = Services.wm.getMostRecentWindow('navigator:browser');
 aDOMWindow.addEventListener('mouseover', onSpatMouseover, true);

 function onMyMouseover(event){
    if (event.target.nodeName == 'toolbarbutton'){
        console.log(event.explicitOriginalTarget.nodeName);
        if(event.currentTarget.nodeName == '#MyButton'){
             console.log("found the button");
        }
    }
 }

But it does not yet find #MyButton.


回答1:


First of all, error message you're getting already tells you how to make it work.

But it's not necessarily what you need anyway, usually sdk/view/core provides access to the underlying XUL elements through one of its 3 methods.




回答2:


Here is a complete example of how to do this. There are two functions, actually, one for mouseover and one for mouseout. If you change the icon of a button using mouseover, you need mouseout to change it back to normal.

const { browserWindows } = require("sdk/windows");
const { CustomizableUI } = require('resource:///modules/CustomizableUI.jsm');
const { viewFor } = require("sdk/view/core");
const { ActionButton } = require("sdk/ui/button/action");

var myButton = ActionButton({
    id: "mybutton",
    label: "My Button",
    icon: { "16": "./icon-16.png", "32":"./icon-32.png", "64": "./icon-64.png" },
    onClick: function(state) {
        console.log("My Button was clicked");
    }  
});


//create a mouseover effect for a control
exports.MouseOver = (whatbutton, whatwindow, whatfunction) =>{
    CustomizableUI.getWidget( viewFor(whatbutton).id ).forWindow(whatwindow).node.addEventListener('mouseover', whatfunction, true);
};
exports.MouseOut = (whatbutton, whatwindow, whatfunction) =>{
    CustomizableUI.getWidget( viewFor(whatbutton).id ).forWindow(whatwindow).node.addEventListener('mouseout', whatfunction, true);
};    


function myMouseOverFunction(){
  console.log("mousing over...");
}
function myMouseOutFunction(){
  console.log("mousing out...");
}



//add events to the browser window
for(let w of browserWindows){
    exports.MouseOver(mybutton, viewFor(w), myMouseOverFunction);   
    exports.MouseOut(mybutton, viewFor(w), onMouseOutFunction );    
}


来源:https://stackoverflow.com/questions/34322949/firefox-addon-sdk-onmouseover-event-for-a-button

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