okt*_*dia 5 javascript mouse google-chrome coordinate google-chrome-extension
当我单击contextMenu或使用shortcutKey时,我拼命寻找检索用户鼠标坐标的方法
我想尽可能不必使用需要移动用户的onmousemove事件:/
你知不知道怎么?
提前感谢您的回复
这只是一个简单的示例,仅适用于:
文件
- >更改"匹配":manifest.json中的 ["file:"] 添加新功能
上下文菜单选择
- >更改上下文:["选择"]在contextMenus.create(bg.js)中添加新功能
辅助鼠标按钮
- > 在(c.js)中更改(mousePos.button == 2)以添加新功能
您也可以尝试使用mousedown事件
对于运行和测试,创建这三个文件,在chrome中加载扩展,在chrome(example.txt)中加载任何文件,选择任何文本,然后,(鼠标左键单击)将出现新的上下文菜单.只需单击即可获得光标位置.
经测试和工作:2014年 3月26日在ChromeVersión33.0.1750.154上
欢迎任何评论;)
manifest.json
{
"name": "menuContext position",
"version": "0.1",
"description": "determine menuContext position",
"permissions": ["contextMenus"],
"content_security_policy": "script-src 'self'; object-src 'self'",
"background": {
"scripts": ["bg.js"]
},
"content_scripts": [{
"matches": ["file:///*/*"],
"js": ["c.js"],
"run_at": "document_end",
"all_frames": true
}],
"manifest_version": 2
}
Run Code Online (Sandbox Code Playgroud)
c.js
'use strict';
//when mouse up, send message to background.js with this position
document.addEventListener('mouseup', function (mousePos) {
if (mousePos.button == 2) {
var p = {clientX: mousePos.clientX, clientY: mousePos.clientY};
var msg = {text: 'example', point: p, from: 'mouseup'};
chrome.runtime.sendMessage(msg, function(response) {});
}
})
Run Code Online (Sandbox Code Playgroud)
bg.js
'use strict';
//global var for store cursor position
var gPos = null;
//receiving message
chrome.extension.onMessage.addListener(function(msg, sender, sendResponse) {
if (msg.from == 'mouseup') {
//storing position
gPos = msg.point;
}
})
// onclick callback function.
function OnClick(info, tab, text, mousePos) {
if (info.menuItemId == idConsole) {
if (gPos != null) {
alert('Position X: ' + gPos.clientX + '\nPosition Y: ' + gPos.clientY );
//console.log('Position X: ' + gPos.clientX + '\nPosition Y: ' + gPos.clientY );
}
}
}
//on click sample callback with more params
var idConsole = chrome.contextMenus.create({
title: 'Cursor Position',
contexts: ["selection"],
onclick: function(info, tab) {
OnClick(info, tab, '%s', gPos);
}
})
Run Code Online (Sandbox Code Playgroud)
如果可能,请在您的问题中添加标签[google-chrome-extension].问候
| 归档时间: |
|
| 查看次数: |
2588 次 |
| 最近记录: |