vimperator:plugins:firefox:javascript::获取鼠标位置的元素并模拟点击
- Vimperator V2.4 Pre
- Firefox V3.6.18
- Vista Home Basic
我想获取鼠标位置的元素并在 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' /command_line 等
我的问题是有人知道更好的方法吗?谢谢
- 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, // 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
=================================================
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论