vimperator:plugins:firefox:javascript::Get the element at mouse position and simulate a click
- Vimperator V2.4 Pre
- Firefox V3.6.18
- Vista Home Basic
I would like to get the element at mouse position and simulate a click on it in vimperator plugins.
There are some functions that could simulate a mouse click,
How to simulate a mouse click using JavaScript?
Simulate Mouse Over in Vimperator plugin
However, how use them in vimperator plugins?
I tried the following code, and load it and execute it like this in vimperator:
:so! click.vimp
:javacript click();
unfortunately it could not work.
======================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
==========================================================
Thanks in advance
i tried the following code. Unfortunately, 'elem' is NULL. Somebody know how to get the element at current mouse position correctly?
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, // canBubb开发者_运维问答le
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
=================================================
Well, i got a awkward workaround today.
Step1. Modify the Vimpeator-modes.js as the following
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();
},
Step2: Run below AutoHotkey Script
d::
WinGetActiveTitle, title
StringRight, whichmode, title, 1
if ( whichmode = "1" or whichmode = "6")
{
Send, {Click}
}
else
{
Send, d
}
return
=============================================================
This way can map 'd' to mouse click and input raw char 'd' as while the mode is insert/command_line, etc.
My question is anybody know more better method? thanks
精彩评论