vimperator:plugins:firefox:javascript ::在鼠标位置获取元素并模拟单击

时间:2011-08-21 07:32:52

标签: javascript firefox mouse simulate vimperator

  • Vimperator V2.4 Pre
  • Firefox V3.6.18
  • Vista Home Basic

我想在鼠标位置获取元素,并在vimperator插件中模拟它的单击。

有些功能可以模拟鼠标点击,

How to simulate a mouse click using JavaScript?

Simulate Mouse Over in Vimperator plugin

然而,如何在vimperator插件中使用它们?

我尝试了以下代码,并在vimperator中加载并执行它:

:so! click.vimp

:javacript click();

不幸的是它不起作用。

======================click.vimp===================

js << EOF

click = function () {

    let contents=gBrowser.selectedBrowser.contentDocument;

    let evt=contents.createEvent("MouseEvents");

    evt.initMouseEvent(

        'click',

        true, // canBubble

        true, // cancelable

        window, // view

        1, // detail

        0, // screenX

        0, // screenY

        0, // clientX

        0, // clientY

        false, // ctrlKey

        false, // altKey

        false, // shiftKey

        false, // metaKey

        0, // button

        null //relatedTarget
    );

    gBrowser.selectedBrowser.contentDocument.dispatchEvent(evt);

} 
EOF

=============================================== ===========

提前致谢


我尝试了以下代码。不幸的是,&#39; elem&#39;一片空白。 有人知道如何正确地将鼠标放在当前鼠标位置吗?

js<<EOF

var x=0,y=0;
function getMousePosition(e)
{
return e.pageX ? {'x':e.pageX, 'y':e.pageY} : {'x':e.clientX + document.documentElement.scrollLeft + document.body.scrollLeft, 'y':e.clientY + document.documentElement.scrollTop + document.body.scrollTop};
}

function showMousePos(e)
{
if (!e) e = event; // make sure we have a reference to the event
var mp = getMousePosition(e);
x=mp.x;
y=mp.y;
//x = e.clientX;
//y = e.clientY;
//alert('x:'+x+'y:'+y);
}
function init()
{
document.onmousemove = showMousePos;
}

document.onmousemove=init; 

simulateClick = function ()
{

    let evt=document.createEvent("MouseEvents");
    evt.initMouseEvent(
        'click',
        true, // canBubble
        true, // cancelable
        window, // view
        1, // detail
        0, // screenX
        0, // screenY
        0, // clientX
        0, // clientY
        false, // ctrlKey
        false, // altKey
        false, // shiftKey
        false, // metaKey
        0, // button
        null //relatedTarget
    );
    let elem = document.elementFromPoint(x, y);
    elem.dispatchEvent(evt);
}
EOF

=============================================== ==

好吧,我今天得到了一个尴尬的解决方法。

步骤1。将Vimpeator-modes.js修改为以下

set: function (mainMode, extendedMode, silent, stack) {
    silent = (silent || this._main == mainMode && this._extended == extendedMode);
    // if a this._main mode is set, the this._extended is always cleared
    let oldMain = this._main, oldExtended = this._extended;
    if (typeof extendedMode === "number")
        this._extended = extendedMode;
    if (typeof mainMode === "number") {
        this._main = mainMode;
        if (!extendedMode)
            this._extended = modes.NONE;

        if (this._main != oldMain)
            this._handleModeChange(oldMain, mainMode, oldExtended);
    }
    liberator.triggerObserver("modeChange", [oldMain, oldExtended], [this._main, this._extended], stack);

    options.titlestring = mainMode;
    if (!silent)
        this.show();
},

步骤2:在AutoHotkey脚本下面运行

d::

WinGetActiveTitle, title
StringRight, whichmode, title, 1
if ( whichmode = "1" or whichmode = "6")
{
    Send, {Click}
}
else
{
    Send, d
}
return

=============================================== ==============

这种方式可以映射&#39; d&#39;鼠标点击并输入原始字符&#39; d&#39;因为模式是insert / command_line等。

我的问题是有谁知道更好的方法?感谢

0 个答案:

没有答案