Javascript 在google chrome扩展中获取鼠标坐标

Javascript 在google chrome扩展中获取鼠标坐标,javascript,google-chrome,google-chrome-extension,mouse,coordinate,Javascript,Google Chrome,Google Chrome Extension,Mouse,Coordinate,我拼命地寻找在单击我的上下文菜单或使用快捷键时检索用户鼠标坐标的方法 如果可能,我希望不必使用需要移动用户的onmousemove事件:/ 你知道怎么做吗 提前感谢您的回复这只是一个简单的示例,仅适用于: 文件 ->更改manifest.json中的“匹配项”:[“文件:”]以添加新功能 上下文菜单选择 ->更改上下文:[“选择”]在contextMenus.create(bg.js)中添加新功能 辅助鼠标按钮 ->更改(c.js)中的(mousePos.button==2)以添加新功能 您也可

我拼命地寻找在单击我的上下文菜单或使用快捷键时检索用户鼠标坐标的方法

如果可能,我希望不必使用需要移动用户的onmousemove事件:/

你知道怎么做吗


提前感谢您的回复

这只是一个简单的示例,仅适用于:


文件 ->更改manifest.json中的“匹配项”:[“文件:”]以添加新功能

上下文菜单选择 ->更改上下文:[“选择”]在contextMenus.create(bg.js)中添加新功能

辅助鼠标按钮
->更改(c.js)中的(mousePos.button==2)以添加新功能

您也可以尝试使用mousedown事件

对于run and test,创建这三个文件,在chrome中加载扩展名,在chrome中加载任何文件(example.txt),选择任何文本,然后,(鼠标二次键单击)出现“新建”上下文菜单。只需单击获取光标位置

测试和工作:2014年3月26日chrome版本33.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
}
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) {});
  }
})
'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);
  }
})
bg.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) {});
  }
})
'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);
  }
})

如果可能,请在您的问题中添加标签[google chrome extension]。问候语

如果测试正常,不要忘记标记为有效响应。如果测试失败,请评论。问候非常感谢:不客气!别忘了添加标签[google chrome extension]。标签对于寻找解决方案的人很有用。