- Vimperator V2.4 Pre
- 火狐 V3.6.18
- Vista家庭基本
我想在鼠标位置获取元素并在 vimperator 插件中模拟点击它。
有一些功能可以模拟鼠标点击,
但是,如何在 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
==================================================== ========
提前致谢
我尝试了以下代码。不幸的是,'elem' 为 NULL。有人知道如何正确获取当前鼠标位置的元素吗?
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();
},
Step2: 在 AutoHotkey 脚本下运行
d::
WinGetActiveTitle, title
StringRight, whichmode, title, 1
if ( whichmode = "1" or whichmode = "6")
{
Send, {Click}
}
else
{
Send, d
}
return
==================================================== ===========
这种方式可以将“d”映射到鼠标单击并输入原始字符“d”,就像模式是插入/命令行等一样。
我的问题是有人知道更好的方法吗?谢谢