在Vimperator插件中模拟鼠标悬停

时间:2022-11-03 18:02:05

I'm attempting to write a Vimperator plugin to allow use of hints mode to simulate mouse over on drop down menus. I have the hints mode working and can correctly choose elements that have mouseover events attached. The problem is my function to simulate the mouse over is not working. This is what I currently have:

我正在尝试编写一个Vimperator插件,允许使用提示模式在下拉菜单上模拟鼠标。我有提示模式工作,可以正确选择附加鼠标悬停事件的元素。问题是我的模拟鼠标功能不起作用。这就是我目前所拥有的:

function SimulateMouseOver(elem)
{
    var evt = elem.ownerDocument.createEvent('MouseEvents');
    evt.initMouseEvent('mouseover',true,true,
        elem.ownerDocument.defaultView,0,0,0,0,0,
        false,false,false,false,0,null);
    var canceled = !elem.dispatchEvent(evt);
    if(canceled)
        alert('Event Cancelled');
}

The above code works for some pages but not for others. For example it doesn't work on AccuWeather. Any ideas how to simulate a mouse over that will work for most pages?

上面的代码适用于某些页面,但不适用于其他页面。例如,它不适用于AccuWeather。任何关于如何模拟鼠标的想法都适用于大多数页面?

3 个解决方案

#1


here's some code to start with to create the event, simpler and works for more browsers (if you don't need to specify exact mouse coordinates)

这里有一些代码开始创建事件,更简单,适用于更多浏览器(如果您不需要指定精确的鼠标坐标)

        if( document.createEvent ) {
            var evObj = document.createEvent('MouseEvents');
            evObj.initEvent( 'mouseover', true, false );
            elem.dispatchEvent(evObj);
        } else if( document.createEventObject ) {
            elem.fireEvent('onmouseover');
        }

hope that helps

希望有所帮助

#2


In case anyone bumps into this looking for a framework agnostic way to fire any HTML and Mouse event (and set some options, if needed), have a look here: How to simulate a mouse click using JavaScript?

如果有人碰到这个寻找框架无关的方法来触发任何HTML和鼠标事件(并设置一些选项,如果需要),请看一下:如何使用JavaScript模拟鼠标点击?

#3


You may only trigger mouseover event on fields/elements that have a mouseover event bound to them. You can't just hijack the mouse.

您只能在绑定了鼠标悬停事件的字段/元素上触发鼠标悬停事件。你不能只是劫持鼠标。

#1


here's some code to start with to create the event, simpler and works for more browsers (if you don't need to specify exact mouse coordinates)

这里有一些代码开始创建事件,更简单,适用于更多浏览器(如果您不需要指定精确的鼠标坐标)

        if( document.createEvent ) {
            var evObj = document.createEvent('MouseEvents');
            evObj.initEvent( 'mouseover', true, false );
            elem.dispatchEvent(evObj);
        } else if( document.createEventObject ) {
            elem.fireEvent('onmouseover');
        }

hope that helps

希望有所帮助

#2


In case anyone bumps into this looking for a framework agnostic way to fire any HTML and Mouse event (and set some options, if needed), have a look here: How to simulate a mouse click using JavaScript?

如果有人碰到这个寻找框架无关的方法来触发任何HTML和鼠标事件(并设置一些选项,如果需要),请看一下:如何使用JavaScript模拟鼠标点击?

#3


You may only trigger mouseover event on fields/elements that have a mouseover event bound to them. You can't just hijack the mouse.

您只能在绑定了鼠标悬停事件的字段/元素上触发鼠标悬停事件。你不能只是劫持鼠标。